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