2017-08-05 16:49:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-12 10:58:39 +00:00
|
|
|
function runtimeSend(request) {
|
2017-08-05 16:49:25 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-18 03:34:12 +00:00
|
|
|
chrome.runtime.sendMessage(
|
|
|
|
request,
|
|
|
|
({status, result}) => (status === 'error' ? reject : resolve)(result)
|
|
|
|
);
|
2017-08-05 16:49:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-24 03:39:04 +00:00
|
|
|
const pendingSource = createSourceLoader().load();
|
|
|
|
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-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
|
|
|
}
|