-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggle.js
60 lines (48 loc) · 1.29 KB
/
toggle.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
54
55
56
57
58
59
60
var activeOnTabs = {};
function changeIconTo(tab, state) {
var icon = {};
if (state == 'active') {
icon = {
'19': 'images/active19.png',
'38': 'images/active38.png'
}
} else {
icon = {
'19': 'images/inactive19.png',
'38': 'images/inactive38.png'
};
}
chrome.browserAction.setIcon({
path: icon,
tabId: tab.id
});
}
function activateOnTab(tab) {
chrome.tabs.sendMessage(tab.id, {action: "swap-images"});
changeIconTo(tab, 'active');
activeOnTabs[tab.id] = true;
}
function deactivateOnTab(tab) {
chrome.tabs.sendMessage(tab.id, {action: "undo-swap-images"});
changeIconTo(tab, 'inactive');
delete activeOnTabs[tab.id];
}
chrome.browserAction.onClicked.addListener(function(tab) {
if (activeOnTabs[tab.id]) {
deactivateOnTab(tab);
} else {
activateOnTab(tab);
}
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (!request) { return; }
var tab = sender.tab;
if (request.action == 'init') {
if (activeOnTabs[tab.id]) {
changeIconTo(tab, 'active');
sendResponse({action: 'swap-images'});
return;
}
}
sendResponse({action: 'none'});
});