diff --git a/README.md b/README.md index 7cf9e61..4b8269a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ In addition to that, you can load local styles and apply styles to the Installing styles -Styles can be installed from userstyles.org, github.com or any other website that offers appropriate styles for download. +Styles can be installed from userstyles.org, GitHub or any other website that offers appropriate styles for download. On userstyles.org, open the styles detail page, choose your settings if the style offers any, then click the reStyle icon in the browsers toolbar and "Add style" in the popup. On other pages, you need to open the .css file before clicking the reStyle icon. diff --git a/background/index.js b/background/index.js index 2dd6f7e..bc35a16 100644 --- a/background/index.js +++ b/background/index.js @@ -58,7 +58,7 @@ Object.assign(global, module.exports = { Browser: require('node_modules/web-ext-utils/browser/'), options, Parser, - background: global, + Native, ContentStyle, ChromeStyle, LocalStyle, diff --git a/background/local/index.js b/background/local/index.js index e6664b9..1a042bc 100644 --- a/background/local/index.js +++ b/background/local/index.js @@ -48,7 +48,7 @@ async function enable(init) { if (init) { // on initial enable, sync with ../remote/ if (global.__startupSyncPoint__) { global.__startupSyncPoint__(); } - else { (await Promise.race([ new Promise(done => (global.__startupSyncPoint__ = done)), require.async('../local/'), ])); } + else { (await new Promise((resolve, reject) => { global.__startupSyncPoint__ = resolve; require.async('../remote/').catch(reject); })); } delete global.__startupSyncPoint__; } diff --git a/background/remote/index.js b/background/remote/index.js index 7f4f066..a258156 100644 --- a/background/remote/index.js +++ b/background/remote/index.js @@ -39,7 +39,7 @@ const urlList = options.remote.children.urls.values; const styles = new Map/* (global.__startupSyncPoint__ = done)), require.async('../local/'), ])); } + else { (await new Promise((resolve, reject) => { global.__startupSyncPoint__ = resolve; require.async('../local/').catch(reject); })); } // enable all styles at once to allow later optimizations actions.forEach(action => { try { action(); } catch (error) { reportError(`Failed to restore remote style`, error); } }); @@ -58,14 +58,16 @@ async function add(/*url*/) { const style = (await new RemoteStyle(url, '')); styles.set(style.id, style); style.onChanged(onChanged); query && (style.options.query.value = query); - try { (await update(style, query)); } - catch (error) { + try { + (await update(style, query)); + if (!style.code) { throw new Error(`Can not install an empty style sheet`); } + } catch (error) { style.destroy(); throw error; } (await insertUrl(url)); - return style.id; + return style; } async function update(style, query) { diff --git a/background/remote/map-url.js b/background/remote/map-url.js index eb26a38..5a73590 100644 --- a/background/remote/map-url.js +++ b/background/remote/map-url.js @@ -2,13 +2,13 @@ async function mapUrl(url, tab) { switch (true) { case (/^https?:\/\/userstyles\.org\/styles\/\d+/).test(url): { - if (!tab) { return 'https://userstyles.org/styles/'+ (/\d+/).exec(url)[0] +'.css'; } + const id = (/\d+/).exec(url)[0]; + if (!tab) { return `https://userstyles.org/styles/${id}.css`; } let query; try { query = (await (await require.async('node_modules/web-ext-utils/loader/')) .runInFrame(tab.id, 0, readUserstylesOrgOptions) ); } catch (error) { console.error(error); } - if (!query) { return 'https://userstyles.org/styles/'+ (/\d+/).exec(url)[0] +'.css'; } - return 'https://userstyles.org/styles/chrome/'+ (/\d+/).exec(url)[0] +'.css?'+ query; + return `https://userstyles.org/styles/${id}.css` + (query ? '?'+ query : ''); } case (/^https:\/\/github.com\/[\w-]+\/[\w-]+\/blob\/master\/.*\.css/).test(url): { return url.replace('github.com', 'raw.githubusercontent.com').replace('/blob/master/', '/master/'); diff --git a/common/options.js b/common/options.js index 675f46b..52e3458 100644 --- a/common/options.js +++ b/common/options.js @@ -83,7 +83,7 @@ const model = { default: +isBeta, hidden: !isBeta, restrict: { type: 'number', from: 0, to: 2, }, - input: { type: 'integer', suffix: `set to > 0 to enable debugging`, }, + input: { type: 'integer', suffix: `set to > 0 to enable some diagnostic logging`, }, }, }; diff --git a/views/panel.js b/views/panel.js index f7970ad..8849e1b 100644 --- a/views/panel.js +++ b/views/panel.js @@ -48,17 +48,25 @@ const add = document.querySelector('#add'); add.addEventListener('click', event => { if (event.button) { return; } const url = input.value.trim(); add.disabled = true; - Remote.add(url).then( - () => { reportSuccess(`Style added`, `from "${ url }"`); input.value = ''; add.disabled = false; }, - error => { reportError(`Failed to add style from "${ url }"`, error); add.disabled = false; }, - ); + Remote.add(url).then( style => { + if (!style.name || (/^\d+$/).test(style)) { + style.options.name.value = tab.title.split(/-|–|—|\||::|·/)[0].trim(); + } + reportSuccess(`Style added`, `from "${ url }"`); + input.value = ''; add.disabled = false; + }, error => { + add.disabled = false; + reportError(`Failed to add style from "${ url }"`, error); + }, ); }); /// active styles list const list = document.querySelector('#styles'), url = new global.URL(tab.url); const styles = Array.from(Style, _=>_[1]).filter(style => ( - style.matches(url.href) || style.options.include.children.some(_=>_.values.current.some(domain => isSubDomain(domain, url.hostname))) + style.matches(url.href) || style.options.include.children.some( + _=>_.values.current.some(domain => isSubDomain(domain, url.hostname)) + ) )).sort((a, b) => a.url < b.url ? -1 : 1); styles.forEach(appendStyle); function appendStyle(style) { @@ -85,8 +93,9 @@ styles.forEach(appendStyle); function appendStyle(style) { value && value.split(/\s+/g).forEach(value => values.add(value)); (await include.values.replace(Array.from(values))); document.getElementById(style.id).remove(); - (style.matches(url.href) || style.options.include.children.some(_=>_.values.current.some(domain => isSubDomain(domain, url.hostname)))) - && appendStyle(style); + if (style.matches(url.href) || style.options.include.children.some( + _=>_.values.current.some(domain => isSubDomain(domain, url.hostname)) + )) { appendStyle(style); } }, }), ]);