2017-11-01 00:17:12 +00:00
|
|
|
/* global runtimeSend */
|
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() {
|
|
|
|
return fetchText(location.href)
|
|
|
|
.then(_source => {
|
|
|
|
source = _source;
|
|
|
|
return source;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
.then(_source => {
|
|
|
|
if (source !== _source) {
|
|
|
|
source = _source;
|
|
|
|
return cb(source);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
.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 => {
|
|
|
|
// FIXME: is this the correct way to reject a connection?
|
|
|
|
// https://developer.chrome.com/extensions/messaging#connect
|
|
|
|
console.assert(port.name === 'usercss-install');
|
|
|
|
|
|
|
|
port.onMessage.addListener(msg => {
|
|
|
|
switch (msg.method) {
|
|
|
|
case 'getSourceCode':
|
|
|
|
pendingSource.then(sourceCode =>
|
|
|
|
port.postMessage({method: msg.method + 'Response', sourceCode})
|
|
|
|
).catch(err =>
|
|
|
|
port.postMessage({method: msg.method + 'Response', error: err.message || String(err)})
|
|
|
|
);
|
|
|
|
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 {
|
|
|
|
runtimeSend({method: 'closeTab'});
|
|
|
|
}
|
|
|
|
break;
|
2017-09-24 03:39:04 +00:00
|
|
|
}
|
|
|
|
});
|
2017-09-01 10:23:50 +00:00
|
|
|
});
|
2017-09-24 03:39:04 +00:00
|
|
|
return runtimeSend({
|
|
|
|
method: 'openUsercssInstallPage',
|
|
|
|
updateUrl: location.href
|
|
|
|
}).catch(alert);
|
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
|
|
|
}
|