fixup! use executeScript for early data injection
This commit is contained in:
parent
5d306150b0
commit
14c4dbcfed
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
define(async require => {
|
define(async require => {
|
||||||
const {API} = require('/js/msg');
|
const {API} = require('/js/msg');
|
||||||
const {isEmptyObj} = require('/js/polyfill');
|
|
||||||
const prefs = require('/js/prefs');
|
const prefs = require('/js/prefs');
|
||||||
|
|
||||||
const idCSP = 'patchCsp';
|
const idCSP = 'patchCsp';
|
||||||
|
@ -10,12 +9,12 @@ define(async require => {
|
||||||
const idXHR = 'styleViaXhr';
|
const idXHR = 'styleViaXhr';
|
||||||
const rxHOST = /^('none'|(https?:\/\/)?[^']+?[^:'])$/; // strips CSP sources covered by *
|
const rxHOST = /^('none'|(https?:\/\/)?[^']+?[^:'])$/; // strips CSP sources covered by *
|
||||||
const blobUrlPrefix = 'blob:' + chrome.runtime.getURL('/');
|
const blobUrlPrefix = 'blob:' + chrome.runtime.getURL('/');
|
||||||
|
/** @type {Object<string,StylesToPass>} */
|
||||||
const stylesToPass = {};
|
const stylesToPass = {};
|
||||||
const state = {};
|
const state = {};
|
||||||
|
|
||||||
await prefs.initializing;
|
|
||||||
prefs.subscribe([idXHR, idOFF, idCSP], toggle);
|
|
||||||
toggle();
|
toggle();
|
||||||
|
prefs.subscribe([idXHR, idOFF, idCSP], toggle);
|
||||||
|
|
||||||
function toggle() {
|
function toggle() {
|
||||||
const off = prefs.get(idOFF);
|
const off = prefs.get(idOFF);
|
||||||
|
@ -51,14 +50,15 @@ define(async require => {
|
||||||
/** @param {chrome.webRequest.WebRequestBodyDetails} req */
|
/** @param {chrome.webRequest.WebRequestBodyDetails} req */
|
||||||
async function prepareStyles(req) {
|
async function prepareStyles(req) {
|
||||||
const sections = await API.styles.getSectionsByUrl(req.url);
|
const sections = await API.styles.getSectionsByUrl(req.url);
|
||||||
if (!isEmptyObj(sections)) {
|
stylesToPass[req2key(req)] = /** @namespace StylesToPass */ {
|
||||||
stylesToPass[req.url] = JSON.stringify(sections);
|
blobId: '',
|
||||||
setTimeout(cleanUp, 600e3, req.url);
|
str: JSON.stringify(sections),
|
||||||
}
|
timer: setTimeout(cleanUp, 600e3, req),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function injectData(req) {
|
function injectData(req) {
|
||||||
const str = stylesToPass[req.url];
|
const {str} = stylesToPass[req2key(req)] || {};
|
||||||
if (str) {
|
if (str) {
|
||||||
chrome.tabs.executeScript(req.tabId, {
|
chrome.tabs.executeScript(req.tabId, {
|
||||||
frameId: req.frameId,
|
frameId: req.frameId,
|
||||||
|
@ -69,25 +69,23 @@ define(async require => {
|
||||||
}
|
}
|
||||||
}})(${str})`,
|
}})(${str})`,
|
||||||
});
|
});
|
||||||
|
if (!state.xhr) cleanUp(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeObjectUrl(data) {
|
|
||||||
const blob = new Blob([data]);
|
|
||||||
return URL.createObjectURL(blob).slice(blobUrlPrefix.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @param {chrome.webRequest.WebResponseHeadersDetails} req */
|
/** @param {chrome.webRequest.WebResponseHeadersDetails} req */
|
||||||
function modifyHeaders(req) {
|
function modifyHeaders(req) {
|
||||||
const {responseHeaders} = req;
|
const {responseHeaders} = req;
|
||||||
const str = stylesToPass[req.url];
|
const data = stylesToPass[req2key(req)];
|
||||||
if (!str) {
|
if (!data || data.str === '{}') {
|
||||||
|
cleanUp(req);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (state.xhr) {
|
if (state.xhr) {
|
||||||
|
data.blobId = URL.createObjectURL(new Blob([data.str])).slice(blobUrlPrefix.length);
|
||||||
responseHeaders.push({
|
responseHeaders.push({
|
||||||
name: 'Set-Cookie',
|
name: 'Set-Cookie',
|
||||||
value: `${chrome.runtime.id}=${makeObjectUrl(str)}`,
|
value: `${chrome.runtime.id}=${data.blobId}`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const csp = state.csp &&
|
const csp = state.csp &&
|
||||||
|
@ -132,9 +130,19 @@ define(async require => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanUp(key) {
|
function cleanUp(req) {
|
||||||
const blobId = stylesToPass[key];
|
const key = req2key(req);
|
||||||
delete stylesToPass[key];
|
const data = stylesToPass[key];
|
||||||
if (blobId) URL.revokeObjectURL(blobUrlPrefix + blobId);
|
if (data) {
|
||||||
|
delete stylesToPass[key];
|
||||||
|
clearTimeout(data.timer);
|
||||||
|
if (data.blobId) {
|
||||||
|
URL.revokeObjectURL(blobUrlPrefix + data.blobId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function req2key(req) {
|
||||||
|
return req.tabId + ':' + req.frameId;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user