2019-03-10 02:58:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** @type {function(opts):StyleInjector} */
|
|
|
|
window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
|
2020-02-12 14:39:00 +00:00
|
|
|
compare,
|
|
|
|
onUpdate = () => {},
|
|
|
|
}) => {
|
2019-03-10 02:58:17 +00:00
|
|
|
const PREFIX = 'stylus-';
|
2020-02-12 14:39:00 +00:00
|
|
|
const PATCH_ID = 'transition-patch';
|
2019-03-10 02:58:17 +00:00
|
|
|
// styles are out of order if any of these elements is injected between them
|
|
|
|
const ORDERED_TAGS = new Set(['head', 'body', 'frameset', 'style', 'link']);
|
2022-02-13 01:36:33 +00:00
|
|
|
const docRewriteObserver = RewriteObserver(sort);
|
|
|
|
const docRootObserver = RootObserver(sortIfNeeded);
|
2022-02-10 18:28:47 +00:00
|
|
|
const toSafeChar = c => String.fromCharCode(0xFF00 + c.charCodeAt(0) - 0x20);
|
2019-03-10 02:58:17 +00:00
|
|
|
const list = [];
|
|
|
|
const table = new Map();
|
2020-02-12 14:39:00 +00:00
|
|
|
let isEnabled = true;
|
2022-03-08 20:38:51 +00:00
|
|
|
let isTransitionPatched = chrome.app && CSS.supports('accent-color', 'red'); // Chrome 93
|
2022-02-10 18:28:47 +00:00
|
|
|
let exposeStyleName;
|
2020-02-12 14:39:00 +00:00
|
|
|
// will store the original method refs because the page can override them
|
|
|
|
let creationDoc, createElement, createElementNS;
|
2020-07-15 04:31:33 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
return /** @namespace StyleInjector */ {
|
2020-07-15 04:31:33 +00:00
|
|
|
|
2019-03-10 02:58:17 +00:00
|
|
|
list,
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async apply(styleMap) {
|
2022-02-13 01:36:33 +00:00
|
|
|
const styles = styleMapToArray(styleMap);
|
2021-01-01 14:27:58 +00:00
|
|
|
const value = !styles.length
|
|
|
|
? []
|
|
|
|
: await docRootObserver.evade(() => {
|
|
|
|
if (!isTransitionPatched && isEnabled) {
|
2022-02-13 01:36:33 +00:00
|
|
|
applyTransitionPatch(styles);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2022-02-13 01:36:33 +00:00
|
|
|
return styles.map(addUpdate);
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
2022-02-13 01:36:33 +00:00
|
|
|
emitUpdate();
|
2021-01-01 14:27:58 +00:00
|
|
|
return value;
|
2020-07-15 04:31:33 +00:00
|
|
|
},
|
2020-02-12 14:39:00 +00:00
|
|
|
|
2020-07-15 04:31:33 +00:00
|
|
|
clear() {
|
2022-02-13 01:36:33 +00:00
|
|
|
addRemoveElements(false);
|
2020-07-15 04:31:33 +00:00
|
|
|
list.length = 0;
|
|
|
|
table.clear();
|
2022-02-13 01:36:33 +00:00
|
|
|
emitUpdate();
|
2020-07-15 04:31:33 +00:00
|
|
|
},
|
2020-02-12 14:39:00 +00:00
|
|
|
|
2020-07-15 04:31:33 +00:00
|
|
|
clearOrphans() {
|
|
|
|
for (const el of document.querySelectorAll(`style[id^="${PREFIX}"].stylus`)) {
|
|
|
|
const id = el.id.slice(PREFIX.length);
|
|
|
|
if (/^\d+$/.test(id) || id === PATCH_ID) {
|
|
|
|
el.remove();
|
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
2020-07-15 04:31:33 +00:00
|
|
|
},
|
2019-03-10 02:58:17 +00:00
|
|
|
|
2020-07-15 04:31:33 +00:00
|
|
|
remove(id) {
|
2022-02-13 01:36:33 +00:00
|
|
|
remove(id);
|
|
|
|
emitUpdate();
|
2020-07-15 04:31:33 +00:00
|
|
|
},
|
2019-03-10 02:58:17 +00:00
|
|
|
|
2020-07-15 04:31:33 +00:00
|
|
|
replace(styleMap) {
|
2022-02-13 01:36:33 +00:00
|
|
|
const styles = styleMapToArray(styleMap);
|
2020-07-15 04:31:33 +00:00
|
|
|
const added = new Set(styles.map(s => s.id));
|
|
|
|
const removed = [];
|
|
|
|
for (const style of list) {
|
|
|
|
if (!added.has(style.id)) {
|
|
|
|
removed.push(style.id);
|
|
|
|
}
|
2020-02-12 14:39:00 +00:00
|
|
|
}
|
2022-02-13 01:36:33 +00:00
|
|
|
styles.forEach(addUpdate);
|
|
|
|
removed.forEach(remove);
|
|
|
|
emitUpdate();
|
2020-07-15 04:31:33 +00:00
|
|
|
},
|
2019-03-10 02:58:17 +00:00
|
|
|
|
2020-07-15 04:31:33 +00:00
|
|
|
toggle(enable) {
|
|
|
|
if (isEnabled === enable) return;
|
|
|
|
isEnabled = enable;
|
2022-02-13 01:36:33 +00:00
|
|
|
if (!enable) toggleObservers(false);
|
|
|
|
addRemoveElements(enable);
|
|
|
|
if (enable) toggleObservers(true);
|
2020-07-15 04:31:33 +00:00
|
|
|
},
|
2022-01-14 12:44:48 +00:00
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
sort: sort,
|
2020-07-15 04:31:33 +00:00
|
|
|
};
|
2020-02-12 14:39:00 +00:00
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function add(style) {
|
|
|
|
const el = style.el = createStyle(style);
|
2020-07-15 04:31:33 +00:00
|
|
|
const i = list.findIndex(item => compare(item, style) > 0);
|
2019-03-10 02:58:17 +00:00
|
|
|
table.set(style.id, style);
|
2020-07-15 04:31:33 +00:00
|
|
|
if (isEnabled) {
|
|
|
|
document.documentElement.insertBefore(el, i < 0 ? null : list[i].el);
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
2020-07-17 19:42:21 +00:00
|
|
|
list.splice(i < 0 ? list.length : i, 0, style);
|
2020-02-12 14:39:00 +00:00
|
|
|
return el;
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function addRemoveElements(add) {
|
2020-07-15 04:31:33 +00:00
|
|
|
for (const {el} of list) {
|
|
|
|
if (add) {
|
|
|
|
document.documentElement.appendChild(el);
|
|
|
|
} else {
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function addUpdate(style) {
|
|
|
|
return table.has(style.id) ? update(style) : add(style);
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function applyTransitionPatch(styles) {
|
2020-02-12 16:44:25 +00:00
|
|
|
isTransitionPatched = true;
|
2020-02-12 14:39:00 +00:00
|
|
|
// CSS transition bug workaround: since we insert styles asynchronously,
|
|
|
|
// the browsers, especially Firefox, may apply all transitions on page load
|
2020-02-12 16:44:25 +00:00
|
|
|
if (document.readyState === 'complete' ||
|
|
|
|
document.visibilityState === 'hidden' ||
|
|
|
|
!styles.some(s => s.code.includes('transition'))) {
|
2020-02-12 14:39:00 +00:00
|
|
|
return;
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
2022-02-13 01:36:33 +00:00
|
|
|
const el = createStyle({id: PATCH_ID, code: `
|
2020-02-12 14:39:00 +00:00
|
|
|
:root:not(#\\0):not(#\\0) * {
|
|
|
|
transition: none !important;
|
|
|
|
}
|
2022-02-10 18:28:47 +00:00
|
|
|
`});
|
2020-02-12 14:39:00 +00:00
|
|
|
document.documentElement.appendChild(el);
|
|
|
|
// wait for the next paint to complete
|
|
|
|
// note: requestAnimationFrame won't fire in inactive tabs
|
|
|
|
requestAnimationFrame(() => setTimeout(() => el.remove()));
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function createStyle(style = {}) {
|
|
|
|
const {id} = style;
|
|
|
|
if (!creationDoc) initCreationDoc();
|
2019-03-10 02:58:17 +00:00
|
|
|
let el;
|
|
|
|
if (document.documentElement instanceof SVGSVGElement) {
|
|
|
|
// SVG document style
|
2020-02-12 14:39:00 +00:00
|
|
|
el = createElementNS.call(creationDoc, 'http://www.w3.org/2000/svg', 'style');
|
2019-03-10 02:58:17 +00:00
|
|
|
} else if (document instanceof XMLDocument) {
|
|
|
|
// XML document style
|
2020-02-12 14:39:00 +00:00
|
|
|
el = createElementNS.call(creationDoc, 'http://www.w3.org/1999/xhtml', 'style');
|
2019-03-10 02:58:17 +00:00
|
|
|
} else {
|
|
|
|
// HTML document style; also works on HTML-embedded SVG
|
2020-02-12 14:39:00 +00:00
|
|
|
el = createElement.call(creationDoc, 'style');
|
|
|
|
}
|
|
|
|
if (id) {
|
|
|
|
el.id = `${PREFIX}${id}`;
|
|
|
|
const oldEl = document.getElementById(el.id);
|
|
|
|
if (oldEl) oldEl.id += '-superseded-by-Stylus';
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
2022-02-13 01:36:33 +00:00
|
|
|
el.type = 'text/css';
|
|
|
|
// SVG className is not a string, but an instance of SVGAnimatedString
|
|
|
|
el.classList.add('stylus');
|
|
|
|
setTextAndName(el, style);
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTextAndName(el, {id, code = '', name}) {
|
2022-02-10 18:28:47 +00:00
|
|
|
if (exposeStyleName && name) {
|
|
|
|
el.dataset.name = name;
|
2022-02-13 13:19:58 +00:00
|
|
|
name = encodeURIComponent(name.replace(/[?#%/@:']/g, toSafeChar));
|
2022-02-10 21:08:20 +00:00
|
|
|
code += `\n/*# sourceURL=${chrome.runtime.getURL(name)}.user.css#${id} */`;
|
2022-02-10 18:28:47 +00:00
|
|
|
}
|
2020-02-12 14:39:00 +00:00
|
|
|
el.textContent = code;
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function toggleObservers(shouldStart) {
|
2020-07-15 04:31:33 +00:00
|
|
|
const onOff = shouldStart && isEnabled ? 'start' : 'stop';
|
|
|
|
docRewriteObserver[onOff]();
|
|
|
|
docRootObserver[onOff]();
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function emitUpdate() {
|
|
|
|
toggleObservers(list.length);
|
2020-02-12 14:39:00 +00:00
|
|
|
onUpdate();
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 14:39:00 +00:00
|
|
|
/*
|
|
|
|
FF59+ workaround: allow the page to read our sheets, https://github.com/openstyles/stylus/issues/461
|
|
|
|
First we're trying the page context document where inline styles may be forbidden by CSP
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=1579345#c3
|
|
|
|
and since userAgent.navigator can be spoofed via about:config or devtools,
|
|
|
|
we're checking for getPreventDefault that was removed in FF59
|
|
|
|
*/
|
2022-02-13 01:36:33 +00:00
|
|
|
function initCreationDoc() {
|
2020-02-12 14:39:00 +00:00
|
|
|
creationDoc = !Event.prototype.getPreventDefault && document.wrappedJSObject;
|
|
|
|
if (creationDoc) {
|
|
|
|
({createElement, createElementNS} = creationDoc);
|
2022-02-13 01:36:33 +00:00
|
|
|
const el = document.documentElement.appendChild(createStyle());
|
2020-02-12 14:39:00 +00:00
|
|
|
const isApplied = el.sheet;
|
|
|
|
el.remove();
|
|
|
|
if (isApplied) return;
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
2020-02-12 14:39:00 +00:00
|
|
|
creationDoc = document;
|
|
|
|
({createElement, createElementNS} = document);
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function remove(id) {
|
2020-02-12 14:39:00 +00:00
|
|
|
const style = table.get(id);
|
|
|
|
if (!style) return;
|
|
|
|
table.delete(id);
|
|
|
|
list.splice(list.indexOf(style), 1);
|
|
|
|
style.el.remove();
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function sort() {
|
2020-02-12 14:39:00 +00:00
|
|
|
docRootObserver.evade(() => {
|
|
|
|
list.sort(compare);
|
2022-02-13 01:36:33 +00:00
|
|
|
addRemoveElements(true);
|
2020-02-12 14:39:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function sortIfNeeded() {
|
2020-02-12 14:39:00 +00:00
|
|
|
let needsSort;
|
|
|
|
let el = list.length && list[0].el;
|
|
|
|
if (!el) {
|
|
|
|
needsSort = false;
|
|
|
|
} else if (el.parentNode !== creationDoc.documentElement) {
|
|
|
|
needsSort = true;
|
|
|
|
} else {
|
|
|
|
let i = 0;
|
|
|
|
while (el) {
|
|
|
|
if (i < list.length && el === list[i].el) {
|
|
|
|
i++;
|
|
|
|
} else if (ORDERED_TAGS.has(el.localName)) {
|
|
|
|
needsSort = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
el = el.nextElementSibling;
|
|
|
|
}
|
|
|
|
// some styles are not injected to the document
|
|
|
|
if (i < list.length) needsSort = true;
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
2022-02-13 01:36:33 +00:00
|
|
|
if (needsSort) sort();
|
2020-02-12 14:39:00 +00:00
|
|
|
return needsSort;
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function styleMapToArray(styleMap) {
|
|
|
|
if (styleMap.cfg) {
|
|
|
|
({exposeStyleName} = styleMap.cfg);
|
|
|
|
delete styleMap.cfg;
|
|
|
|
}
|
2022-02-10 18:28:47 +00:00
|
|
|
return Object.values(styleMap).map(({id, code, name}) => ({
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
code: code.join(''),
|
2020-02-12 14:39:00 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function update(newStyle) {
|
|
|
|
const {id, code} = newStyle;
|
2020-02-12 14:39:00 +00:00
|
|
|
const style = table.get(id);
|
2022-02-13 01:36:33 +00:00
|
|
|
if (style.code !== code ||
|
|
|
|
style.name !== newStyle.name && exposeStyleName) {
|
2021-01-01 14:27:58 +00:00
|
|
|
style.code = code;
|
2022-02-13 01:36:33 +00:00
|
|
|
setTextAndName(style.el, newStyle);
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 14:39:00 +00:00
|
|
|
function RewriteObserver(onChange) {
|
|
|
|
// detect documentElement being rewritten from inside the script
|
|
|
|
let root;
|
|
|
|
let observing = false;
|
|
|
|
let timer;
|
2022-02-13 01:36:33 +00:00
|
|
|
const observer = new MutationObserver(check);
|
2020-02-12 14:39:00 +00:00
|
|
|
return {start, stop};
|
|
|
|
|
|
|
|
function start() {
|
|
|
|
if (observing) return;
|
|
|
|
// detect dynamic iframes rewritten after creation by the embedder i.e. externally
|
|
|
|
root = document.documentElement;
|
2022-02-13 01:36:33 +00:00
|
|
|
timer = setTimeout(check);
|
2020-02-12 14:39:00 +00:00
|
|
|
observer.observe(document, {childList: true});
|
|
|
|
observing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop() {
|
|
|
|
if (!observing) return;
|
|
|
|
clearTimeout(timer);
|
|
|
|
observer.disconnect();
|
|
|
|
observing = false;
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function check() {
|
2020-02-12 14:39:00 +00:00
|
|
|
if (root !== document.documentElement) {
|
|
|
|
root = document.documentElement;
|
|
|
|
onChange();
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 14:39:00 +00:00
|
|
|
|
|
|
|
function RootObserver(onChange) {
|
|
|
|
let digest = 0;
|
|
|
|
let lastCalledTime = NaN;
|
|
|
|
let observing = false;
|
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
if (digest) {
|
|
|
|
if (performance.now() - lastCalledTime > 1000) {
|
|
|
|
digest = 0;
|
|
|
|
} else if (digest > 5) {
|
|
|
|
throw new Error('The page keeps generating mutations. Skip the event.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (onChange()) {
|
|
|
|
digest++;
|
|
|
|
lastCalledTime = performance.now();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {evade, start, stop};
|
|
|
|
|
|
|
|
function evade(fn) {
|
|
|
|
const restore = observing && start;
|
|
|
|
stop();
|
2022-02-13 01:36:33 +00:00
|
|
|
return new Promise(resolve => run(fn, resolve, waitForRoot))
|
2020-02-12 14:39:00 +00:00
|
|
|
.then(restore);
|
|
|
|
}
|
|
|
|
|
|
|
|
function start() {
|
|
|
|
if (observing) return;
|
|
|
|
observer.observe(document.documentElement, {childList: true});
|
|
|
|
observing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop() {
|
|
|
|
if (!observing) return;
|
|
|
|
// FIXME: do we need this?
|
|
|
|
observer.takeRecords();
|
|
|
|
observer.disconnect();
|
|
|
|
observing = false;
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function run(fn, resolve, wait) {
|
2020-02-12 14:39:00 +00:00
|
|
|
if (document.documentElement) {
|
|
|
|
resolve(fn());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (wait) wait(fn, resolve);
|
|
|
|
}
|
|
|
|
|
2022-02-13 01:36:33 +00:00
|
|
|
function waitForRoot(...args) {
|
|
|
|
new MutationObserver((_, observer) => run(...args) && observer.disconnect())
|
2020-02-12 14:39:00 +00:00
|
|
|
.observe(document, {childList: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|