-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbadge.js
83 lines (67 loc) · 2.17 KB
/
badge.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
(c) Copyright 2012 iOpus Software GmbH - http://www.iopus.com
*/
// Handy wrapper for browser action functions
// (badge is not really good naming for the object)
var badge = {
// execute callback for all tabs in window
// callback is function(tab) {...}
forAllTabs: function(win_id, callback) {
// for some stupid reason windows.get(win) does not
// contain "tabs" property, so we have to get All windows
chrome.windows.getAll({populate: true}, function(ws) {
ws.forEach(function(win) {
if (win.id == win_id) {
win.tabs.forEach(function(tab) {
callback(tab);
});
return;
}
});
});
},
setBackgroundColor: function(win_id, color) {
this.forAllTabs(win_id, function(tab) {
chrome.browserAction.setBadgeBackgroundColor(
{tabId: tab.id, color: color}
);
});
},
setText: function(win_id, text) {
this.forAllTabs(win_id, function(tab) {
chrome.browserAction.setBadgeText(
{tabId: tab.id, text: text}
);
});
},
setIcon: function(win_id, icon) {
this.forAllTabs(win_id, function(tab) {
chrome.browserAction.setIcon(
{tabId: tab.id, path: icon}
);
});
},
set: function(win_id, details) {
switch (details.status) {
case "tag_wait":
this.setBackgroundColor(win_id, [255,100,100,200]);
break;
case "loading":
this.setBackgroundColor(win_id, [255,100,100,200]);
break;
case "waiting":
this.setBackgroundColor(win_id, [100,255,100,200]);
break;
case "playing":
this.setBackgroundColor(win_id, [100,100,255,200]);
break;
case "recording":
this.setBackgroundColor(win_id, [255,100,100,200]);
break;
};
this.setText(win_id, details.text.toString());
},
clearText: function(win_id) {
this.setText(win_id, "");
}
};