Fixed dynamic script injection failing sometimes with "No matching message handler" error.

This commit is contained in:
hackademix 2018-07-22 16:50:24 +02:00
parent e7fcd76705
commit 81b3851256
2 changed files with 15 additions and 3 deletions

View File

@ -30,7 +30,14 @@
frameId,
}, details);
try {
await browser.tabs.executeScript(tabId, details);
for (let attempts = 10; attempts-- > 0;) {
try {
await browser.tabs.executeScript(tabId, details);
} catch(e) {
if (!/No matching message handler/.test(e.message)) throw e;
debug("Couldn't inject script into %s: too early? Retrying up to %s times...", url, attempts);
}
}
count++;
debug("Execute on start OK", url, details);
} catch (e) {

View File

@ -85,8 +85,13 @@ async function init() {
debug("canScript:", canScript);
} catch (e) {
debug("Error querying canScript", e);
// background script not initialized yet?
setTimeout(() => init(), 100);
if (document.readyState !== "complete" &&
document.URL !== "about:blank" &&
/Receiving end does not exist/.test(e.message)) {
window.location.reload(false);
} else {
setTimeout(() => init(), 100);
}
return;
} finally {
queryingCanScript = false;