MCPcopy Index your code
hub / github.com/darkreader/darkreader / createOrUpdateStyle

Function createOrUpdateStyle

src/inject/style.ts:3–28  ·  view source on GitHub ↗
(css: string, type: string)

Source from the content-addressed store, hash-verified

1import {createNodeAsap, removeNode} from './utils/dom';
2
3export function createOrUpdateStyle(css: string, type: string): void {
4 createNodeAsap({
5 selectNode: () => document.getElementById('dark-reader-style')!,
6 createNode: (target) => {
7 document.documentElement.setAttribute('data-darkreader-mode', type);
8 const style = document.createElement('style');
9 style.id = 'dark-reader-style';
10 style.classList.add('darkreader');
11 style.type = 'text/css';
12 style.textContent = css;
13 target.appendChild(style);
14 },
15 updateNode: (existing) => {
16 if (css.replace(/^\s+/gm, '') !== existing.textContent!.replace(/^\s+/gm, '')) {
17 existing.textContent = css;
18 }
19 },
20 selectTarget: () => document.head,
21 createTarget: () => {
22 const head = document.createElement('head');
23 document.documentElement.insertBefore(head, document.documentElement.firstElementChild);
24 return head;
25 },
26 isTargetMutation: (mutation) => mutation.target.nodeName.toLowerCase() === 'head',
27 });
28}
29
30export function removeStyle(): void {
31 removeNode(document.getElementById('dark-reader-style'));

Callers 1

onMessageFunction · 0.90

Calls 2

createNodeAsapFunction · 0.90
addMethod · 0.80

Tested by

no test coverage detected