From b17eef40535101ae8dd5d352836a885f5f2cd7e1 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 2 Nov 2021 15:32:43 +0300 Subject: [PATCH] allow live-reload on localhost and updates on file:// ...if file access is allowed --- background/background.js | 2 ++ background/update-manager.js | 4 +++- install-usercss/install-usercss.js | 18 ++++++++++++------ install-usercss/preinit.js | 5 ++++- js/toolbox.js | 2 ++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/background/background.js b/background/background.js index 2cdbc06c..b3a31a92 100644 --- a/background/background.js +++ b/background/background.js @@ -175,6 +175,8 @@ msg.on((msg, sender) => { //#endregion Promise.all([ + browser.extension.isAllowedFileSchemeAccess() + .then(res => API.data.set('hasFileAccess', res)), bgReady.styles, /* These are loaded conditionally. Each item uses `require` individually so IDE can jump to the source and track usage. */ diff --git a/background/update-manager.js b/background/update-manager.js index 9797c115..0c84cef8 100644 --- a/background/update-manager.js +++ b/background/update-manager.js @@ -223,7 +223,9 @@ const updateMan = (() => { let err; if (!delta && !ignoreDigest) { // re-install is invalid in a soft upgrade - err = response === style.sourceCode ? STATES.SAME_CODE : STATES.SAME_VERSION; + err = response === style.sourceCode + ? STATES.SAME_CODE + : !URLS.isLocalhost(updateUrl) && STATES.SAME_VERSION; } if (delta < 0) { // downgrade is always invalid diff --git a/install-usercss/install-usercss.js b/install-usercss/install-usercss.js index 8c978a0d..2cc9a06a 100644 --- a/install-usercss/install-usercss.js +++ b/install-usercss/install-usercss.js @@ -1,6 +1,6 @@ /* global $ $create $createLink $$remove showSpinner */// dom.js /* global API */// msg.js -/* global closeCurrentTab deepEqual */// toolbox.js +/* global URLS closeCurrentTab deepEqual */// toolbox.js /* global messageBox */ /* global prefs */ /* global preinit */ @@ -61,12 +61,18 @@ setTimeout(() => !cm && showSpinner($('#header')), 200); '/js/color/color-view', ])); - ({tabId, initialUrl} = await preinit); + ({tabId, initialUrl} = preinit); liveReload = initLiveReload(); - const {dup, style, error, sourceCode} = await preinit.ready; + const [ + {dup, style, error, sourceCode}, + hasFileAccess, + ] = await Promise.all([ + preinit.ready, + API.data.get('hasFileAccess'), + ]); if (!style && sourceCode == null) { - messageBox.alert(isNaN(error) ? error : 'HTTP Error ' + error, 'pre'); + messageBox.alert(isNaN(error) ? `${error}` : 'HTTP Error ' + error, 'pre'); return; } await scriptsReady; @@ -118,7 +124,7 @@ setTimeout(() => !cm && showSpinner($('#header')), 200); checker.checked = true; // there is no way to "unset" updateUrl, you can only overwrite it. checker.disabled = true; - } else if (updateUrl.protocol !== 'file:') { + } else if (updateUrl.protocol !== 'file:' || hasFileAccess) { checker.checked = true; style.updateUrl = updateUrl.href; } @@ -129,7 +135,7 @@ setTimeout(() => !cm && showSpinner($('#header')), 200); $('.set-update-url p').textContent = updateUrl.href.length < 300 ? updateUrl.href : updateUrl.href.slice(0, 300) + '...'; - if (initialUrl.startsWith('file:')) { + if (URLS.isLocalhost(initialUrl)) { $('.live-reload input').onchange = liveReload.onToggled; } else { $('.live-reload').remove(); diff --git a/install-usercss/preinit.js b/install-usercss/preinit.js index 6f37f2e0..8451e095 100644 --- a/install-usercss/preinit.js +++ b/install-usercss/preinit.js @@ -25,7 +25,10 @@ const preinit = (() => { function DirectDownloader() { let oldCode = null; return async () => { - const code = await download(initialUrl); + const code = await download(initialUrl, { + // Disabling cache on http://localhost otherwise the recheck delay gets too big + headers: {'Cache-Control': 'no-cache, no-store'}, + }); if (oldCode !== code) { oldCode = code; return code; diff --git a/js/toolbox.js b/js/toolbox.js index a5966342..a11ae1c0 100644 --- a/js/toolbox.js +++ b/js/toolbox.js @@ -115,6 +115,8 @@ const URLS = { url.startsWith(URLS.ownOrigin) || !URLS.chromeProtectsNTP && url.startsWith('chrome://newtab/') ), + + isLocalhost: url => /^file:|^https?:\/\/(localhost|127\.0\.0\.1)\//.test(url), }; const RX_META = /\/\*!?\s*==userstyle==[\s\S]*?==\/userstyle==\s*\*\//i;