-
Notifications
You must be signed in to change notification settings - Fork 0
/
background1.js
65 lines (58 loc) · 1.82 KB
/
background1.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
console.log('this is background1 6969');
// console.log('打印chrome.action', chrome.action);
console.log('加载插件localStorage22');
let localStorageData;
chrome.runtime.onInstalled.addListener(function () {
console.log('插件安装了');
setThemeMode('light');
chrome.storage.local.get(['theme'], function (res) {
console.log('缓存的res', res.theme);
localStorageData = res.theme;
});
listenIconClick()
// changeTabTheme()
});
chrome.runtime.onMessage.addListener((req,sender, sendResponse) => {
sendResponse({theme: localStorageData})
})
function changeTabTheme(){
chrome.tabs.query({},
(tabs) => {
for (var i = 0; i < tabs.length; i++) {
console.log('获取url', tabs[i].url);
try {
const location = new URL(tabs[i].url)
const host = location.host
if (host.includes('juejin.cn')) {
let message = {
theme: localStorageData,
};
chrome.tabs.sendMessage(tabs[i].id, message, (res) => {
console.log('background=>content');
console.log(res);
});
}
}
catch (e) {
console.log('报错',e);
}
}
}
);
}
function listenIconClick() {
console.log('获取 localStorageData', localStorageData);
chrome.action.onClicked.addListener(function () {
console.log('逻辑中 localStorageData', localStorageData);
localStorageData = localStorageData == 'light' ? 'dark' : 'light';
setThemeMode(localStorageData);
chrome.action.setIcon({ path: 'icons/' + localStorageData + '.png' });
// chrome.action.setIcon({ imageData:{'16': 'ww'} });
changeTabTheme()
});
}
function setThemeMode(mode) {
chrome.storage.local.set({ theme: mode }, function(){
console.log('设置了theme', mode);
});
}