forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
55 lines (47 loc) · 1.61 KB
/
background.js
File metadata and controls
55 lines (47 loc) · 1.61 KB
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
'use strict';
var tabs = [];
var activity = new Activity();
var storage = new LocalStorage();
function updateSummaryTime() {
setInterval(backgroundCheck, SETTINGS_INTERVAL_CHECK);
}
function updateStorage() {
setInterval(backgroundUpdateStorage, SETTINGS_INTERVAL_SAVE_STORAGE);
}
function backgroundCheck() {
chrome.windows.getLastFocused({ populate: true }, function (currentWindow) {
if (currentWindow.focused) {
var activeTab = currentWindow.tabs.find(t => t.active === true);
if (activeTab !== undefined && activity.isValidPage(activeTab)) {
var activeUrl = activity.extractHostname(activeTab.url);
var tab = activity.getTab(activeUrl);
if (tab === undefined) {
activity.addTab(activeTab);
}
chrome.idle.queryState(SETTINGS_INTERVAL_INACTIVITY, function (state) {
if (state === 'active') {
tab.summaryTime += 1;
chrome.browserAction.setBadgeText({
tabId: activeTab.id,
text: String(convertSummaryTimeToBadgeString(tab.summaryTime))
});
}
});
}
}
});
}
function backgroundUpdateStorage() {
if (tabs.length > 0)
storage.save(tabs);
}
function addListener() {
chrome.tabs.onActivated.addListener(function (info) {
chrome.tabs.get(info.tabId, function (tab) {
activity.addTab(tab);
});
});
}
addListener();
updateSummaryTime();
updateStorage();