mirror of
https://github.com/SoPat712/videospeed.git
synced 2025-08-22 02:18:45 -04:00
Use event-driven or direct script initialization
Chrome may inject the script immediately after the readystatechange/load events fired, so we need to explicitly check the readyState after script injection. Also unconditionally listen to the window.onload event for further cross-browser robustness (we have init-once logic either way).
This commit is contained in:

committed by
Ilya Grigorik

parent
2818103c7d
commit
58e032b14a
17
inject.js
17
inject.js
@@ -193,17 +193,22 @@ chrome.extension.sendMessage({}, function(response) {
|
|||||||
if (blacklisted)
|
if (blacklisted)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var readyStateCheckInterval = setInterval(function() {
|
window.onload = () => initializeNow(document);
|
||||||
if (document && document.readyState === 'complete') {
|
if (document) {
|
||||||
clearInterval(readyStateCheckInterval);
|
if (document.readyState === "complete") {
|
||||||
|
initializeNow(document);
|
||||||
|
} else {
|
||||||
|
document.onreadystatechange = () => {
|
||||||
|
if (document.readyState === "complete") {
|
||||||
initializeNow(document);
|
initializeNow(document);
|
||||||
}
|
}
|
||||||
}, 10);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeNow(document) {
|
function initializeNow(document) {
|
||||||
// in theory, this should only run once, in practice..
|
// enforce init-once due to redundant callers
|
||||||
// that's not guaranteed, hence we enforce own init-once.
|
|
||||||
if (document.body.classList.contains('vsc-initialized')) {
|
if (document.body.classList.contains('vsc-initialized')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user