-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer-bg.js
76 lines (64 loc) · 2.08 KB
/
timer-bg.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
/*
* Copyright 2013 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var History = {};
chrome.browserAction.setBadgeText({ 'text': '?'});
chrome.browserAction.setBadgeBackgroundColor({ 'color': "#777" });
function Update(t, tabId, url) {
if (!url) {
return;
}
if (tabId in History) {
if (url == History[tabId][0][1]) {
return;
}
} else {
History[tabId] = [];
}
History[tabId].unshift([t, url]);
var history_limit = parseInt(localStorage["history_size"]);
if (! history_limit) {
history_limit = 23;
}
while (History[tabId].length > history_limit) {
History[tabId].pop();
}
chrome.browserAction.setBadgeText({ 'tabId': tabId, 'text': '0:00'});
chrome.browserAction.setPopup({ 'tabId': tabId, 'popup': "popup.html#tabId=" + tabId});
}
function HandleUpdate(tabId, changeInfo, tab) {
Update(new Date(), tabId, changeInfo.url);
}
function HandleRemove(tabId, removeInfo) {
delete History[tabId];
}
function HandleReplace(addedTabId, removedTabId) {
var t = new Date();
delete History[removedTabId];
chrome.tabs.get(addedTabId, function(tab) {
Update(t, addedTabId, tab.url);
});
}
function UpdateBadges() {
var now = new Date();
for (tabId in History) {
var description = FormatDuration(now - History[tabId][0][0]);
chrome.browserAction.setBadgeText({ 'tabId': parseInt(tabId), 'text': description});
}
}
setInterval(UpdateBadges, 1000);
chrome.tabs.onUpdated.addListener(HandleUpdate);
chrome.tabs.onRemoved.addListener(HandleRemove);
chrome.tabs.onReplaced.addListener(HandleReplace);