Skip to content

Commit

Permalink
handle external overwrites of adoptedStyleSheets
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Dec 6, 2023
1 parent c1ebd08 commit a7314ed
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions content/style-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
const docRewriteObserver = RewriteObserver(updateRoot);
const docRootObserver = RootObserver(restoreOrder);
const toSafeChar = c => String.fromCharCode(0xFF00 + c.charCodeAt(0) - 0x20);
const getAss = () => Object.isExtensible(ass) ? ass : ass.slice(); // eslint-disable-line no-use-before-define
/** @type {InjectedStyle[]} */
const list = [];
/** @type {Map<number,InjectedStyle>} */
const table = new Map();
let /** @type {CSSStyleSheet[]} */ass;
let /** @type {CSSStyleSheet[]} */ ass; // frozen array in old Chrome, the ref changes
let /** @type {CSSStyleSheet[]} */ assV2; // mutable array, the ref doesn't change
let root = document.documentElement;
let isEnabled = true;
let isTransitionPatched = chrome.app && CSS.supports('accent-color', 'red'); // Chrome 93
Expand Down Expand Up @@ -87,13 +87,13 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({

function addElement(el, before) {
if (ass) {
const sheets = getAss();
const sheets = assV2 || wrappedDoc[kAss];
let i = sheets.indexOf(el);
if (i >= 0) el = sheets.splice(i, 1)[0];
i = before ? sheets.indexOf(before) : -1;
if (i >= 0) sheets.splice(i, 0, el);
else sheets.push(el);
if (sheets !== ass) wrappedDoc[kAss] = sheets;
if (!assV2) wrappedDoc[kAss] = sheets;
} else {
updateRoot().insertBefore(el, before);
}
Expand All @@ -112,11 +112,11 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
if (el.remove) {
el.remove();
} else if (ass) {
const sheets = getAss();
const sheets = assV2 || wrappedDoc[kAss];
const i = sheets.indexOf(el);
if (i >= 0) {
sheets.splice(i, 1);
if (sheets !== ass) wrappedDoc[kAss] = sheets;
if (!assV2) wrappedDoc[kAss] = sheets;
}
}
}
Expand All @@ -129,9 +129,9 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({

function replaceAss(readd) {
const elems = list.map(s => s.el);
const res = ass.filter(arrItemNotIn, new Set(elems));
const res = (assV2 || wrappedDoc[kAss]).filter(arrItemNotIn, new Set(elems));
if (readd) res.push(...elems);
ass = wrappedDoc[kAss] = res;
wrappedDoc[kAss] = res;
}

function applyStyles(isReplace, {cfg, sections}) {
Expand Down Expand Up @@ -203,7 +203,7 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
id = MEDIA + id;
el = new CSSStyleSheet({media: id});
setTextAndName(el, style);
for (const {media: m} of ass) if (m.mediaText === id) m.mediaText += '-old';
for (const {media: m} of assV2 || wrappedDoc[kAss]) if (m.mediaText === id) m.mediaText += '-old';
return el;
}
if (!creationDoc && (el = initCreationDoc(style))) {
Expand Down Expand Up @@ -296,9 +296,9 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
if (ok) return;
} catch (err) {}
}
if (retry && ffCsp) { // ffCsp bug got fixed
if (retry && ffCsp && (ass = wrappedDoc[kAss])) { // ffCsp bug got fixed
console.debug('Stylus switched to document.adoptedStyleSheets due to a strict CSP of the page');
ass = wrappedDoc[kAss];
assV2 = Object.isExtensible(ass) && ass;
return createStyle(style);
}
creationDoc = document;
Expand All @@ -319,6 +319,7 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
if (!el) {
bad = false;
} else if (ass) {
if (!assV2) ass = wrappedDoc[kAss];
for (let i = ass.length - list.length; i < ass.length; i++) {
if (i < 0 || ass[i] !== list[i].el) {
bad = true;
Expand Down Expand Up @@ -363,6 +364,7 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
if (!ass !== !cfg.ass) {
removeAllElements();
ass = ass ? null : wrappedDoc[kAss];
assV2 = ass && Object.isExtensible(ass) && ass;
for (const s of list) s.el = createStyle(s);
addAllElements();
}
Expand Down

0 comments on commit a7314ed

Please sign in to comment.