-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
62 lines (56 loc) · 1.68 KB
/
background.js
File metadata and controls
62 lines (56 loc) · 1.68 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
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ enabled: true }) // Set default state to enabled
updateIconBasedOnDomain()
})
chrome.runtime.onStartup.addListener(() => {
updateIconBasedOnDomain() // Ensure correct icon on browser startup for all tabs
})
chrome.action.onClicked.addListener(tab => {
chrome.storage.local.get({ enabled: true }, data => {
const newState = !data.enabled
chrome.storage.local.set({ enabled: newState }, () => {
sendMessageWithRetry(tab.id, { enabled: newState })
updateIconForTab(tab) // Update icon based on the new state and tab URL
})
})
})
function sendMessageWithRetry(tabId, message, retryCount = 0) {
chrome.tabs.sendMessage(tabId, message, response => {
if (chrome.runtime.lastError) {
if (retryCount < 3) {
setTimeout(
() => sendMessageWithRetry(tabId, message, retryCount + 1),
1000
)
} else {
console.error(
"Failed to send message after retries:",
chrome.runtime.lastError.message
)
}
return
}
})
}
function updateIconForTab(tab) {
chrome.storage.local.get({ enabled: true }, data => {
const iconPath = tab.url.includes("tiktok.com")
? data.enabled
? "icon-enabled.png"
: "icon-disabled.png"
: "icon-disabled.png"
chrome.action.setIcon({ path: iconPath, tabId: tab.id })
})
}
function updateIconBasedOnDomain() {
chrome.tabs.query({}, tabs => {
tabs.forEach(tab => {
updateIconForTab(tab)
})
})
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete" && tab.url) {
updateIconForTab(tab)
}
})