Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] - Throttle Playlist #24393

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class PlaylistScriptHandler: NSObject, TabContentScript {
handlerNamesMap: [
"$<message_handler>": messageHandlerName,
"$<tagUUID>": "tagId_\(uniqueID)",
"$<sendMessageTimeout>": "smt_\(uniqueID)",
"$<playlistLongPressed>": playlistLongPressed,
"$<playlistProcessDocumentLoad>": playlistProcessDocumentLoad,
"$<mediaCurrentTimeFromTag>": mediaCurrentTimeFromTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ window.__firefox__.includeOnce("Playlist", function($) {
}

let sendMessage = $(function(name, node, target, type, detected) {
$(function() {
let post = $(function() {
var location = "";
var pageTitle = "";

Expand All @@ -90,7 +90,16 @@ window.__firefox__.includeOnce("Playlist", function($) {
"tagId": target.$<tagUUID>,
"invisible": !target.parentNode
});
})();
});

if (node.$<sendMessageTimeout>) {
clearTimeout(node.$<sendMessageTimeout>);
}

node.$<sendMessageTimeout> = setTimeout(function(){
node.$<sendMessageTimeout> = null;
post();
}, 2000);
});

function isVideoNode(node) {
Expand Down Expand Up @@ -228,21 +237,14 @@ window.__firefox__.includeOnce("Playlist", function($) {
// MARK: ---------------------------------------

function setupDetector() {
function requestWhenIdleShim(fn) {
var start = Date.now()
return setTimeout(function () {
fn({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start))
},
})
}, 2000); // Resolution of 1000ms is fine for us.
}

var readyTimeout = null;
function onReady(fn) {
if (document.readyState === "complete" || document.readyState === "ready") {
fn();
if (readyTimeout) {
clearTimeout(readyTimeout);
}

readyTimeout = setTimeout(fn, 2000);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
Expand Down Expand Up @@ -345,7 +347,7 @@ window.__firefox__.includeOnce("Playlist", function($) {
};*/

function checkPageForVideos(ignoreSource) {
onReady(function() {
function fetchMedia() {
let videos = getAllVideoElements();
let audios = getAllAudioElements();

Expand All @@ -372,6 +374,10 @@ window.__firefox__.includeOnce("Playlist", function($) {
}
notifyNode(node, 'audio', true, ignoreSource);
});
}

onReady(function() {
fetchMedia();

$(function() {
$.postNativeMessage('$<message_handler>', {
Expand All @@ -381,26 +387,9 @@ window.__firefox__.includeOnce("Playlist", function($) {
})();
});

// Timeinterval is needed for DailyMotion as their DOM is bad
let interval = setInterval(function() {
getAllVideoElements().forEach(function(node) {
if (useObservers) {
observeNode(node);
}
notifyNode(node, 'video', true, ignoreSource);
});

getAllAudioElements().forEach(function(node) {
if (useObservers) {
observeNode(node);
}
notifyNode(node, 'audio', true, ignoreSource);
});
}, 1000);

let timeout = setTimeout(function() {
clearInterval(interval);
}, 10000);
setTimeout(function() {
fetchMedia();
}, 5000);
}

// Needed for Japanese videos like tver.jp which literally never loads automatically
Expand All @@ -414,21 +403,25 @@ window.__firefox__.includeOnce("Playlist", function($) {
}
});

// Needed for pages like Bichute and Soundcloud and Youtube that do NOT reload the page
// They instead alter the history or document and update the href that way
window.addEventListener("load", () => {
let lastLocation = document.location.href;
const body = document.querySelector("body");
const observer = new MutationObserver(mutations => {
if (lastLocation !== document.location.href) {
lastLocation = document.location.href;
checkPageForVideos(false);
}
if (useObservers) {
// Needed for pages like Bichute and Soundcloud and Youtube that do NOT reload the page
// They instead alter the history or document and update the href that way
window.addEventListener("load", () => {
let lastLocation = document.location.href;
const body = document.querySelector("body");
const observer = new MutationObserver(mutations => {
if (lastLocation !== document.location.href) {
lastLocation = document.location.href;
checkPageForVideos(false);
}
});
observer.observe(body, { childList: true, subtree: true });
});
observer.observe(body, { childList: true, subtree: true });
});
}

checkPageForVideos(false);
setTimeout(function() {
checkPageForVideos(false);
}, 2000);
}

observePage();
Expand Down
Loading