-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
134 lines (109 loc) · 3.04 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import ExclusionHandler from './exclusion-handler'
const exclusionHandler = new ExclusionHandler();
const DARK_THRESHOLD = 128;
const EXCLUDES = [
'#bluebarRoot', // Facebook header
'.player-poster', // Player poster from Zing
'#_header.header' // header from tv.zing.vn
];
const ADDITIONAL_CHECK = [
'header',
'.header',
'footer'
];
const BACK_INVERSE_TAGS = 'img, canvas, video, [style*="background-image"]';
class NightPage {
constructor() {
this.currentHost = this.getHost();
if (!this.isExclusiveSupport()) {
this.invertPage();
this.backInverse();
this.setObserver();
} else {
this.handleExclusion();
}
}
getHost() {
return document.location.host.replace('www.', '');
}
isExclusiveSupport() {
return exclusionHandler.isExclusion(this.currentHost);
}
invertPage() {
const bodyStyle = getComputedStyle(document.body);
if (this._isDark(bodyStyle.backgroundColor)) {
console.log('Website is already dark');
return;
}
if (parseInt(bodyStyle.height) < window.outerHeight) {
document.body.style.minHeight = '100vh';
}
this._i1(document.documentElement);
document.body.style.background = 'rgb(230,230,230)'; // gray color
document.body.style.margin = '0';
document.documentElement.style.background = 'white';
}
backInverse() {
Array.from(document.querySelectorAll(BACK_INVERSE_TAGS)).forEach(this._i1);
Array.from(document.querySelectorAll(EXCLUDES.join(','))).forEach(el => {
this._i1(el);
Array.from(el.querySelectorAll(BACK_INVERSE_TAGS)).forEach(this._i0);
});
Array.from(document.querySelectorAll(ADDITIONAL_CHECK.join(','))).forEach(el => {
const styles = window.getComputedStyle(el);
Array.from(el.querySelectorAll(BACK_INVERSE_TAGS)).forEach(this._i0);
if (styles.backgroundColor && this._isDark(styles.backgroundColor)) {
this._i1(el);
}
});
}
setObserver() {
/*
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes && mutation.addedNodes.length) {
Array.from(mutation.addedNodes).forEach(node => {
if (node.matches && node.matches('img')) {
debugger;
node.style.filter = 'invert(1)';
}
});
}
});
});
observer.observe(document.body, {subtree: true, childList: true});
*/
}
/**
* Runs a special rule for this website
*/
handleExclusion() {
exclusionHandler.applyExclusion(this.currentHost);
}
_i1(el) {
el.style.filter = 'invert(1)';
}
_i0(el) {
el.style.filter = 'invert(0)';
}
_isDark(styleStr) {
return styleStr !== 'rgba(0, 0, 0, 0)' && this._getBrightness(styleStr) < DARK_THRESHOLD;
}
_getBrightness(rgb) {
if (typeof rgb === 'string') {
rgb = this._toRGB(rgb);
}
return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000;
}
_toRGB(styleStr) {
const re = /rgba?\((\d{1,3}),(\d{1,3}),(\d{1,3})/i;
styleStr = styleStr.replace(/\s+/g, '');
const found = styleStr.match(re);
if (found && found.length === 4) {
return {r: found[1], g: found[2], b: found[3]};
}
return null;
}
}
// Run
new NightPage();