-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
39 lines (34 loc) · 1.14 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
const ICON_DIRECTORY = './icons/'
const ICON_SHOW = 'show.png'
const ICON_HIDE = 'hide.png'
function updateShowState() {
chrome.tabs.executeScript({file: 'comments.js'})
}
function updateIcon(value) {
let icon = value ? ICON_HIDE : ICON_SHOW
chrome.browserAction.setIcon({path: ICON_DIRECTORY + icon})
}
function updateStorageState(tabId, value) {
chrome.storage.local.set({[tabId]: value}, () => updateIcon(value))
}
function handleMessage(request, sender, sendResponse) {
let action = request.action
let tabId = sender.tab.id.toString()
if (action === 'init') {
updateStorageState(tabId, false)
}
else if (action === 'get') {
chrome.storage.local.get(tabId, result => sendResponse({currentStatus: result[tabId]}))
}
else if (action === 'update') {
let updatedStatus = request.updatedStatus
updateStorageState(tabId, updatedStatus)
}
return true
}
chrome.tabs.onActivated.addListener(activeInfo => {
let activeTabId = activeInfo.tabId.toString()
chrome.storage.local.get(activeTabId, result => updateIcon(result[activeTabId]))
})
chrome.browserAction.onClicked.addListener(updateShowState)
chrome.runtime.onMessage.addListener(handleMessage)