mirror of
https://github.com/SoPat712/videospeed.git
synced 2025-08-21 18:08:46 -04:00
delay DOM node walking via requestIdleCallback
This commit is contained in:

committed by
Ilya Grigorik

parent
e85dad3280
commit
8e199cd975
19
inject.js
19
inject.js
@@ -285,18 +285,29 @@ chrome.runtime.sendMessage({}, function(response) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process the DOM nodes lazily
|
||||||
|
function enqueueCheckNodes(node, parent, added) {
|
||||||
|
const checkNodes = _ => checkForVideo(node, parent, added);
|
||||||
|
if ('requestIdleCallback' in window) {
|
||||||
|
requestIdleCallback(checkNodes, {timeout: 1000});
|
||||||
|
} else {
|
||||||
|
checkNodes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var observer = new MutationObserver(function(mutations) {
|
var observer = new MutationObserver(function(mutations) {
|
||||||
mutations.forEach(function(mutation) {
|
mutations.forEach(function(mutation) {
|
||||||
forEach.call(mutation.addedNodes, function(node) {
|
forEach.call(mutation.addedNodes, function(node) {
|
||||||
if (typeof node === "function")
|
if (typeof node === "function")
|
||||||
return;
|
return;
|
||||||
checkForVideo(node, node.parentNode || mutation.target, true);
|
enqueueCheckNodes(node, node.parentNode || mutation.target, true);
|
||||||
})
|
});
|
||||||
forEach.call(mutation.removedNodes, function(node) {
|
forEach.call(mutation.removedNodes, function(node) {
|
||||||
if (typeof node === "function")
|
if (typeof node === "function")
|
||||||
return;
|
return;
|
||||||
checkForVideo(node, node.parentNode || mutation.target, false);
|
enqueueCheckNodes(node, node.parentNode || mutation.target, false);
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
observer.observe(document, { childList: true, subtree: true });
|
observer.observe(document, { childList: true, subtree: true });
|
||||||
|
Reference in New Issue
Block a user