diff options
author | hackademix | 2019-10-02 00:02:01 +0200 |
---|---|---|
committer | hackademix | 2019-10-02 00:02:01 +0200 |
commit | 8621a36fa1b03b4ffd2e78ef444bf30037d8b9c2 (patch) | |
tree | 679ffe7ebebd2de4819f1fe69492400dc823b672 | |
parent | 1b3a02659e4b0ece9640c5916548bd3cca383791 (diff) | |
download | noscript-8621a36fa1b03b4ffd2e78ef444bf30037d8b9c2.tar.gz noscript-8621a36fa1b03b4ffd2e78ef444bf30037d8b9c2.tar.xz noscript-8621a36fa1b03b4ffd2e78ef444bf30037d8b9c2.zip |
Make extra efforts to ensure safety net reloads happen only once on startup.
-rw-r--r-- | src/content/staticNS.js | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/src/content/staticNS.js b/src/content/staticNS.js index 3102756..e23fed9 100644 --- a/src/content/staticNS.js +++ b/src/content/staticNS.js @@ -3,6 +3,23 @@ let listenersMap = new Map(); let backlog = new Set(); + let stopAndReload = beforeReloading => { + stop(); + let navTimes = performance.getEntriesByType && performance.getEntriesByType("navigation")[0]; + debug("Should I reload? %o, now: %s", navTimes, performance.now()) + if (navTimes && navTimes.type === "reload" || performance.now() > 10000) { + debug("Won't reload."); + return; + } + setTimeout(() => { + debug("Reloading..."); + if (typeof beforeReloading === "function") { + beforeReloading(); + } + location.reload(); + }, 1000) + }; + let ns = { debug: true, // DEV_ONLY get embeddingDocument() { @@ -36,17 +53,12 @@ fetchPolicy() { debug(`Fetching policy from document %s, readyState %s, content %s`, document.URL, document.readyState, document.documentElement.outerHTML); - let url = document.URL; - - if (url.startsWith("file:")) { + let isFileUrl = url.startsWith("file:"); + if (isFileUrl) { let cookie = "noscript.startupFileReloaded=true"; if (!document.cookie.split(/\s*;\s*/).includes(cookie)) { - stop(); - setTimeout(() => { - document.cookie = cookie; - location.reload(); - }, 10) + stopAndReload(() => document.cookie = cookie); } } @@ -56,18 +68,17 @@ debug("Fetched %o, readyState %s", policy, document.readyState); if (!policy) { debug("Could not fetch policy!"); - if (url.startsWith("file:") && !sessionStorage.__noScriptFallbackReload__) { + if (isFileUrl && !sessionStorage.__noScriptFallbackReload__) { sessionStorage.__noScriptFallbackReload__ = "true"; - location.reload(); - } else { - // let's try asynchronously - (async () => { - this.setup(await Messages.send("fetchPolicy", {url, contextUrl: url})); - })(); + stopAndReload(); } + // let's try asynchronously + (async () => { + this.setup(await Messages.send("fetchPolicy", {url, contextUrl: url})); + })(); return false; } else if (policy.fallback) { - location.reload(); + stopAndReload(); } this.setup(policy); return true; |