2017-08-05 16:49:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-10 14:05:44 +00:00
|
|
|
function createSourceLoader() {
|
2017-09-01 10:23:50 +00:00
|
|
|
let source;
|
2017-09-10 14:05:44 +00:00
|
|
|
|
|
|
|
function fetchText(url) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// you can't use fetch in Chrome under 'file:' protocol
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', url);
|
|
|
|
xhr.addEventListener('load', () => resolve(xhr.responseText));
|
|
|
|
xhr.addEventListener('error', () => reject(xhr));
|
|
|
|
xhr.send();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function load() {
|
2017-11-08 21:52:59 +00:00
|
|
|
return fetchText(location.href).then(newSource => {
|
|
|
|
source = newSource;
|
|
|
|
return source;
|
|
|
|
});
|
2017-09-10 14:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function watch(cb) {
|
|
|
|
let timer;
|
|
|
|
const DELAY = 1000;
|
|
|
|
|
|
|
|
function start() {
|
|
|
|
if (timer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timer = setTimeout(check, DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop() {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function check() {
|
|
|
|
fetchText(location.href)
|
2017-11-01 00:37:18 +00:00
|
|
|
.then(newSource => {
|
|
|
|
if (source !== newSource) {
|
|
|
|
source = newSource;
|
2017-09-10 14:05:44 +00:00
|
|
|
return cb(source);
|
|
|
|
}
|
|
|
|
})
|
2017-11-09 04:36:16 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.log(t('liveReloadError', error));
|
|
|
|
})
|
2017-09-10 14:05:44 +00:00
|
|
|
.then(() => {
|
|
|
|
timer = setTimeout(check, DELAY);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {start, stop};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {load, watch, source: () => source};
|
|
|
|
}
|
|
|
|
|
|
|
|
function initUsercssInstall() {
|
2017-09-25 12:01:50 +00:00
|
|
|
const sourceLoader = createSourceLoader();
|
|
|
|
const pendingSource = sourceLoader.load();
|
|
|
|
let watcher;
|
|
|
|
|
2017-09-24 03:39:04 +00:00
|
|
|
chrome.runtime.onConnect.addListener(port => {
|
|
|
|
port.onMessage.addListener(msg => {
|
|
|
|
switch (msg.method) {
|
|
|
|
case 'getSourceCode':
|
2017-11-08 22:00:05 +00:00
|
|
|
pendingSource
|
|
|
|
.then(sourceCode => port.postMessage({method: msg.method + 'Response', sourceCode}))
|
|
|
|
.catch(err => port.postMessage({method: msg.method + 'Response', error: err.message || String(err)}));
|
2017-09-24 03:39:04 +00:00
|
|
|
break;
|
2017-09-25 12:01:50 +00:00
|
|
|
|
|
|
|
case 'liveReloadStart':
|
|
|
|
if (!watcher) {
|
|
|
|
watcher = sourceLoader.watch(sourceCode => {
|
|
|
|
port.postMessage({method: 'sourceCodeChanged', sourceCode});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
watcher.start();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'liveReloadStop':
|
|
|
|
watcher.stop();
|
|
|
|
break;
|
2017-10-06 08:29:06 +00:00
|
|
|
|
|
|
|
case 'closeTab':
|
|
|
|
if (history.length > 1) {
|
|
|
|
history.back();
|
|
|
|
} else {
|
2017-11-24 15:26:20 +00:00
|
|
|
chrome.runtime.sendMessage({method: 'closeTab'});
|
2017-10-06 08:29:06 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-09-24 03:39:04 +00:00
|
|
|
}
|
|
|
|
});
|
2017-09-01 10:23:50 +00:00
|
|
|
});
|
2017-11-24 15:26:20 +00:00
|
|
|
chrome.runtime.sendMessage({
|
2018-01-01 17:02:49 +00:00
|
|
|
method: 'installUsercss',
|
2017-11-24 16:33:50 +00:00
|
|
|
url: location.href,
|
2017-11-24 15:26:20 +00:00
|
|
|
}, r => r && r.__ERROR__ && alert(r.__ERROR__));
|
2017-09-01 10:23:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 16:49:25 +00:00
|
|
|
function isUsercss() {
|
|
|
|
if (!/text\/(css|plain)/.test(document.contentType)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!/==userstyle==/i.test(document.body.textContent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUsercss()) {
|
2017-09-01 10:23:50 +00:00
|
|
|
initUsercssInstall();
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|