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:
Johannes Pfrang
2017-08-04 23:41:14 +02:00
committed by Ilya Grigorik
parent 2818103c7d
commit 58e032b14a

View File

@@ -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;
} }