forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle.ts
More file actions
30 lines (28 loc) · 1.1 KB
/
Copy pathstyle.ts
File metadata and controls
30 lines (28 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import {createNodeAsap, removeNode} from './utils/dom';
export function createOrUpdateStyle(css: string) {
createNodeAsap({
selectNode: () => document.getElementById('dark-reader-style'),
createNode: (target) => {
const style = document.createElement('style');
style.id = 'dark-reader-style';
style.type = 'text/css';
style.textContent = css;
target.appendChild(style);
},
updateNode: (existing) => {
if (css.replace(/^\s+/gm, '') !== existing.textContent.replace(/^\s+/gm, '')) {
existing.textContent = css;
}
},
selectTarget: () => document.head,
createTarget: () => {
const head = document.createElement('head');
document.documentElement.insertBefore(head, document.documentElement.firstElementChild);
return head;
},
isTargetMutation: (mutation) => mutation.target.nodeName.toLowerCase() === 'head',
});
}
export function removeStyle() {
removeNode(document.getElementById('dark-reader-style'));
}