diff --git a/background/usercss-install-helper.js b/background/usercss-install-helper.js index b0fbb593..73d0af2d 100644 --- a/background/usercss-install-helper.js +++ b/background/usercss-install-helper.js @@ -1,4 +1,10 @@ -/* global API_METHODS openURL download URLS tabManager */ +/* global + API_METHODS + download + openURL + tabManager + URLS +*/ 'use strict'; (() => { @@ -27,16 +33,36 @@ return code; }; + const maybeDistro = { + // https://github.com/StylishThemes/GitHub-Dark/raw/master/github-dark.user.css + 'github.com': { + glob: '/*/raw/*', + rx: /^https:\/\/github\.com\/[^/]+\/[^/]+\/raw\/[^/]+\/[^/]+?\.user\.(css|styl)([#?].*)?$/, + }, + // https://raw.githubusercontent.com/StylishThemes/GitHub-Dark/master/github-dark.user.css + 'raw.githubusercontent.com': { + glob: '/*', + rx: /^https:\/\/raw\.githubusercontent\.com(\/[^/]+?){4}\.user\.(css|styl)([#?].*)?$/, + }, + }; + // Faster installation on known distribution sites to avoid flicker of css text chrome.webRequest.onBeforeSendHeaders.addListener(({tabId, url}) => { - openInstallerPage(tabId, url, {}); - // Silently suppressing navigation like it never happened - return {redirectUrl: 'javascript:void 0'}; // eslint-disable-line no-script-url + const m = maybeDistro[new URL(url).hostname]; + if (!m || m.rx.test(url)) { + openInstallerPage(tabId, url, {}); + // Silently suppress navigation. + // Don't redirect to the install URL as it'll flash the text! + return {redirectUrl: 'javascript:void 0'}; // eslint-disable-line no-script-url + } }, { urls: [ URLS.usoArchiveRaw + 'usercss/*.user.css', '*://greasyfork.org/scripts/*/code/*.user.css', '*://sleazyfork.org/scripts/*/code/*.user.css', + ...[].concat( + ...Object.entries(maybeDistro) + .map(([host, {glob}]) => makeUsercssGlobs(host, glob))), ], types: ['main_frame'], }, ['blocking']); @@ -46,7 +72,7 @@ const h = responseHeaders.find(h => h.name.toLowerCase() === 'content-type'); tabManager.set(tabId, isContentTypeText.name, h && isContentTypeText(h.value) || undefined); }, { - urls: '%css,%css?*,%styl,%styl?*'.replace(/%/g, '*://*/*.user.').split(','), + urls: makeUsercssGlobs('*', '/*'), types: ['main_frame'], }, ['responseHeaders']); @@ -57,7 +83,7 @@ !oldUrl.startsWith(URLS.installUsercss)) { const inTab = url.startsWith('file:') && Boolean(fileLoader); const code = await (inTab ? fileLoader : urlLoader)(tabId, url); - if (/==userstyle==/i.test(code)) { + if (/==userstyle==/i.test(code) && !/^\s*