forked from nickbaum/simple-window-saver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.js
80 lines (66 loc) · 2.01 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
/* BADGE FUNCTIONS */
// Because chrome doesn't support per-window badges, we maintain it per-tab
// and update it on both window and tab selection changes
// used to count how many times we show the update message
var updateMsgCount = restoreFromLocalStorage("updateMsgCount", 0);
// if the extension has been updated, show the update message 5 times
if (!localStorage.version) {
localStorage.version = "1.3";
}
if (parseFloat(localStorage.version) < 1.3) {
updateMsgCount = 5;
localStorage.version = "1.3";
}
if (updateMsgCount > 0) {
updateBadgeForAllWindows();
}
// updates the browserAction badge to show the window as saved
function showSavedBadge(tabId, w) {
var text;
if (updateMsgCount > 0) {
text = "new!";
} else {
text = "" + w.tabs.length;
}
chrome.browserAction.setBadgeText({text:text, tabId:tabId});
chrome.browserAction.setBadgeBackgroundColor(
{color:[0,255,0,255], tabId:tabId});
}
// updates the browserAction badge to show the window as unsaved
// because chrome doesn't support per-window badges, we do it per-tab
// and update it on both window and tab selection changes
function showUnsavedBadge(tabId) {
var text;
if (updateMsgCount > 0) {
text = "new!";
} else {
text = "";
}
chrome.browserAction.setBadgeText({text:text, tabId:tabId});
chrome.browserAction.setBadgeBackgroundColor(
{color:[102,170,255,255], tabId:tabId});
}
// update the badge for the given tab
function updateBadgeForTab(tab) {
var windowName = windowIdToName[tab.windowId];
if (windowName) {
var w = savedWindows[windowName]
showSavedBadge(tab.id, w);
} else {
showUnsavedBadge(tab.id);
}
}
// update the badge for the given window
function updateBadgeForWindow(windowId) {
if (windowId != -1) {
chrome.tabs.getSelected(windowId, updateBadgeForTab);
}
}
// update the badge for the given window
function updateBadgeForAllWindows() {
chrome.windows.getAll(null, function(windows) {
for (i in windows) {
updateBadgeForWindow(windows[i].id);
}
});
}