process current contents when live-reload is enabled

This commit is contained in:
tophf 2020-10-09 14:37:29 +03:00
parent 9994811819
commit 707cd6576f
2 changed files with 12 additions and 7 deletions

View File

@ -5,10 +5,10 @@ if (typeof self.oldCode !== 'string') {
self.oldCode = (document.querySelector('body > pre') || document.body).textContent; self.oldCode = (document.querySelector('body > pre') || document.body).textContent;
chrome.runtime.onConnect.addListener(port => { chrome.runtime.onConnect.addListener(port => {
if (port.name !== 'downloadSelf') return; if (port.name !== 'downloadSelf') return;
port.onMessage.addListener(({id, timer}) => { port.onMessage.addListener(({id, force}) => {
fetch(location.href, {mode: 'same-origin'}) fetch(location.href, {mode: 'same-origin'})
.then(r => r.text()) .then(r => r.text())
.then(code => ({id, code: timer && code === self.oldCode ? null : code})) .then(code => ({id, code: force || code !== self.oldCode ? code : null}))
.catch(error => ({id, error: error.message || `${error}`})) .catch(error => ({id, error: error.message || `${error}`}))
.then(msg => { .then(msg => {
port.postMessage(msg); port.postMessage(msg);

View File

@ -331,7 +331,11 @@
onToggled(e) { onToggled(e) {
if (e) isEnabled = e.target.checked; if (e) isEnabled = e.target.checked;
if (installed || installedDup) { if (installed || installedDup) {
(isEnabled ? start : stop)(); if (isEnabled) {
check({force: true});
} else {
stop();
}
$('.install').disabled = isEnabled; $('.install').disabled = isEnabled;
Object.assign($('#live-reload-install-hint'), { Object.assign($('#live-reload-install-hint'), {
hidden: !isEnabled, hidden: !isEnabled,
@ -340,8 +344,8 @@
} }
}, },
}; };
function check() { function check(opts) {
getData() getData(opts)
.then(update, logError) .then(update, logError)
.then(() => { .then(() => {
timer = 0; timer = 0;
@ -398,10 +402,11 @@
.then(tab => tab.url === initialUrl && location.reload()) .then(tab => tab.url === initialUrl && location.reload())
.catch(closeCurrentTab); .catch(closeCurrentTab);
}); });
return ({timer = true} = {}) => new Promise((resolve, reject) => { return (opts = {}) => new Promise((resolve, reject) => {
const id = performance.now(); const id = performance.now();
resolvers.set(id, {resolve, reject}); resolvers.set(id, {resolve, reject});
port.postMessage({id, timer}); opts.id = id;
port.postMessage(opts);
}); });
} }
} }