Inject Stylus into shadowRoots

inject styles of stylus-addon in shadowRoot

Från och med 2025-06-29. Se den senaste versionen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name                Inject Stylus into shadowRoots
// @name:pt-BR          Injetar Stylus em shadowRoots
// @namespace           https://greatest.deepsurf.us/users/821661
// @version             1.0
// @description         inject styles of stylus-addon in shadowRoot
// @description:pt-BR   injeta estilos do stylus-addon em shadowRoot
// @author              hdyzen
// @run-at              document-start
// @match               https://*/*
// @grant               none
// @license             GPL-3.0-only
// ==/UserScript==

const sheet = new CSSStyleSheet();
const originalShadowRootDescriptor = Object.getOwnPropertyDescriptor(ShadowRoot.prototype, "adoptedStyleSheets");

function syncStyles() {
	const css = [...document.querySelectorAll("html > style.stylus")].map((s) => s.textContent).join("\n");
	sheet.replaceSync(css);
}

Object.defineProperty(ShadowRoot.prototype, "adoptedStyleSheets", {
	get() {
		return originalShadowRootDescriptor.get.call(this);
	},
	set(newSheets) {
		const sheets = [...newSheets];

		if (!sheets.includes(sheet)) {
			sheets.push(sheet);
		}

		originalShadowRootDescriptor.set.call(this, sheets);
	},
	configurable: true,
	enumerable: true,
});

new MutationObserver(syncStyles).observe(document.documentElement, {
	childList: true,
	subtree: true,
	characterData: true,
});