Skip to content

Commit 81b3851

Browse files
committed
Fixed dynamic script injection failing sometimes with "No matching message handler" error.
1 parent e7fcd76 commit 81b3851

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/bg/RequestUtil.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
frameId,
3131
}, details);
3232
try {
33-
await browser.tabs.executeScript(tabId, details);
33+
for (let attempts = 10; attempts-- > 0;) {
34+
try {
35+
await browser.tabs.executeScript(tabId, details);
36+
} catch(e) {
37+
if (!/No matching message handler/.test(e.message)) throw e;
38+
debug("Couldn't inject script into %s: too early? Retrying up to %s times...", url, attempts);
39+
}
40+
}
3441
count++;
3542
debug("Execute on start OK", url, details);
3643
} catch (e) {

src/content/content.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ async function init() {
8585
debug("canScript:", canScript);
8686
} catch (e) {
8787
debug("Error querying canScript", e);
88-
// background script not initialized yet?
89-
setTimeout(() => init(), 100);
88+
if (document.readyState !== "complete" &&
89+
document.URL !== "about:blank" &&
90+
/Receiving end does not exist/.test(e.message)) {
91+
window.location.reload(false);
92+
} else {
93+
setTimeout(() => init(), 100);
94+
}
9095
return;
9196
} finally {
9297
queryingCanScript = false;

0 commit comments

Comments
 (0)