Repository navigation
Expand file tree
/
Copy pathpage.js
More file actions
55 lines (42 loc) · 1.56 KB
/
Copy pathpage.js
File metadata and controls
55 lines (42 loc) · 1.56 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const host = window.location.hostname
const style = document.documentElement.style
const config = { attributes: false, childList: true, subtree: false }
const dom = {
get themeColor() {
return document.head.querySelector('meta[name="theme-color"]') || document.createElement('meta')
},
get colorScheme() {
return document.head.querySelector('meta[name="color-scheme"]') || document.createElement('meta')
},
get contrast() {
return style.getPropertyValue('--blackout-contrast')
},
set contrast(val) {
style.setProperty('--blackout-contrast', +(val))
},
}
const callback = (element, name, content) => {
const observerCallback = (_, observer) => {
const meta = dom[element]
if (meta.content !== content) setMeta(element, name, content)
}
return observerCallback
}
const observe = (isLight) => {
const observerCallback = isLight ? callback('colorScheme', 'color-scheme', 'only light') : callback('themeColor', 'theme-color', '#000000')
const observer = new MutationObserver(observerCallback)
observer.observe(document.head, config)
}
chrome.storage.local.get({ contrast: '1.4', blockListUrls: [] }, ({ blockListUrls, contrast }) => {
const isLight = blockListUrls.includes(host)
observe(isLight)
if (isLight) return setMeta('colorScheme', 'color-scheme', 'only light')
if (dom.contrast === '') dom.contrast = contrast
setMeta('themeColor', 'theme-color', '#000000')
})
const setMeta = (element, name, content) => {
const meta = dom[element]
meta.name = name
meta.content = content
document.head.appendChild(meta)
}