From 8621a36fa1b03b4ffd2e78ef444bf30037d8b9c2 Mon Sep 17 00:00:00 2001 From: hackademix Date: Wed, 2 Oct 2019 00:02:01 +0200 Subject: Make extra efforts to ensure safety net reloads happen only once on startup. --- src/content/staticNS.js | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) (limited to 'src') 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; -- cgit v1.2.3