-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracker.js
93 lines (90 loc) · 2.92 KB
/
tracker.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
84
85
86
87
88
89
90
91
92
93
/**
* Responsible for detecting focus change from tabs and windows.
*/
var startDate=new Date();
function Tracker(config, sites) {
this._sites = sites;
var self = this;
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
// console.log("Trying1");
// This tab has updated, but it may not be on focus.
// It is more reliable to request the current tab URL.
self._updateTimeWithCurrentTab();
}
);
chrome.tabs.onActivated.addListener(
function(activeInfo) {
chrome.tabs.get(activeInfo.tabId, function(tab) {
// console.log("Trying2");
// console.log(self);
// upload(tab.url,)
self._sites.setCurrentFocus(tab.url);
});
}
);
chrome.windows.onFocusChanged.addListener(
function(windowId) {
if (windowId == chrome.windows.WINDOW_ID_NONE) {
self._sites.setCurrentFocus(null);
return;
}
// console.log("Trying3"+document.title);
self._updateTimeWithCurrentTab();
}
);
chrome.idle.onStateChanged.addListener(function(idleState) {
if (idleState == "active") {
config.idle = false;
// console.log("Trying4"+document.title);
self._updateTimeWithCurrentTab();
} else {
config.idle = true;
// console.log("Trying5");
self._sites.setCurrentFocus(null);
}
});
chrome.alarms.create(
"updateTime",
{periodInMinutes: config.updateTimePeriodMinutes});
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name == "updateTime") {
// These event gets fired on a periodic basis and isn't triggered
// by a user event, like the tabs/windows events. Because of that,
// we need to ensure the user is not idle or we'll track time for
// the current tab forever.
if (!config.idle) {
self._updateTimeWithCurrentTab();
}
// Force a check of the idle state to ensure that we transition
// back from idle to active as soon as possible.
chrome.idle.queryState(60, function(idleState) {
if (idleState == "active") {
config.idle = false;
} else {
config.idle = true;
self._sites.setCurrentFocus(null);
}
});
}
});
}
Tracker.prototype._updateTimeWithCurrentTab = function() {
var self = this;
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) {
if (tabs.length == 1) {
// Is the tab in the currently focused window? If not, assume Chrome
// is out of focus. Although we ask for the lastFocusedWindow, it's
// possible for that window to go out of focus quickly. If we don't do
// this, we risk counting time towards a tab while the user is outside of
// Chrome altogether.
var url = tabs[0].url;
chrome.windows.get(tabs[0].windowId, function(win) {
if (!win.focused) {
url = null;
}
self._sites.setCurrentFocus(url);
});
}
});
};