mirror of
https://github.com/SoPat712/videospeed.git
synced 2025-08-21 18:08:46 -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)
|
||||
return;
|
||||
|
||||
var readyStateCheckInterval = setInterval(function() {
|
||||
if (document && document.readyState === 'complete') {
|
||||
clearInterval(readyStateCheckInterval);
|
||||
window.onload = () => initializeNow(document);
|
||||
if (document) {
|
||||
if (document.readyState === "complete") {
|
||||
initializeNow(document);
|
||||
} else {
|
||||
document.onreadystatechange = () => {
|
||||
if (document.readyState === "complete") {
|
||||
initializeNow(document);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 10);
|
||||
}
|
||||
}
|
||||
|
||||
function initializeNow(document) {
|
||||
// in theory, this should only run once, in practice..
|
||||
// that's not guaranteed, hence we enforce own init-once.
|
||||
// enforce init-once due to redundant callers
|
||||
if (document.body.classList.contains('vsc-initialized')) {
|
||||
return;
|
||||
}
|
||||
|
Reference in New Issue
Block a user