2021-01-01 14:27:58 +00:00
|
|
|
/* global URLS download openURL */// toolbox.js
|
|
|
|
/* global addAPI bgReady */// common.js
|
|
|
|
/* global tabMan */// msg.js
|
2020-10-06 10:49:43 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
bgReady.all.then(() => {
|
2020-10-06 10:49:43 +00:00
|
|
|
const installCodeCache = {};
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
addAPI(/** @namespace API */ {
|
|
|
|
usercss: {
|
|
|
|
getInstallCode(url) {
|
|
|
|
// when the installer tab is reloaded after the cache is expired, this will throw intentionally
|
|
|
|
const {code, timer} = installCodeCache[url];
|
|
|
|
clearInstallCode(url);
|
|
|
|
clearTimeout(timer);
|
|
|
|
return code;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-10-06 10:49:43 +00:00
|
|
|
|
2020-11-18 11:17:15 +00:00
|
|
|
// `glob`: pathname match pattern for webRequest
|
|
|
|
// `rx`: pathname regex to verify the URL really looks like a raw usercss
|
|
|
|
const maybeDistro = {
|
|
|
|
// https://github.com/StylishThemes/GitHub-Dark/raw/master/github-dark.user.css
|
|
|
|
'github.com': {
|
|
|
|
glob: '/*/raw/*',
|
|
|
|
rx: /^\/[^/]+\/[^/]+\/raw\/[^/]+\/[^/]+?\.user\.(css|styl)$/,
|
|
|
|
},
|
|
|
|
// https://raw.githubusercontent.com/StylishThemes/GitHub-Dark/master/github-dark.user.css
|
|
|
|
'raw.githubusercontent.com': {
|
|
|
|
glob: '/*',
|
|
|
|
rx: /^(\/[^/]+?){4}\.user\.(css|styl)$/,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
chrome.webRequest.onBeforeSendHeaders.addListener(maybeInstallFromDistro, {
|
2020-10-06 10:49:43 +00:00
|
|
|
urls: [
|
|
|
|
URLS.usoArchiveRaw + 'usercss/*.user.css',
|
|
|
|
'*://greasyfork.org/scripts/*/code/*.user.css',
|
|
|
|
'*://sleazyfork.org/scripts/*/code/*.user.css',
|
2020-11-18 11:17:15 +00:00
|
|
|
...[].concat(
|
|
|
|
...Object.entries(maybeDistro)
|
|
|
|
.map(([host, {glob}]) => makeUsercssGlobs(host, glob))),
|
2020-10-06 10:49:43 +00:00
|
|
|
],
|
|
|
|
types: ['main_frame'],
|
|
|
|
}, ['blocking']);
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
chrome.webRequest.onHeadersReceived.addListener(rememberContentType, {
|
2020-11-18 11:17:15 +00:00
|
|
|
urls: makeUsercssGlobs('*', '/*'),
|
2020-10-06 10:49:43 +00:00
|
|
|
types: ['main_frame'],
|
|
|
|
}, ['responseHeaders']);
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
tabMan.onUpdate(maybeInstall);
|
|
|
|
|
|
|
|
function clearInstallCode(url) {
|
|
|
|
return delete installCodeCache[url];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sites may be using custom types like text/stylus so this coarse filter only excludes html */
|
|
|
|
function isContentTypeText(type) {
|
|
|
|
return /^text\/(?!html)/i.test(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// in Firefox we have to use a content script to read file://
|
|
|
|
async function loadFromFile(tabId) {
|
|
|
|
return (await browser.tabs.executeScript(tabId, {file: '/content/install-hook-usercss.js'}))[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadFromUrl(tabId, url) {
|
|
|
|
return (
|
|
|
|
url.startsWith('file:') ||
|
|
|
|
tabMan.get(tabId, isContentTypeText.name) ||
|
|
|
|
isContentTypeText((await fetch(url, {method: 'HEAD'})).headers.get('content-type'))
|
|
|
|
) && download(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeUsercssGlobs(host, path) {
|
|
|
|
return '%css,%css?*,%styl,%styl?*'.replace(/%/g, `*://${host}${path}.user.`).split(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function maybeInstall({tabId, url, oldUrl = ''}) {
|
2020-10-06 10:49:43 +00:00
|
|
|
if (url.includes('.user.') &&
|
|
|
|
/^(https?|file|ftps?):/.test(url) &&
|
|
|
|
/\.user\.(css|styl)$/.test(url.split(/[#?]/, 1)[0]) &&
|
|
|
|
!oldUrl.startsWith(URLS.installUsercss)) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const inTab = url.startsWith('file:') && !chrome.app;
|
|
|
|
const code = await (inTab ? loadFromFile : loadFromUrl)(tabId, url);
|
|
|
|
if (!/^\s*</.test(code) && URLS.rxMETA.test(code)) {
|
2020-10-06 10:49:43 +00:00
|
|
|
openInstallerPage(tabId, url, {code, inTab});
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Faster installation on known distribution sites to avoid flicker of css text */
|
|
|
|
function maybeInstallFromDistro({tabId, url}) {
|
|
|
|
const u = new URL(url);
|
|
|
|
const m = maybeDistro[u.hostname];
|
|
|
|
if (!m || m.rx.test(u.pathname)) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 10:49:43 +00:00
|
|
|
|
|
|
|
function openInstallerPage(tabId, url, {code, inTab} = {}) {
|
|
|
|
const newUrl = `${URLS.installUsercss}?updateUrl=${encodeURIComponent(url)}`;
|
|
|
|
if (inTab) {
|
|
|
|
browser.tabs.get(tabId).then(tab =>
|
|
|
|
openURL({
|
|
|
|
url: `${newUrl}&tabId=${tabId}`,
|
|
|
|
active: tab.active,
|
|
|
|
index: tab.index + 1,
|
|
|
|
openerTabId: tabId,
|
|
|
|
currentWindow: null,
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
const timer = setTimeout(clearInstallCode, 10e3, url);
|
|
|
|
installCodeCache[url] = {code, timer};
|
|
|
|
chrome.tabs.update(tabId, {url: newUrl});
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 11:17:15 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** Remember Content-Type to avoid wasting time to re-fetch in loadFromUrl **/
|
|
|
|
function rememberContentType({tabId, responseHeaders}) {
|
|
|
|
const h = responseHeaders.find(h => h.name.toLowerCase() === 'content-type');
|
|
|
|
tabMan.set(tabId, isContentTypeText.name, h && isContentTypeText(h.value) || undefined);
|
2020-11-18 11:17:15 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|