-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
53 lines (46 loc) · 1.74 KB
/
background.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
const OLD_FILES_TO_REMOVE = [
"css/main.css",
"css/colors/lavender.css",
"css/colors/lime.css",
"css/colors/none.css",
"css/colors/pink.css",
"css/colors/seafoam.css",
"css/colors/skyblue.css",
"css/colors/tangerine.css"
];
const setThemeColorInitial = async () => {
const items = await chrome.storage.sync.get({
themeColor: 'pink',
useOriginalFont: false
}).catch(e => console.log(e));
if (items.themeColor === "none") {
await chrome.scripting.unregisterContentScripts();
} else {
await chrome.scripting.unregisterContentScripts();
await chrome.scripting.registerContentScripts([
{ id: "main", matches: ['https://twitter.com/*', 'https://x.com/*'], css: ['css/main.css'], runAt: "document_start" },
{ id: "theme-color", matches: ['https://twitter.com/*', 'https://x.com/*'], css: [`css/colors/${items.themeColor}.css`], runAt: "document_start" }
]).catch(e => console.log(e));
}
}
const doSetup = async () => {
const twtTabs = await chrome.tabs.query({ status: 'complete', discarded: false, url: "https://twitter.com/*" }).catch(e => console.log(e));
const xTabs = await chrome.tabs.query({ status: 'complete', discarded: false, url: "https://x.com/*" }).catch(e => console.log(e));
const allTwtTabs = [...twtTabs, ...xTabs]
for (let i = 0; i < allTwtTabs.length; i++) {
const tab = allTwtTabs[i];
await chrome.scripting.removeCSS({
files: OLD_FILES_TO_REMOVE,
target: { tabId: tab.id }
}).catch(e => console.log(e));
}
await setThemeColorInitial();
allTwtTabs.forEach(tab => {
if (tab.active) {
chrome.tabs.update(tab.id, { url: tab.url }).catch(e => console.log(e));
} else {
chrome.tabs.discard(tab.id).catch(e => console.log(e));
}
});
}
doSetup();