allow live-reload on localhost and updates on file://

...if file access is allowed
This commit is contained in:
tophf 2021-11-02 15:32:43 +03:00
parent 7ad3f94697
commit b17eef4053
5 changed files with 23 additions and 8 deletions

View File

@ -175,6 +175,8 @@ msg.on((msg, sender) => {
//#endregion //#endregion
Promise.all([ Promise.all([
browser.extension.isAllowedFileSchemeAccess()
.then(res => API.data.set('hasFileAccess', res)),
bgReady.styles, bgReady.styles,
/* These are loaded conditionally. /* These are loaded conditionally.
Each item uses `require` individually so IDE can jump to the source and track usage. */ Each item uses `require` individually so IDE can jump to the source and track usage. */

View File

@ -223,7 +223,9 @@ const updateMan = (() => {
let err; let err;
if (!delta && !ignoreDigest) { if (!delta && !ignoreDigest) {
// re-install is invalid in a soft upgrade // 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) { if (delta < 0) {
// downgrade is always invalid // downgrade is always invalid

View File

@ -1,6 +1,6 @@
/* global $ $create $createLink $$remove showSpinner */// dom.js /* global $ $create $createLink $$remove showSpinner */// dom.js
/* global API */// msg.js /* global API */// msg.js
/* global closeCurrentTab deepEqual */// toolbox.js /* global URLS closeCurrentTab deepEqual */// toolbox.js
/* global messageBox */ /* global messageBox */
/* global prefs */ /* global prefs */
/* global preinit */ /* global preinit */
@ -61,12 +61,18 @@ setTimeout(() => !cm && showSpinner($('#header')), 200);
'/js/color/color-view', '/js/color/color-view',
])); ]));
({tabId, initialUrl} = await preinit); ({tabId, initialUrl} = preinit);
liveReload = initLiveReload(); 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) { if (!style && sourceCode == null) {
messageBox.alert(isNaN(error) ? error : 'HTTP Error ' + error, 'pre'); messageBox.alert(isNaN(error) ? `${error}` : 'HTTP Error ' + error, 'pre');
return; return;
} }
await scriptsReady; await scriptsReady;
@ -118,7 +124,7 @@ setTimeout(() => !cm && showSpinner($('#header')), 200);
checker.checked = true; checker.checked = true;
// there is no way to "unset" updateUrl, you can only overwrite it. // there is no way to "unset" updateUrl, you can only overwrite it.
checker.disabled = true; checker.disabled = true;
} else if (updateUrl.protocol !== 'file:') { } else if (updateUrl.protocol !== 'file:' || hasFileAccess) {
checker.checked = true; checker.checked = true;
style.updateUrl = updateUrl.href; style.updateUrl = updateUrl.href;
} }
@ -129,7 +135,7 @@ setTimeout(() => !cm && showSpinner($('#header')), 200);
$('.set-update-url p').textContent = updateUrl.href.length < 300 ? updateUrl.href : $('.set-update-url p').textContent = updateUrl.href.length < 300 ? updateUrl.href :
updateUrl.href.slice(0, 300) + '...'; updateUrl.href.slice(0, 300) + '...';
if (initialUrl.startsWith('file:')) { if (URLS.isLocalhost(initialUrl)) {
$('.live-reload input').onchange = liveReload.onToggled; $('.live-reload input').onchange = liveReload.onToggled;
} else { } else {
$('.live-reload').remove(); $('.live-reload').remove();

View File

@ -25,7 +25,10 @@ const preinit = (() => {
function DirectDownloader() { function DirectDownloader() {
let oldCode = null; let oldCode = null;
return async () => { 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) { if (oldCode !== code) {
oldCode = code; oldCode = code;
return code; return code;

View File

@ -115,6 +115,8 @@ const URLS = {
url.startsWith(URLS.ownOrigin) || url.startsWith(URLS.ownOrigin) ||
!URLS.chromeProtectsNTP && url.startsWith('chrome://newtab/') !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; const RX_META = /\/\*!?\s*==userstyle==[\s\S]*?==\/userstyle==\s*\*\//i;