2017-09-11 18:32:27 +00:00
|
|
|
/* global semverCompare escapeHtml */
|
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',
|
|
|
|
reason: 'install',
|
|
|
|
updateUrl: location.href
|
|
|
|
});
|
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-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) => {
|
|
|
|
chrome.runtime.sendMessage(request, result => {
|
|
|
|
if (result.status === 'error') {
|
|
|
|
reject(result.error);
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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(() => {
|
|
|
|
const versionTest = dup && semverCompare(style.version, dup.version);
|
2017-09-01 10:23:50 +00:00
|
|
|
document.body.innerHTML = '';
|
|
|
|
document.body.appendChild(tHTML(`
|
|
|
|
<div class="container">
|
|
|
|
<div class="header">
|
2017-09-11 18:32:27 +00:00
|
|
|
<h1>
|
|
|
|
${escapeHtml(style.name)}
|
|
|
|
<small class="meta-version">v${escapeHtml(style.version)}</small>
|
|
|
|
</h1>
|
|
|
|
<p>${escapeHtml(style.description)}</p>
|
|
|
|
<h3 i18n-text="author"></h3>
|
|
|
|
${escapeHtml(style.author)}
|
|
|
|
<h3 i18n-text="license"></h3>
|
|
|
|
${escapeHtml(style.license)}
|
|
|
|
<h3 i18n-text="appliesLabel"></h3>
|
2017-09-05 02:32:30 +00:00
|
|
|
<ul>
|
2017-09-11 18:32:27 +00:00
|
|
|
${getAppliesTo(style).map(s => `<li>${escapeHtml(s)}</li>`)}
|
2017-09-05 02:32:30 +00:00
|
|
|
</ul>
|
2017-09-01 10:23:50 +00:00
|
|
|
<div class="actions">
|
2017-09-11 18:32:27 +00:00
|
|
|
<button class="install">${t(
|
|
|
|
!dup ? 'installButton' : versionTest > 0 ?
|
|
|
|
'installButtonUpdate' : 'installButtonReinstall'
|
|
|
|
)}</button>
|
2017-09-01 10:23:50 +00:00
|
|
|
</div>
|
2017-09-05 02:32:30 +00:00
|
|
|
<div class="external">
|
2017-09-11 19:56:05 +00:00
|
|
|
<a href="${encodeURI(style.url)}" target="_blank" i18n-text="externalHomepage" rel="noopener"></a>
|
|
|
|
<a href="${encodeURI(style.supportURL)}" target="_blank" i18n-text="externalSupport" rel="noopener"></a>
|
2017-09-05 02:32:30 +00:00
|
|
|
</div>
|
2017-09-01 10:23:50 +00:00
|
|
|
</div>
|
2017-09-10 14:05:44 +00:00
|
|
|
<div class="main">
|
|
|
|
<div class="code"></div>
|
|
|
|
</div>
|
2017-09-01 10:23:50 +00:00
|
|
|
</div>
|
|
|
|
`));
|
|
|
|
if (versionTest < 0) {
|
2017-09-11 20:10:20 +00:00
|
|
|
$('.actions').parentNode.insertBefore(tHTML(`
|
2017-09-11 18:32:27 +00:00
|
|
|
<div class="warning" i18n-text="versionInvalidOlder"></div>
|
2017-09-11 20:10:20 +00:00
|
|
|
`), $('.actions'));
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
2017-09-01 10:23:50 +00:00
|
|
|
$('.code').textContent = style.source;
|
|
|
|
$('button.install').onclick = () => {
|
|
|
|
if (dup) {
|
|
|
|
if (confirm(chrome.i18n.getMessage('styleInstallOverwrite', [style.name, dup.version, style.version]))) {
|
|
|
|
install(style);
|
|
|
|
}
|
|
|
|
} else if (confirm(chrome.i18n.getMessage('styleInstall', [style.name]))) {
|
|
|
|
install(style);
|
|
|
|
}
|
|
|
|
};
|
2017-09-10 14:05:44 +00:00
|
|
|
|
|
|
|
if (location.protocol === 'file:') {
|
|
|
|
initLiveReload(sourceLoader);
|
|
|
|
}
|
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,
|
|
|
|
source: source
|
|
|
|
}).then(() => {
|
|
|
|
$$('.main .warning').forEach(e => e.remove());
|
|
|
|
}).catch(err => {
|
|
|
|
const oldWarning = $('.main .warning');
|
|
|
|
const warning = tHTML(`
|
2017-09-11 18:32:27 +00:00
|
|
|
<div class="warning" i18n-text="parseUsercssError">
|
|
|
|
<pre>${escapeHtml(err)}</pre>
|
2017-09-10 14:05:44 +00:00
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
window.addEventListener('installed', ({detail: {style}}) => {
|
|
|
|
installed = style;
|
|
|
|
if ($('.live-reload-checkbox').checked) {
|
|
|
|
watcher.start();
|
|
|
|
}
|
|
|
|
});
|
2017-09-11 20:10:20 +00:00
|
|
|
$('.actions').appendChild(tHTML(`
|
2017-09-10 14:05:44 +00:00
|
|
|
<label class="live-reload">
|
|
|
|
<input type="checkbox" class="live-reload-checkbox">
|
2017-09-11 18:32:27 +00:00
|
|
|
<span i18n-text="liveReloadLabel"></span>
|
2017-09-10 14:05:44 +00:00
|
|
|
</label>
|
|
|
|
`));
|
|
|
|
$('.live-reload-checkbox').onchange = e => {
|
|
|
|
if (!installed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e.target.checked) {
|
|
|
|
watcher.start();
|
|
|
|
} else {
|
|
|
|
watcher.stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-01 10:23:50 +00:00
|
|
|
document.body.innerHTML = '';
|
|
|
|
document.body.appendChild(tHTML(`
|
2017-09-11 18:32:27 +00:00
|
|
|
<div class="warning" i18n-text="parseUsercssError">
|
|
|
|
<pre>${escapeHtml(err)}</pre>
|
2017-09-01 10:23:50 +00:00
|
|
|
</div>
|
|
|
|
<div class="code"></div>
|
|
|
|
`));
|
|
|
|
$('.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-01 10:23:50 +00:00
|
|
|
method: 'filterUsercss',
|
2017-09-10 14:05:44 +00:00
|
|
|
source: 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 (!/\.user\.(css|styl|less|scss|sass)$/i.test(location.pathname)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|