-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
102 lines (97 loc) · 2.91 KB
/
content-script.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
console.log('content-script');
let observerObject, theme;
//
function setBeforeBackgroundColor() {
theme == 'light'
? document.querySelector('.view-nav') &&
document.querySelector('.view-nav').classList.remove('blackcolor')
: document.querySelector('.view-nav') &&
document.querySelector('.view-nav').classList.add('blackcolor');
}
function setColor(elements) {
const doms = [...elements];
for (const element of doms) {
theme == 'light'
? element.classList.remove('blackBackground')
: element.classList.add('blackBackground');
}
setBeforeBackgroundColor();
}
function callback(mutationList, observer) {
mutationList.forEach((mutation) => {
switch (mutation.type) {
case 'childList':
const elements = document.querySelectorAll(
'.entry-list-wrap .entry-list .entry, .entry-list-wrap .entry-list'
);
if (elements) {
setColor(elements);
}
break;
}
});
}
function observeDom() {
var targetNode = document.querySelector('#juejin .entry-list-wrap');
var observerOptions = {
childList: true, // 观察目标子节点的变化,是否有添加或者删除
};
observerObject = new MutationObserver(callback);
observerObject.observe(targetNode, observerOptions);
}
function setDarkColor() {
document.querySelector('body').classList.add('blackBackground');
document.querySelector('#juejin header') &&
document.querySelector('#juejin header').classList.add('blackBackground');
document.querySelector('#juejin header.list-header') &&
document.querySelector('#juejin header.list-header').classList.add('blackBackground');
if (observerObject) {
observerObject.disconnect();
}
observeDom();
const elements = document.querySelectorAll(
'.entry-list-wrap .entry-list .entry, .entry-list-wrap .entry-list'
);
if (elements) {
setColor(elements);
}
}
function setLightColor() {
document.querySelector('body').classList.remove('blackBackground');
document.querySelector('#juejin header') &&
document
.querySelector('#juejin header')
.classList.remove('blackBackground');
document.querySelector('#juejin header.list-header') &&
document.querySelector('#juejin header.list-header').classList.remove('blackBackground');
if (observerObject) {
observerObject.disconnect();
}
observeDom();
const elements = document.querySelectorAll(
'.entry-list-wrap .entry-list .entry, .entry-list-wrap .entry-list'
);
if (elements) {
setColor(elements);
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
sendResponse('得到了结果');
const { theme } = request;
switchTheme(theme);
});
function init() {
chrome.runtime.sendMessage({}, (res) => {
const { theme } = res;
switchTheme(theme);
});
}
init();
function switchTheme(reqTheme) {
theme = reqTheme;
if (theme == 'dark') {
setDarkColor();
} else {
setLightColor();
}
}