-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
80 lines (71 loc) · 2.34 KB
/
background.js
File metadata and controls
80 lines (71 loc) · 2.34 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
const ICON_JS_ENABLED = "icon-js-enabled.png"
const ICON_JS_DISABLED = "icon-js-disabled.png"
// get current javascript setting for the given url
async function getJsSettingForUrl(url) {
return chrome.contentSettings.javascript.get({ primaryUrl: url })
}
// update javascript setting for a given domain pattern
async function setJsSettingForPattern(pattern, setting) {
return chrome.contentSettings.javascript.set({
primaryPattern: pattern,
setting
})
}
// update icon for a tab based on current javascript setting
async function updateTabIcon(tabId, tabUrl) {
// handle http and https urls only
try {
const url = new URL(tabUrl)
if (url.protocol !== "http:" && url.protocol !== "https:") return
} catch {
return
}
// set correct icon based on current javascript permission
try {
const details = await getJsSettingForUrl(tabUrl)
const isEnabled = details.setting !== "block"
await chrome.action.setIcon({
tabId,
path: isEnabled ? ICON_JS_ENABLED : ICON_JS_DISABLED
})
} catch (error) {
console.error("error updating javascript icon:", error)
}
}
// toggle javascript on click for the active tab
chrome.action.onClicked.addListener(async (tab) => {
if (!tab || !tab.url || tab.id == null) return
// extract domain from http and https urls only
let domain
try {
const url = new URL(tab.url)
if (url.protocol !== "http:" && url.protocol !== "https:") return
domain = url.hostname
} catch {
return
}
// toggle javascript permission and reload tab
try {
const details = await getJsSettingForUrl(tab.url)
const isEnabled = details.setting !== "block"
await setJsSettingForPattern(`*://${domain}/*`, isEnabled ? "block" : "allow")
chrome.tabs.reload(tab.id)
} catch (error) {
console.error("error toggling javascript:", error)
}
})
// update icon when tab finishes loading
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status !== "complete" || !tab || !tab.url) return
await updateTabIcon(tabId, tab.url)
})
// update icon when switching tabs
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
try {
const tab = await chrome.tabs.get(tabId)
if (!tab || !tab.url) return
await updateTabIcon(tabId, tab.url)
} catch (error) {
console.error("error updating icon on tab activation:", error)
}
})