2021-01-01 14:27:58 +00:00
|
|
|
/* global API */// msg.js
|
2022-02-10 18:28:47 +00:00
|
|
|
/* global CHROME URLS ignoreChromeError */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global prefs */
|
2020-11-18 11:17:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
(() => {
|
2020-11-18 11:17:15 +00:00
|
|
|
const idCSP = 'patchCsp';
|
|
|
|
const idOFF = 'disableAll';
|
|
|
|
const idXHR = 'styleViaXhr';
|
|
|
|
const rxHOST = /^('none'|(https?:\/\/)?[^']+?[^:'])$/; // strips CSP sources covered by *
|
|
|
|
const blobUrlPrefix = 'blob:' + chrome.runtime.getURL('/');
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @type {Object<string,StylesToPass>} */
|
2020-11-18 11:17:15 +00:00
|
|
|
const stylesToPass = {};
|
2021-01-01 14:27:58 +00:00
|
|
|
const state = {};
|
|
|
|
const injectedCode = CHROME && `${data => {
|
|
|
|
if (self.INJECTED !== 1) { // storing data only if apply.js hasn't run yet
|
|
|
|
window[Symbol.for('styles')] = data;
|
|
|
|
}
|
|
|
|
}}`;
|
2020-11-18 11:17:15 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
toggle();
|
|
|
|
prefs.subscribe([idXHR, idOFF, idCSP], toggle);
|
2020-11-18 11:17:15 +00:00
|
|
|
|
|
|
|
function toggle() {
|
2021-01-01 14:27:58 +00:00
|
|
|
const off = prefs.get(idOFF);
|
|
|
|
const csp = prefs.get(idCSP) && !off;
|
|
|
|
const xhr = prefs.get(idXHR) && !off;
|
|
|
|
if (xhr === state.xhr && csp === state.csp && off === state.off) {
|
2020-11-18 11:17:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const reqFilter = {
|
|
|
|
urls: ['*://*/*'],
|
|
|
|
types: ['main_frame', 'sub_frame'],
|
|
|
|
};
|
|
|
|
chrome.webNavigation.onCommitted.removeListener(injectData);
|
2020-11-18 11:17:15 +00:00
|
|
|
chrome.webRequest.onBeforeRequest.removeListener(prepareStyles);
|
|
|
|
chrome.webRequest.onHeadersReceived.removeListener(modifyHeaders);
|
|
|
|
if (xhr || csp) {
|
2021-01-01 14:27:58 +00:00
|
|
|
// We unregistered it above so that the optional EXTRA_HEADERS is properly re-registered
|
2020-11-18 11:17:15 +00:00
|
|
|
chrome.webRequest.onHeadersReceived.addListener(modifyHeaders, reqFilter, [
|
|
|
|
'blocking',
|
|
|
|
'responseHeaders',
|
|
|
|
xhr && chrome.webRequest.OnHeadersReceivedOptions.EXTRA_HEADERS,
|
|
|
|
].filter(Boolean));
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (CHROME ? !off : xhr || csp) {
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(prepareStyles, reqFilter);
|
|
|
|
}
|
|
|
|
if (CHROME && !off) {
|
|
|
|
chrome.webNavigation.onCommitted.addListener(injectData, {url: [{urlPrefix: 'http'}]});
|
2020-11-18 11:17:15 +00:00
|
|
|
}
|
2022-02-10 18:28:47 +00:00
|
|
|
if (CHROME) {
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(openNamedStyle, {
|
2022-02-10 21:08:20 +00:00
|
|
|
urls: [URLS.ownOrigin + '*.user.css'],
|
2022-02-10 18:28:47 +00:00
|
|
|
types: ['main_frame'],
|
|
|
|
}, ['blocking']);
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
state.csp = csp;
|
|
|
|
state.off = off;
|
|
|
|
state.xhr = xhr;
|
2020-11-18 11:17:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @param {chrome.webRequest.WebRequestBodyDetails} req */
|
|
|
|
async function prepareStyles(req) {
|
|
|
|
const sections = await API.styles.getSectionsByUrl(req.url);
|
|
|
|
stylesToPass[req2key(req)] = /** @namespace StylesToPass */ {
|
|
|
|
blobId: '',
|
|
|
|
str: JSON.stringify(sections),
|
|
|
|
timer: setTimeout(cleanUp, 600e3, req),
|
|
|
|
};
|
2020-11-18 11:17:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function injectData(req) {
|
|
|
|
const data = stylesToPass[req2key(req)];
|
|
|
|
if (data && !data.injected) {
|
|
|
|
data.injected = true;
|
|
|
|
chrome.tabs.executeScript(req.tabId, {
|
|
|
|
frameId: req.frameId,
|
|
|
|
runAt: 'document_start',
|
|
|
|
code: `(${injectedCode})(${data.str})`,
|
2021-01-19 06:36:53 +00:00
|
|
|
}, ignoreChromeError);
|
2021-01-01 14:27:58 +00:00
|
|
|
if (!state.xhr) cleanUp(req);
|
|
|
|
}
|
2020-11-18 11:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {chrome.webRequest.WebResponseHeadersDetails} req */
|
|
|
|
function modifyHeaders(req) {
|
|
|
|
const {responseHeaders} = req;
|
2021-01-01 14:27:58 +00:00
|
|
|
const data = stylesToPass[req2key(req)];
|
|
|
|
if (!data || data.str === '{}') {
|
|
|
|
cleanUp(req);
|
2020-11-18 11:17:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (state.xhr) {
|
|
|
|
data.blobId = URL.createObjectURL(new Blob([data.str])).slice(blobUrlPrefix.length);
|
2020-11-18 11:17:15 +00:00
|
|
|
responseHeaders.push({
|
|
|
|
name: 'Set-Cookie',
|
2021-01-01 14:27:58 +00:00
|
|
|
value: `${chrome.runtime.id}=${data.blobId}`,
|
2020-11-18 11:17:15 +00:00
|
|
|
});
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const csp = state.csp &&
|
2020-11-18 11:17:15 +00:00
|
|
|
responseHeaders.find(h => h.name.toLowerCase() === 'content-security-policy');
|
|
|
|
if (csp) {
|
|
|
|
patchCsp(csp);
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (state.xhr || csp) {
|
2020-11-18 11:17:15 +00:00
|
|
|
return {responseHeaders};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {chrome.webRequest.HttpHeader} csp */
|
|
|
|
function patchCsp(csp) {
|
|
|
|
const src = {};
|
|
|
|
for (let p of csp.value.split(';')) {
|
|
|
|
p = p.trim().split(/\s+/);
|
|
|
|
src[p[0]] = p.slice(1);
|
|
|
|
}
|
|
|
|
// Allow style assets
|
|
|
|
patchCspSrc(src, 'img-src', 'data:', '*');
|
|
|
|
patchCspSrc(src, 'font-src', 'data:', '*');
|
2021-07-30 12:45:27 +00:00
|
|
|
// Allow our DOM styles, allow @import from any URL
|
|
|
|
patchCspSrc(src, 'style-src', "'unsafe-inline'", '*');
|
2020-11-18 11:17:15 +00:00
|
|
|
// Allow our XHR cookies in CSP sandbox (known case: raw github urls)
|
|
|
|
if (src.sandbox && !src.sandbox.includes('allow-same-origin')) {
|
|
|
|
src.sandbox.push('allow-same-origin');
|
|
|
|
}
|
|
|
|
csp.value = Object.entries(src).map(([k, v]) =>
|
|
|
|
`${k}${v.length ? ' ' : ''}${v.join(' ')}`).join('; ');
|
|
|
|
}
|
|
|
|
|
|
|
|
function patchCspSrc(src, name, ...values) {
|
|
|
|
let def = src['default-src'];
|
|
|
|
let list = src[name];
|
|
|
|
if (def || list) {
|
|
|
|
if (!def) def = [];
|
|
|
|
if (!list) list = [...def];
|
|
|
|
if (values.includes('*')) list = src[name] = list.filter(v => !rxHOST.test(v));
|
|
|
|
list.push(...values.filter(v => !list.includes(v) && !def.includes(v)));
|
|
|
|
if (!list.length) delete src[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function cleanUp(req) {
|
|
|
|
const key = req2key(req);
|
|
|
|
const data = stylesToPass[key];
|
|
|
|
if (data) {
|
|
|
|
delete stylesToPass[key];
|
|
|
|
clearTimeout(data.timer);
|
|
|
|
if (data.blobId) {
|
|
|
|
URL.revokeObjectURL(blobUrlPrefix + data.blobId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:28:47 +00:00
|
|
|
/** @param {chrome.webRequest.WebRequestBodyDetails} req */
|
|
|
|
function openNamedStyle(req) {
|
|
|
|
chrome.tabs.update(req.tabId, {url: 'edit.html?id=' + req.url.split('#')[1]});
|
|
|
|
return {cancel: true};
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function req2key(req) {
|
|
|
|
return req.tabId + ':' + req.frameId;
|
2020-11-18 11:17:15 +00:00
|
|
|
}
|
|
|
|
})();
|