Rewrite usercss installation page

This commit is contained in:
eight 2017-09-01 18:23:50 +08:00
parent 3c40b52f96
commit 4dec09708c
2 changed files with 145 additions and 19 deletions

View File

@ -0,0 +1,55 @@
body {
margin: 0;
font: 12px arial, sans-serif;
}
* {
box-sizing: border-box;
}
.container {
display: flex;
min-height: 100vh;
align-items: stretch;
}
.header {
width: 280px;
padding: 15px;
border-right: 1px dashed #aaa;
box-shadow: 0 0 50px -18px black;
}
.header h1:first-child {
margin-top: 0;
}
.meta {
font-size: 1.4em;
}
.warning {
padding: 3px 6px;
border: 1px dashed black;
border-color: #ef6969;
background: #ffe2e2;
}
.header .warning {
margin: 3px 0;
}
.actions {
margin: 15px 0;
}
.actions > * {
display: inline-block;
}
.code {
padding: 2em;
font-family: monospace;
white-space: pre-wrap;
}

View File

@ -1,5 +1,9 @@
/* global usercss */
'use strict'; 'use strict';
let pendingResource;
function fetchText(url) { function fetchText(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// you can't use fetch in Chrome under 'file:' protocol // you can't use fetch in Chrome under 'file:' protocol
@ -18,7 +22,16 @@ function install(style) {
url: location.href, url: location.href,
updateUrl: location.href updateUrl: location.href
}); });
return communicate(request); return communicate(request)
.then(() => {
$$('.meta-version + .warning')
.forEach(el => el.remove());
$('button.install').textContent = 'Installed';
$('button.install').disabled = true;
})
.catch(err => {
alert(chrome.i18n.getMessage('styleInstallFailed', String(err)));
});
} }
function communicate(request) { function communicate(request) {
@ -33,26 +46,85 @@ function communicate(request) {
}); });
} }
function initUsercssInstall() { function initInstallPage({style, dup}) {
fetchText(location.href).then(source => pendingResource.then(() => {
communicate({ const versionTest = dup && usercss.semverTest(style.version, dup.version);
method: 'filterUsercss', document.body.innerHTML = '';
source: source, // FIXME: i18n
checkDup: true document.body.appendChild(tHTML(`
}) <div class="container">
).then(({style, dup}) => { <div class="header">
if (dup) { <h1>Install Usercss</h1>
if (confirm(chrome.i18n.getMessage('styleInstallOverwrite', [style.name, dup.version, style.version]))) { <h2>Name</h2>
return install(style); <span class="meta meta-name">${style.name}</span>
} <h2>Version</h2>
} else if (confirm(chrome.i18n.getMessage('styleInstall', [style.name]))) { <span class="meta meta-version">${style.version}</span>
return install(style); <div class="actions">
<button class="install">${!dup ? 'Install' : versionTest > 0 ? 'Update' : 'Reinstall'}</button>
</div>
</div>
<div class="code"></div>
</div>
`));
if (versionTest < 0) {
// FIXME: i18n
$('.meta-version').after(tHTML(`
<div class="warning">
The version is older then installed style.
</div>
`));
} }
}).catch(err => { $('.code').textContent = style.source;
alert(chrome.i18n.getMessage('styleInstallFailed', String(err))); $('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);
}
};
}); });
} }
function initErrorPage(err, source) {
pendingResource.then(() => {
document.body.innerHTML = '';
// FIXME: i18n
document.body.appendChild(tHTML(`
<div class="warning">
Stylus failed to parse usercss: ${err}
</div>
<div class="code"></div>
`));
$('.code').textContent = source;
});
}
function initUsercssInstall() {
let source;
pendingResource = communicate({
method: 'injectResource',
resources: [
'/js/dom.js',
'/js/localization.js',
'/js/usercss.js',
'/content/install-user-css.css'
]
});
fetchText(location.href)
.then(_source => {
source = _source;
return communicate({
method: 'filterUsercss',
source,
checkDup: true
});
})
.then(initInstallPage)
.catch(err => initErrorPage(err, source));
}
function isUsercss() { function isUsercss() {
if (!/\.user\.(css|styl|less|scss|sass)$/i.test(location.pathname)) { if (!/\.user\.(css|styl|less|scss|sass)$/i.test(location.pathname)) {
return false; return false;
@ -67,6 +139,5 @@ function isUsercss() {
} }
if (isUsercss()) { if (isUsercss()) {
// It seems that we need to wait some time to redraw the page. initUsercssInstall();
setTimeout(initUsercssInstall, 500);
} }