From 419a05e801da688a1d0917a6bf16d468e6362a3d Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 9 Aug 2017 16:51:25 +0200 Subject: [PATCH] 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. --- src/webex/wxBackend.ts | 39 +++++++++++++++++++++++++++++---------- 1 file 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 @@ -525,6 +525,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. * @@ -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()); }); };