From 51e115d3bcd2ad683f264e7d9c97347e0afac14e Mon Sep 17 00:00:00 2001 From: hackademix Date: Wed, 23 Oct 2019 17:12:01 +0200 Subject: Cleaner and tighter usage of SyncMessage to fetch policies. --- src/content/staticNS.js | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) (limited to 'src/content') diff --git a/src/content/staticNS.js b/src/content/staticNS.js index a175220..40e2420 100644 --- a/src/content/staticNS.js +++ b/src/content/staticNS.js @@ -3,22 +3,6 @@ let listenersMap = new Map(); let backlog = new Set(); - let stopAndReload = beforeReloading => { - debug("Should I reload? %o, now: %s", performance.now()) - if (location.href === "about:blank" || performance.now() > 10000 ) { - debug("Won't reload."); - return; - } - stop(); - setTimeout(() => { - debug("Reloading..."); - if (typeof beforeReloading === "function") { - beforeReloading(); - } - location.reload(); - }, 1000) - }; - let ns = { debug: true, // DEV_ONLY get embeddingDocument() { @@ -53,17 +37,25 @@ debug(`Fetching policy from document %s, readyState %s, content %s`, document.URL, document.readyState, document.documentElement.outerHTML); let url = document.URL; - let isFileUrl = url.startsWith("file:"); - if (isFileUrl) { - addEventListener("beforescriptexecute", e => { - if (!this.canScript) e.preventDefault(); - }, true); - } - - let policy = browser.runtime.sendSyncMessage( - {id: "fetchPolicy", url, contextUrl: url}); + addEventListener("beforescriptexecute", e => { + // safety net for syncrhonous load on Firefox + if (!this.canScript) e.preventDefault(); + }, true); - debug("Fetched %o, readyState %s", policy, document.readyState); + let policy = null; + for (;;) { + try { + policy = browser.runtime.sendSyncMessage( + {id: "fetchPolicy", url, contextUrl: url}); + break; + } catch (e) { + if (e.message !== "Could not esablish connection. Receiving end does not exist.") { + break; + } + error("Background page ready yet, retrying to fetch policy...") + } + } + debug("Fetched %o, readyState %s", policy, document.readyState); // DEV_ONLY this.setup(policy); return true; -- cgit v1.2.3