-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
266 lines (244 loc) · 9.24 KB
/
Copy pathbackground.js
File metadata and controls
266 lines (244 loc) · 9.24 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"use strict";
const ALARM_TIME = 60; // In seconds
const ALARM_BUFFER = 1.1; // Alarm usually triggers a little late
const IDLE_TIME = 300; // In seconds
let RESET_HOUR = 2;
let RESET_MIN = 20;
/****************** EVENTS *********************/
// Fires when a different/no window is in focus
chrome.windows.onFocusChanged.addListener(async windowId => {
if (windowId === chrome.windows.WINDOW_ID_NONE) {
// If machine is locked, window also goes unfocused. Ignore in this case.
const newState = await promisify(chrome.idle.queryState, IDLE_TIME);
if (newState === "active") {
console.log(`Chrome unfocused!`);
await stopTracking("unfocused");
}
} else {
console.log(`Chrome changed focus!`);
await updateWithCurrentTab();
}
});
// Fires when user switches tab
chrome.tabs.onActivated.addListener(async activeInfo => {
// If window is closing, tab not found and throws error, so ignore.
try {
var tab = await safePromisify(chrome.tabs.get, activeInfo.tabId);
} catch (e) {
return;
}
console.log(`New active tab!`);
try {
const prev = (await safePromisify(chrome.storage.local.get,
["prevTab", "unfocused"]));
const prevTab = prev.prevTab;
const unfocused = prev.unfocused;
if (unfocused) {
console.log("Chrome previously unfocused, handled by other events.");
}
else if (tab.windowId === prevTab.windowId) {
console.log(`Active tab in same window!`);
await updateWithCurrentTab();
} else {
console.log(`Active tab in different window, handled by other events.`);
}
} catch (e) {
console.error(e);
}
});
// Fires when new page is loaded
chrome.webNavigation.onCompleted.addListener(async details => {
// Only occurs if page is fully loaded
if (details.frameId === 0) {
console.log(`Loaded new page!`);
const tab = await promisify(chrome.tabs.get, details.tabId);
const tabWindow = await promisify(chrome.windows.get, tab.windowId);
if (tab.active && tabWindow.focused) {
console.log(`Loaded on focused, active tab!`);
await updateWithCurrentTab();
} else {
console.log(`Loaded elsewhere (new tab, etc.)!`);
console.log(`---------------------------------`);
}
}
});
// Detect if user is idle or not
chrome.idle.onStateChanged.addListener(async newState => {
console.log(`Machine is ${newState}.`);
if (newState !== "active") {
await stopTracking("idle");
} else {
const currentWindow = await promisify(chrome.windows.getCurrent, {});
try {
// Idle event always late. If status still idle, update if necesary.
const idle = (await safePromisify(chrome.storage.local.get,
"idle")).idle;
await safePromisify(chrome.storage.local.remove, "idle");
if (idle && currentWindow.focused) {
await updateWithCurrentTab();
} else {
console.log("Active state handled by other events.");
console.log(`---------------------------------`);
}
} catch (e) {
console.error(e);
}
}
});
// Update time when alarm called
chrome.alarms.onAlarm.addListener(async alarm => {
console.log(`Update from alarm "${alarm.name}" at ${new Date()}`);
if (alarm.name === "update") {
await updateWithCurrentTab();
} else if (alarm.name === "reset") {
const currentWindow = await promisify(chrome.windows.getCurrent, {});
if (currentWindow.focused) {
await updateWithCurrentTab();
}
await resetTracking();
}
});
// Set time interval when user is active.
chrome.runtime.onInstalled.addListener(async details => {
try {
console.log(`Installing...`);
await safePromisify(chrome.storage.local.clear);
await promisify(chrome.alarms.clearAll);
chrome.idle.setDetectionInterval(IDLE_TIME);
const resetAlarm = await promisify(chrome.alarms.get, "reset");
if (!resetAlarm) {
await resetTracking();
}
await updateWithCurrentTab();
} catch (e) {
console.error(e);
}
});
/****************** TIME TRACKING ******************/
async function updateWithCurrentTab() {
try {
await updateTime();
// await trackCurrentTab();
console.log(`Tracking current tab!`);
const currentTab = (await promisify(chrome.tabs.query,
{currentWindow: true, active: true}))[0];;
console.log(`Current url: ${getTLD(currentTab.url)}`);
await safePromisify(chrome.storage.local.set,
{prevStart: new Date().getTime(), prevTab: currentTab});
await safePromisify(chrome.storage.local.remove, ["idle", "unfocused"]);
console.log(`Setting alarm.`);
chrome.alarms.create("update", {"delayInMinutes": ALARM_TIME / 60});
console.log(`---------------------------------`);
} catch (e) {
console.error(e);
}
}
async function stopTracking(newStatus) {
try {
await updateTime();
console.log(`Stopping time tracking.`);
await safePromisify(chrome.storage.local.remove, "prevStart");
if (newStatus === "idle") {
await safePromisify(chrome.storage.local.set, {idle: true});
} else {
await safePromisify(chrome.storage.local.set, {unfocused: true});
await safePromisify(chrome.storage.local.remove, "idle");
}
console.log(`Clearing alarm.`);
console.log(`---------------------------------`);
await promisify(chrome.alarms.clear, "update");
} catch (e) {
console.error(e);
}
}
async function updateTime() {
try {
const prev = await safePromisify(chrome.storage.local.get,
["prevStart", "prevTab"]);
const prevStart = prev.prevStart;
// Don't update if we weren't tracking time
if (!prevStart) {
return;
}
const timeSpent = (new Date().getTime() - prevStart) / 1000;
// Don't add time if it's greater than alarm time (chrome probably closed)
if (timeSpent < ALARM_TIME * ALARM_BUFFER) {
const prevURL = getTLD(prev.prevTab.url);
let timeData = (await safePromisify(chrome.storage.local.get,
"timeData")).timeData;
let todayData = timeData[timeData.length - 1];
let prevURLTime = todayData[prevURL];
if (!prevURLTime) {
prevURLTime = 0;
}
console.log(`Updating website time!`);
console.log(`Prev url: ${prevURL}`);
console.log(`Time just spent on prev URL: ${timeSpent}`);
todayData[prevURL] = prevURLTime + timeSpent;
await safePromisify(chrome.storage.local.set, {timeData: timeData});
}
} catch (e) {
console.error(e);
}
}
async function resetTracking() {
try {
console.log(`Resetting time tracking!`);
const now = new Date();
// const resetDate = new Date(now.getFullYear(), now.getMonth(),
// now.getDate() + 1, RESET_HOUR, RESET_MIN);
// TEMP LINES
const resetDate = new Date(now.getFullYear(), now.getMonth(),
now.getDate(), RESET_HOUR, RESET_MIN);
RESET_MIN++;
console.log(`New reset timer at ${resetDate}.`);
chrome.alarms.create("reset", {when: resetDate.getTime()});
let timeData = (await safePromisify(chrome.storage.local.get,
"timeData")).timeData;
if (!timeData) {
timeData = [{startTime: now.getTime(), endTime: resetDate.getTime()}];
} else {
timeData.push({startTime: timeData[timeData.length - 1].endTime,
endTime: resetDate.getTime()});
}
await safePromisify(chrome.storage.local.set, {timeData: timeData});
console.log(`---------------------------------`);
} catch (e) {
console.error(e);
}
}
/**************** HELPER/DEBUGGER FUNCTIONS ******************/
chrome.storage.onChanged.addListener((changes, areaName) => {
for (const i in changes) {
console.log(`Key: ${i} \n` + `oldValue: ${changes[i].oldValue} \n` +
`newValue: ${changes[i].newValue}`);
}
});
// Get top level domain name
// E.g. http://store.google.com/xyz -> http://store.google.com
function getTLD(thisURL) {
const regex = /^(\S+?:\/\/[^\/]+).*$/;
return thisURL.match(regex)[1];
}
// Converts ordinary callbacks w/ no errors to promise
function promisify(op, arg) {
return new Promise((resolve, reject) => {
if (arg) {
op(arg, retVal => resolve(retVal));
} else {
op(retVal => resolve(retVal));
}
});
}
// Performs the memory op with the given arg and checks that it succeeded
function safePromisify(op, arg) {
return new Promise((resolve, reject) => {
if (arg) {
op(arg, retVal => chrome.runtime.lastError ?
reject(chrome.runtime.lastError) : resolve(retVal));
} else {
op(retVal => chrome.runtime.lastError ?
reject(chrome.runtime.lastError) : resolve(retVal));
}
});
}