-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
152 lines (133 loc) · 5.07 KB
/
contentScript.js
File metadata and controls
152 lines (133 loc) · 5.07 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"use strict";
(async () => {
const CSS_FILTER = '--blackAndWhiteWeb-filter';
const CSS_INVISIBLE = '--blackAndWhiteWeb-invisible';
const CSS_TRANSITION_IN = '--blackAndWhiteWeb-filter-transition-in';
const CSS_TRANSITION_OUT = '--blackAndWhiteWeb-filter-transition-out';
const DEBUG = false;
let debug = {
log: DEBUG ? console.log.bind(console) : () => {} // log or NO_OP
}
let manifest = chrome.runtime.getManifest();
console.log(manifest.name + " v" + manifest.version);
let firstRun = true;
debug.log("[Black&WhiteWeb:CTX] invisible HTML!");
document.documentElement.classList.toggle(CSS_INVISIBLE, true);
const storage = chrome.storage.local;
let settings = await storage.get(null);
applySettings(settings);
let observer = null;
const INVISIBLE_TIMEOUT = 5000;
// restore visibility if settings is not populated within INVISIBLE_TIMEOUT
setTimeout(() => {
let settingsLoaded = settings && Object.keys(settings).length > 0;
debug.log("[Black&WhiteWeb:CTX] settingsLoaded:", settingsLoaded);
if (!settingsLoaded) {
debug.log("[Black&WhiteWeb:CTX] timed out (" + INVISIBLE_TIMEOUT + "ms): restore HTML visibility!");
document.documentElement.classList.toggle(CSS_INVISIBLE, false);
}
}, INVISIBLE_TIMEOUT);
function checkIfEnabled(options) {
let enabled = document.documentElement.classList.contains(CSS_FILTER);
chrome.runtime.sendMessage({
event: 'set_badge',
data: enabled
});
if (settings?.animate && !(options?.skipAnimate)) {
let transitionClass = enabled ? CSS_TRANSITION_IN : CSS_TRANSITION_OUT;
debug.log("[Black&WhiteWeb:CTX] transitionClass", transitionClass, settings, { firstRun: firstRun });
if (!firstRun || (firstRun && !(settings?.alwaysOn))) {
document.documentElement.classList.toggle(CSS_TRANSITION_IN, enabled);
document.documentElement.classList.toggle(CSS_TRANSITION_OUT, !enabled);
} else {
firstRun = false;
debug.log("[Black&WhiteWeb:CTX] set firstRun to", { firstRun: firstRun });
document.documentElement.classList.toggle(CSS_TRANSITION_OUT, !enabled);
}
}
return enabled;
}
function toggle(forceEnable, options) {
if (forceEnable != null) {
document.documentElement.classList.toggle(CSS_FILTER, forceEnable);
} else {
document.documentElement.classList.toggle(CSS_FILTER);
}
checkIfEnabled(options);
}
function mustEnableFor(tabUrl) {
let mustEnable = true;
// check blackList
if (settings.useBlackList) {
mustEnable = true;
for (let blackListUrl of settings.blackList) {
blackListUrl = blackListUrl.trim();
if (blackListUrl.length > 0 && tabUrl.indexOf(blackListUrl) >= 0) {
debug.log(`[Black&WhiteWeb:CTX] in blackList (matched '${blackListUrl}')`)
mustEnable = false;
break;
}
}
}
// check whiteList
if (settings.useWhiteList) {
mustEnable = false;
for (let whiteListUrl of settings.whiteList) {
whiteListUrl = whiteListUrl.trim();
if (whiteListUrl.length > 0 && tabUrl.indexOf(whiteListUrl) >= 0) {
debug.log(`[Black&WhiteWeb:CTX] in whiteList (matched '${whiteListUrl}')`)
mustEnable = true;
break;
}
}
}
return mustEnable;
}
function applySettings(settings) {
debug.log("[Black&WhiteWeb:CTX] visible HTML!");
document.documentElement.classList.toggle(CSS_INVISIBLE, false);
debug.log("[Black&WhiteWeb:CTX] settings", settings);
if (settings.alwaysOn) {
let mustEnable = mustEnableFor(window.location.href);
toggle(mustEnable, { skipAnimate: true });
}
}
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
debug.log("[Black&WhiteWeb:CTX] onMessage", msg);
const { event, data } = msg;
if (event === "toggle") {
let forceEnable = data;
observer.disconnect();
toggle(forceEnable);
observer.observe(document.documentElement, { attributes: true });
} else if (event === 'got_settings') {
settings = data;
applySettings(settings);
} else if (event === 'check_if_enabled') {
checkIfEnabled();
}
});
function mutationCallback(mutationsList) {
for (let mutation of mutationsList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
const isEnabled = checkIfEnabled({ skipAnimate: true });
const mustEnable = mustEnableFor(window.location.href);
debug.log("[Black&WhiteWeb:CTX] classList ", Array.from(document.documentElement.classList), isEnabled, mustEnable);
if (isEnabled != mustEnable) {
debug.log("[Black&WhiteWeb:CTX] re-apply on class mutation", mutation);
applySettings(settings);
}
}
}
}
function run() {
/*chrome.runtime.sendMessage({
event: 'request_settings',
data: null
});*/
// checkIfEnabled({ skipAnimate: true });
observer = new MutationObserver(mutationCallback);
observer.observe(document.documentElement, { attributes: true });
}
run();
})();