Skip to content

Commit

Permalink
reject installation of empty styles (#2),
Browse files Browse the repository at this point in the history
don't use pre-parsed styles from userstyles.org (#2),
set tab title as style name if style name is a number
  • Loading branch information
NiklasGollenstede committed Nov 5, 2017
1 parent 8f0e8b7 commit c9e03d6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ In addition to that, you can load <b>local styles</b> and apply styles to the <b

<b>Installing styles</b>

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.

Expand Down
2 changes: 1 addition & 1 deletion background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion background/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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__;
}

Expand Down
10 changes: 6 additions & 4 deletions background/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const urlList = options.remote.children.urls.values; const styles = new Map/*<id
} catch (error) { reportError(`Failed to restore remote style`, url, error); } })));

if (global.__startupSyncPoint__) { global.__startupSyncPoint__(); } // sync with ../local/
else { (await Promise.race([ new Promise(done => (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); } });
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions background/remote/map-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
Expand Down
2 changes: 1 addition & 1 deletion common/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, },
},
};

Expand Down
23 changes: 16 additions & 7 deletions views/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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); }
},
}),
]);
Expand Down

0 comments on commit c9e03d6

Please sign in to comment.