diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-08-09 16:51:25 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-08-09 16:51:25 +0200 |
commit | 419a05e801da688a1d0917a6bf16d468e6362a3d (patch) | |
tree | 5e499f969ed7d75aae0fa42f91dbb91f3b97979a /src | |
parent | 1f8c3af5f9143cecaf977f9396c3c8ee7f909735 (diff) |
fix errors in injection
Sometimes chrome reports a different URL for a tab than what it is
internally displaying (e.g. for error pages). Previously this lead to a
"scary" error message being logged.
Diffstat (limited to 'src')
-rw-r--r-- | src/webex/wxBackend.ts | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/src/webex/wxBackend.ts b/src/webex/wxBackend.ts index a8b725495..261477386 100644 --- a/src/webex/wxBackend.ts +++ b/src/webex/wxBackend.ts @@ -526,6 +526,24 @@ async function reinitWallet() { /** + * Inject a script into a tab. Gracefully logs errors + * and works around a bug where the tab's URL does not match the internal URL, + * making the injection fail in a confusing way. + */ +function injectScript(tabId: number, details: chrome.tabs.InjectDetails, actualUrl: string): void { + chrome.tabs.executeScript(tabId, details, () => { + // Required to squelch chrome's "unchecked lastError" warning. + // Sometimes chrome reports the URL of a tab as http/https but + // injection fails. This can happen when a page is unloaded or + // shows a "no internet" page etc. + if (chrome.runtime.lastError) { + console.warn("injection failed on page", actualUrl, chrome.runtime.lastError.message); + } + }); +} + + +/** * Main function to run for the WebExtension backend. * * Sets up all event handlers and other machinery. @@ -548,16 +566,17 @@ export async function wxMain() { return; } const uri = new URI(tab.url); - if (uri.protocol() === "http" || uri.protocol() === "https") { - console.log("injecting into existing tab", tab.id); - chrome.tabs.executeScript(tab.id, { file: "/dist/contentScript-bundle.js" }); - const code = ` - if (("taler" in window) || document.documentElement.getAttribute("data-taler-nojs")) { - document.dispatchEvent(new Event("taler-probe-result")); - } - `; - chrome.tabs.executeScript(tab.id, { code, runAt: "document_idle" }); + if (uri.protocol() !== "http" && uri.protocol() !== "https") { + return; } + console.log("injecting into existing tab", tab.id, "with url", uri.href(), "protocol", uri.protocol()); + injectScript(tab.id, { file: "/dist/contentScript-bundle.js" }, uri.href()); + const code = ` + if (("taler" in window) || document.documentElement.getAttribute("data-taler-nojs")) { + document.dispatchEvent(new Event("taler-probe-result")); + } + `; + injectScript(tab.id, { code, runAt: "document_idle" }, uri.href()); } }); @@ -598,7 +617,7 @@ export async function wxMain() { document.dispatchEvent(new Event("taler-probe-result")); } `; - chrome.tabs.executeScript(tab.id!, { code, runAt: "document_start" }); + injectScript(tab.id!, { code, runAt: "document_start" }, uri.href()); }); }; |