From 4dec09708cf151ce7a2b3bc860c087fb69618748 Mon Sep 17 00:00:00 2001 From: eight Date: Fri, 1 Sep 2017 18:23:50 +0800 Subject: [PATCH] Rewrite usercss installation page --- content/install-user-css.css | 55 ++++++++++++++++++ content/install-user-css.js | 109 +++++++++++++++++++++++++++++------ 2 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 content/install-user-css.css diff --git a/content/install-user-css.css b/content/install-user-css.css new file mode 100644 index 00000000..5488964f --- /dev/null +++ b/content/install-user-css.css @@ -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; +} diff --git a/content/install-user-css.js b/content/install-user-css.js index a8bb4f09..9247a358 100644 --- a/content/install-user-css.js +++ b/content/install-user-css.js @@ -1,5 +1,9 @@ +/* global usercss */ + 'use strict'; +let pendingResource; + function fetchText(url) { return new Promise((resolve, reject) => { // you can't use fetch in Chrome under 'file:' protocol @@ -18,7 +22,16 @@ function install(style) { url: 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) { @@ -33,26 +46,85 @@ function communicate(request) { }); } -function initUsercssInstall() { - fetchText(location.href).then(source => - communicate({ - method: 'filterUsercss', - source: source, - checkDup: true - }) - ).then(({style, dup}) => { - if (dup) { - if (confirm(chrome.i18n.getMessage('styleInstallOverwrite', [style.name, dup.version, style.version]))) { - return install(style); - } - } else if (confirm(chrome.i18n.getMessage('styleInstall', [style.name]))) { - return install(style); +function initInstallPage({style, dup}) { + pendingResource.then(() => { + const versionTest = dup && usercss.semverTest(style.version, dup.version); + document.body.innerHTML = ''; + // FIXME: i18n + document.body.appendChild(tHTML(` +
+
+

Install Usercss

+

Name

+ ${style.name} +

Version

+ ${style.version} +
+ +
+
+
+
+ `)); + if (versionTest < 0) { + // FIXME: i18n + $('.meta-version').after(tHTML(` +
+ The version is older then installed style. +
+ `)); } - }).catch(err => { - alert(chrome.i18n.getMessage('styleInstallFailed', String(err))); + $('.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); + } + }; }); } +function initErrorPage(err, source) { + pendingResource.then(() => { + document.body.innerHTML = ''; + // FIXME: i18n + document.body.appendChild(tHTML(` +
+ Stylus failed to parse usercss: ${err} +
+
+ `)); + $('.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() { if (!/\.user\.(css|styl|less|scss|sass)$/i.test(location.pathname)) { return false; @@ -67,6 +139,5 @@ function isUsercss() { } if (isUsercss()) { - // It seems that we need to wait some time to redraw the page. - setTimeout(initUsercssInstall, 500); + initUsercssInstall(); }