2017-08-05 16:49:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
// preventing reregistration if reinjected by tabs.executeScript for whatever reason, just in case
|
2021-01-01 14:27:58 +00:00
|
|
|
if (typeof window.oldCode !== 'string') {
|
|
|
|
window.oldCode = (document.querySelector('body > pre') || document.body).textContent;
|
2020-02-23 15:43:26 +00:00
|
|
|
chrome.runtime.onConnect.addListener(port => {
|
|
|
|
if (port.name !== 'downloadSelf') return;
|
2021-01-01 14:27:58 +00:00
|
|
|
port.onMessage.addListener(async ({id, force}) => {
|
|
|
|
const msg = {id};
|
|
|
|
try {
|
|
|
|
const code = await (await fetch(location.href, {mode: 'same-origin'})).text();
|
|
|
|
if (code !== window.oldCode || force) {
|
|
|
|
msg.code = window.oldCode = code;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
msg.error = error.message || `${error}`;
|
|
|
|
}
|
|
|
|
port.postMessage(msg);
|
2017-09-10 14:05:44 +00:00
|
|
|
});
|
2020-10-11 11:37:55 +00:00
|
|
|
// FF keeps content scripts connected on navigation https://github.com/openstyles/stylus/issues/864
|
|
|
|
addEventListener('pagehide', () => port.disconnect(), {once: true});
|
2020-02-23 15:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
2017-08-05 16:49:25 +00:00
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
// passing the result to tabs.executeScript
|
2021-01-01 14:27:58 +00:00
|
|
|
window.oldCode; // eslint-disable-line no-unused-expressions
|