forked from insin/control-panel-for-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
45 lines (40 loc) · 1.29 KB
/
background.js
File metadata and controls
45 lines (40 loc) · 1.29 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
const isSafari = location.protocol.startsWith('safari-web-extension:')
const enabledIcons = {
16: 'icons/icon16.png',
32: 'icons/icon32.png',
48: 'icons/icon48.png',
64: 'icons/icon64.png',
96: 'icons/icon96.png',
128: 'icons/icon128.png',
}
const disabledIcons = {
16: 'icons/icon16-disabled.png',
32: 'icons/icon32-disabled.png',
48: 'icons/icon48-disabled.png',
64: 'icons/icon64-disabled.png',
96: 'icons/icon96-disabled.png',
128: 'icons/icon128-disabled.png',
}
function updateToolbarIcon(enabled) {
let title = chrome.i18n.getMessage(enabled ? 'extensionName' : 'extensionNameDisabled')
if (chrome.runtime.getManifest().manifest_version == 3) {
chrome.action.setTitle({title})
if (!isSafari) {
chrome.action.setIcon({path: enabled ? enabledIcons : disabledIcons})
} else {
chrome.action.setBadgeText({text: enabled ? '' : '⏻'})
}
} else {
chrome.browserAction.setTitle({title})
chrome.browserAction.setIcon({path: enabled ? enabledIcons : disabledIcons})
}
}
// Update browser action icon to reflect enabled state
chrome.storage.local.get({enabled: true}, ({enabled}) => {
updateToolbarIcon(enabled)
})
chrome.storage.local.onChanged.addListener((changes) => {
if (changes.enabled) {
updateToolbarIcon(changes.enabled.newValue)
}
})