2017-09-13 09:34:27 +00:00
|
|
|
/* global semverCompare makeLink */
|
2017-09-01 10:23:50 +00:00
|
|
|
|
2017-08-05 16:49:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-01 10:23:50 +00:00
|
|
|
let pendingResource;
|
|
|
|
|
2017-08-05 16:49:25 +00:00
|
|
|
function install(style) {
|
|
|
|
const request = Object.assign(style, {
|
|
|
|
method: 'saveUsercss',
|
2017-09-19 00:23:47 +00:00
|
|
|
reason: 'update'
|
2017-08-05 16:49:25 +00:00
|
|
|
});
|
2017-09-12 10:58:39 +00:00
|
|
|
return runtimeSend(request)
|
2017-09-10 14:05:44 +00:00
|
|
|
.then(result => {
|
2017-09-05 02:32:30 +00:00
|
|
|
$$('.warning')
|
2017-09-01 10:23:50 +00:00
|
|
|
.forEach(el => el.remove());
|
|
|
|
$('button.install').textContent = 'Installed';
|
|
|
|
$('button.install').disabled = true;
|
2017-09-14 17:30:26 +00:00
|
|
|
$('button.install').classList.add('installed');
|
2017-09-19 00:23:47 +00:00
|
|
|
$('.set-update-url').disabled = true;
|
|
|
|
$('.set-update-url-label').title = result.updateUrl ?
|
|
|
|
t('installUpdateFrom', result.updateUrl) : '';
|
2017-09-10 14:05:44 +00:00
|
|
|
window.dispatchEvent(new CustomEvent('installed', {detail: result}));
|
2017-09-01 10:23:50 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
alert(chrome.i18n.getMessage('styleInstallFailed', String(err)));
|
|
|
|
});
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
|
|
|
|
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-05 02:32:30 +00:00
|
|
|
function getAppliesTo(style) {
|
|
|
|
function *_gen() {
|
|
|
|
for (const section of style.sections) {
|
|
|
|
for (const type of ['urls', 'urlPrefixes', 'domains', 'regexps']) {
|
|
|
|
if (section[type]) {
|
|
|
|
yield *section[type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const result = [..._gen()];
|
|
|
|
if (!result.length) {
|
2017-09-11 18:32:27 +00:00
|
|
|
result.push(chrome.i18n.getMessage('appliesToEverything'));
|
2017-09-05 02:32:30 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-10 14:05:44 +00:00
|
|
|
function initInstallPage({style, dup}, sourceLoader) {
|
2017-09-05 19:08:03 +00:00
|
|
|
return pendingResource.then(() => {
|
2017-09-16 01:24:50 +00:00
|
|
|
const data = style.usercssData;
|
|
|
|
const dupData = dup && dup.usercssData;
|
|
|
|
const versionTest = dup && semverCompare(data.version, dupData.version);
|
2017-09-12 11:28:20 +00:00
|
|
|
document.body.textContent = '';
|
|
|
|
document.body.appendChild(buildPage());
|
|
|
|
|
2017-09-01 10:23:50 +00:00
|
|
|
if (versionTest < 0) {
|
2017-09-12 11:28:20 +00:00
|
|
|
$('.actions').parentNode.insertBefore(
|
2017-09-12 15:23:56 +00:00
|
|
|
$element({className: 'warning', textContent: t('versionInvalidOlder')}),
|
2017-09-12 11:28:20 +00:00
|
|
|
$('.actions')
|
|
|
|
);
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
2017-09-16 01:24:50 +00:00
|
|
|
$('.code').textContent = style.sourceCode;
|
2017-09-01 10:23:50 +00:00
|
|
|
$('button.install').onclick = () => {
|
|
|
|
if (dup) {
|
2017-09-16 01:24:50 +00:00
|
|
|
if (confirm(chrome.i18n.getMessage('styleInstallOverwrite', [
|
|
|
|
data.name, dupData.version, data.version
|
|
|
|
]))) {
|
2017-09-01 10:23:50 +00:00
|
|
|
install(style);
|
|
|
|
}
|
2017-09-16 01:24:50 +00:00
|
|
|
} else if (confirm(chrome.i18n.getMessage('styleInstall', [data.name]))) {
|
2017-09-01 10:23:50 +00:00
|
|
|
install(style);
|
|
|
|
}
|
|
|
|
};
|
2017-09-19 00:23:47 +00:00
|
|
|
if (dup && dup.updateUrl === location.href) {
|
|
|
|
$('.set-update-url').checked = true;
|
|
|
|
// there is no way to "unset" updateUrl, you can only overwrite it.
|
|
|
|
$('.set-update-url').disabled = true;
|
|
|
|
} else if (!dup && location.protocol !== 'file:') {
|
|
|
|
$('.set-update-url').checked = true;
|
|
|
|
style.updateUrl = location.href;
|
|
|
|
}
|
|
|
|
$('.set-update-url').onchange = e => {
|
|
|
|
if (e.target.checked) {
|
|
|
|
style.updateUrl = location.href;
|
|
|
|
} else {
|
|
|
|
delete style.updateUrl;
|
|
|
|
}
|
|
|
|
};
|
2017-09-10 14:05:44 +00:00
|
|
|
|
|
|
|
if (location.protocol === 'file:') {
|
|
|
|
initLiveReload(sourceLoader);
|
|
|
|
}
|
2017-09-12 11:28:20 +00:00
|
|
|
|
|
|
|
function buildPage() {
|
|
|
|
return $element({className: 'container', appendChild: [
|
|
|
|
$element({className: 'header', appendChild: [
|
2017-09-14 03:06:32 +00:00
|
|
|
$element({className: 'actions', appendChild: [
|
2017-09-19 00:23:47 +00:00
|
|
|
$element({tag: 'button', className: 'install', textContent: installButtonLabel()}),
|
|
|
|
$element({
|
|
|
|
tag: 'label',
|
|
|
|
title: dup && dup.updateUrl && t('installUpdateFrom', dup.updateUrl) || '',
|
|
|
|
className: 'set-update-url-label',
|
|
|
|
appendChild: [
|
|
|
|
$element({
|
|
|
|
tag: 'input',
|
|
|
|
type: 'checkbox',
|
|
|
|
className: 'set-update-url'
|
|
|
|
}),
|
|
|
|
$element({tag: 'span', textContent: t('installUpdateFromLabel')})
|
|
|
|
]
|
|
|
|
})
|
2017-09-14 03:06:32 +00:00
|
|
|
]}),
|
2017-09-12 11:28:20 +00:00
|
|
|
$element({tag: 'h1', appendChild: [
|
2017-09-16 01:24:50 +00:00
|
|
|
data.name,
|
|
|
|
$element({tag: 'small', className: 'meta-version', textContent: data.version})
|
2017-09-12 11:28:20 +00:00
|
|
|
]}),
|
2017-09-16 01:24:50 +00:00
|
|
|
$element({tag: 'p', textContent: data.description}),
|
|
|
|
data.author && $element({tag: 'h3', textContent: t('author')}),
|
|
|
|
data.author,
|
|
|
|
data.license && $element({tag: 'h3', textContent: t('license')}),
|
|
|
|
data.license,
|
2017-09-12 15:23:56 +00:00
|
|
|
$element({tag: 'h3', textContent: t('appliesLabel')}),
|
2017-09-12 11:28:20 +00:00
|
|
|
$element({tag: 'ul', appendChild: getAppliesTo(style).map(
|
2017-09-12 15:23:56 +00:00
|
|
|
pattern => $element({tag: 'li', textContent: pattern})
|
2017-09-12 11:28:20 +00:00
|
|
|
)}),
|
2017-09-16 01:24:50 +00:00
|
|
|
externalLink(),
|
2017-09-12 11:28:20 +00:00
|
|
|
]}),
|
|
|
|
$element({className: 'main', appendChild: [
|
|
|
|
$element({className: 'code'})
|
|
|
|
]})
|
|
|
|
]});
|
|
|
|
}
|
|
|
|
|
2017-09-16 01:24:50 +00:00
|
|
|
function externalLink() {
|
2017-09-14 03:06:32 +00:00
|
|
|
const urls = [];
|
2017-09-16 01:24:50 +00:00
|
|
|
if (data.homepageURL) {
|
|
|
|
urls.push([data.homepageURL, t('externalHomepage')]);
|
2017-09-14 03:06:32 +00:00
|
|
|
}
|
2017-09-16 01:24:50 +00:00
|
|
|
if (data.supportURL) {
|
|
|
|
urls.push([data.supportURL, t('externalSupport')]);
|
2017-09-14 03:06:32 +00:00
|
|
|
}
|
|
|
|
if (urls.length) {
|
|
|
|
return $element({appendChild: [
|
|
|
|
$element({tag: 'h3', textContent: t('externalLink')}),
|
|
|
|
$element({tag: 'ul', appendChild: urls.map(args =>
|
|
|
|
$element({tag: 'li', appendChild: makeLink(...args)})
|
|
|
|
)})
|
|
|
|
]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:28:20 +00:00
|
|
|
function installButtonLabel() {
|
|
|
|
return t(!dup ? 'installButton' :
|
|
|
|
versionTest > 0 ? 'installButtonUpdate' : 'installButtonReinstall');
|
|
|
|
}
|
2017-08-05 16:49:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-10 14:05:44 +00:00
|
|
|
function initLiveReload(sourceLoader) {
|
|
|
|
let installed;
|
|
|
|
const watcher = sourceLoader.watch(source => {
|
|
|
|
$('.code').textContent = source;
|
2017-09-12 10:58:39 +00:00
|
|
|
return runtimeSend({
|
2017-09-10 14:05:44 +00:00
|
|
|
method: 'saveUsercss',
|
|
|
|
id: installed.id,
|
2017-09-16 01:24:50 +00:00
|
|
|
sourceCode: source
|
2017-09-10 14:05:44 +00:00
|
|
|
}).then(() => {
|
|
|
|
$$('.main .warning').forEach(e => e.remove());
|
|
|
|
}).catch(err => {
|
|
|
|
const oldWarning = $('.main .warning');
|
2017-09-12 11:28:20 +00:00
|
|
|
const warning = buildWarning(err);
|
2017-09-10 14:05:44 +00:00
|
|
|
if (oldWarning) {
|
|
|
|
oldWarning.replaceWith(warning);
|
|
|
|
} else {
|
2017-09-11 20:10:20 +00:00
|
|
|
$('.main').insertBefore(warning, $('.main').childNodes[0]);
|
2017-09-10 14:05:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-09-18 03:34:12 +00:00
|
|
|
window.addEventListener('installed', ({detail: style}) => {
|
2017-09-10 14:05:44 +00:00
|
|
|
installed = style;
|
|
|
|
if ($('.live-reload-checkbox').checked) {
|
|
|
|
watcher.start();
|
|
|
|
}
|
|
|
|
});
|
2017-09-12 11:28:20 +00:00
|
|
|
$('.actions').appendChild($element({tag: 'label', className: 'live-reload', appendChild: [
|
|
|
|
$element({tag: 'input', type: 'checkbox', className: 'live-reload-checkbox'}),
|
2017-09-12 15:23:56 +00:00
|
|
|
$element({tag: 'span', textContent: t('liveReloadLabel')})
|
2017-09-12 11:28:20 +00:00
|
|
|
]}));
|
2017-09-10 14:05:44 +00:00
|
|
|
$('.live-reload-checkbox').onchange = e => {
|
|
|
|
if (!installed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e.target.checked) {
|
|
|
|
watcher.start();
|
|
|
|
} else {
|
|
|
|
watcher.stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:28:20 +00:00
|
|
|
function buildWarning(err) {
|
|
|
|
return $element({className: 'warning', appendChild: [
|
|
|
|
t('parseUsercssError'),
|
2017-09-12 15:23:56 +00:00
|
|
|
$element({tag: 'pre', textContent: String(err)})
|
2017-09-12 15:19:16 +00:00
|
|
|
]});
|
2017-09-12 11:28:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 10:23:50 +00:00
|
|
|
function initErrorPage(err, source) {
|
2017-09-05 19:08:03 +00:00
|
|
|
return pendingResource.then(() => {
|
2017-09-12 17:39:34 +00:00
|
|
|
document.body.textContent = '';
|
2017-09-12 11:28:20 +00:00
|
|
|
[
|
|
|
|
buildWarning(err),
|
|
|
|
$element({className: 'code'})
|
|
|
|
].forEach(e => document.body.appendChild(e));
|
2017-09-01 10:23:50 +00:00
|
|
|
$('.code').textContent = source;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-12 10:58:39 +00:00
|
|
|
pendingResource = runtimeSend({
|
2017-09-12 10:28:16 +00:00
|
|
|
method: 'injectContent',
|
2017-09-12 10:34:48 +00:00
|
|
|
files: [
|
2017-09-01 10:23:50 +00:00
|
|
|
'/js/dom.js',
|
|
|
|
'/js/localization.js',
|
|
|
|
'/js/usercss.js',
|
2017-09-05 19:08:03 +00:00
|
|
|
'/vendor/node-semver/semver.js',
|
2017-09-01 10:23:50 +00:00
|
|
|
'/content/install-user-css.css'
|
|
|
|
]
|
|
|
|
});
|
2017-09-10 14:05:44 +00:00
|
|
|
|
|
|
|
const sourceLoader = createSourceLoader();
|
|
|
|
sourceLoader.load()
|
|
|
|
.then(() =>
|
2017-09-12 10:58:39 +00:00
|
|
|
runtimeSend({
|
2017-09-18 03:34:12 +00:00
|
|
|
method: 'buildUsercss',
|
2017-09-16 01:24:50 +00:00
|
|
|
sourceCode: sourceLoader.source(),
|
2017-09-01 10:23:50 +00:00
|
|
|
checkDup: true
|
2017-09-10 14:05:44 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(result => initInstallPage(result, sourceLoader))
|
|
|
|
.catch(err => initErrorPage(err, sourceLoader.source()));
|
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
|
|
|
}
|