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

Function injectStyleAway

src/inject/dynamic-theme/injection.ts:21–63  ·  view source on GitHub ↗
(styleElement: HTMLStyleElement | SVGStyleElement)

Source from the content-addressed store, hash-verified

19let bodyObserver: MutationObserver | null;
20
21export function injectStyleAway(styleElement: HTMLStyleElement | SVGStyleElement) {
22 if (!document.body) {
23 stylesWaitingForBody.add(styleElement);
24 if (!bodyObserver) {
25 bodyObserver = new MutationObserver(() => {
26 if (document.body) {
27 bodyObserver!.disconnect();
28 bodyObserver = null;
29 stylesWaitingForBody.forEach((el) => injectStyleAway(el));
30 stylesWaitingForBody.clear();
31 }
32 });
33 }
34 return;
35 }
36
37 let container: HTMLElement | null = document.body.querySelector('.darkreader-style-container');
38 if (!container) {
39 container = document.createElement('div');
40 container.classList.add('darkreader');
41 container.classList.add('darkreader-style-container');
42 container.style.display = 'none';
43 document.body.append(container);
44
45 containerObserver = new MutationObserver(() => {
46 if (container?.nextElementSibling != null) {
47 // Prevent clearing style overrides after container move
48 (container.querySelectorAll('.darkreader--sync') as NodeListOf<HTMLStyleElement>).forEach((el) => {
49 if (el.sheet!.cssRules.length > 0) {
50 let cssText = '';
51 for (const rule of el.sheet!.cssRules) {
52 cssText += rule.cssText;
53 }
54 el.textContent = cssText;
55 }
56 });
57 document.body.append(container);
58 }
59 });
60 containerObserver.observe(document.body, {childList: true});
61 }
62 container.append(styleElement);
63}
64
65let containerObserver: MutationObserver;
66

Callers 2

injectStaticStyleFunction · 0.90
insertStyleFunction · 0.90

Calls 3

addMethod · 0.80
disconnectMethod · 0.80
clearMethod · 0.45

Tested by

no test coverage detected