-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
326 lines (290 loc) · 9.17 KB
/
background.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
** The core backend script.
** Involves several event handlers to update the status of the timer.
** Receives messages from options.js and websites.js to update user-defined options.
** Sends messages to hide.js to hide YouTube elements
*/
/*****************
URL MATCHING REGEX
*****************/
/* A regular expression that matches websites under the YouTube domain. */
var youtubeURL = /^https?:\/\/(?:[^./?#]+\.)?youtube\.com/;
/* A regular expression that matches the YouTube home page. */
var youtubeHome = /^https?:\/\/(?:[^./?#]+\.)?youtube\.com\/$/;
/* A regular expression that matches the YouTube trending page. */
var youtubeTrending = /^https?:\/\/(?:[^./?#]+\.)?youtube\.com\/feed\/trending/;
/* A regular expression that matches the YouTube subscription page. */
var subsPage = /^https?:\/\/(?:[^./?#]+\.)?youtube\.com\/feed\/subscriptions/;
/* A regular expression that matches a YouTube video page. */
var videoPage = /^https?:\/\/(?:[^./?#]+\.)?youtube\.com\/watch/;
/* Given a string, return the string with escaped characters for regex purposes. */
function escape(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/***************
WEBSITE MATCHING
***************/
let websites = [];
/* Gets all the websites that need to be tracked shown at websites.html. */
function getWebsites() {
chrome.storage.sync.get("websites", (data) => {
websites = data["websites"];
websites.forEach((item, i) => {
websites[i] = new RegExp(escape(websites[i]));
});
})
}
getWebsites();
/************
USER SETTINGS
************/
/* These values may be set by the user in the extension's options.
** Settings are updated when receiving a message from options.js or websites.js
*/
/* Whether or not to hide recommended videos. */
let hideRecc = true;
/* Whether or not to hide sideBar videos. */
let hideSidebar = true;
/* Whether or not to show the time on the badge. */
let showNumber = false;
/* Whether or not to show notifications. */
let showNotifications = true;
/* The number of minutes between each notification. */
let minutesAlertInterval = 60;
/* Whether or not notifications should be silent. */
let silent = true;
/* Update the internal options based on user settings. */
function updateSettings() {
chrome.storage.sync.get(["hideRecc", "hideSidebar", "showNumber", "showNotifications", "silent"],
(data) => {
hideRecc = data.hideRecc;
hideSidebar = data.hideSidebar;
showNumber = data.showNumber;
showNotifications = data.showNotifications;
silent = data.silent;
getWebsites();
})
}
updateSettings();
/**************
EVENT LISTENERS
**************/
/* Receives "savedOptions" from options.js and websites.js .*/
chrome.runtime.onMessage.addListener(messageReceived);
function messageReceived(request, sender, sendResponse) {
if (request.txt === "savedOptions") {
updateSettings();
updateBadge();
}
}
/* Listen for page changes within the YouTube domain.
** Sends a message to clean YouTube if on a YouTube website.
*/
chrome.webNavigation.onHistoryStateUpdated.addListener(historyUpdated);
function historyUpdated(historyDetails) {
cleanYouTubeMessage(historyDetails.url, historyDetails.tabId);
}
/* Listen for when a tab is updated (ex: changed URL).
** Send a cleanYouTubeMessage if necessary and update the timer state.
*/
chrome.tabs.onUpdated.addListener(tabUpdated);
function tabUpdated(tabId, changeInfo, tab) {
if (changeInfo.status === "complete") {
cleanYouTubeMessage(tab.url, tabId);
updateTimerState(tab.url);
}
}
/* Listen for when the active tab is changed. */
chrome.tabs.onActivated.addListener(tabActivated);
/* Fired when the active tab is changed.
** Update the timer state accordingly.
*/
function tabActivated(activeInfo) {
chrome.tabs.get(activeInfo.tabId, (tab) => {
updateTimerState(tab.url);
});
}
/* Listen for when a window is closed and stop the timer. */
chrome.windows.onRemoved.addListener(windowClosed);
function windowClosed(windowId) {
stopTimer();
if (windowFocusInterval != null) {
clearInterval(windowFocusInterval);
}
}
/*****************
WINDOW FOCUS CHECK
*****************/
/* Continuously check whether there is a chrome window currently being focused
** If so, update the timer state
** Otherwise, stop the timer.
*/
let windowFocusInterval = setInterval(checkFocus, 1000);
function checkFocus() {
chrome.windows.getCurrent(windowChange);
}
/* When switching between chrome windows, update the timer status as necessary.
** windowId is the id of the newly-focused window.
** -1 represents a "null" chrome window.
*/
chrome.windows.onFocusChanged.addListener(focusChanged);
function focusChanged(windowId) {
if (windowId != -1) {
chrome.windows.get(windowId, windowChange);
}
}
/* If there is no browser focused, stop the timer if it is running.
** Otherwise if there IS a browser focused, update the timer state.
*/
function windowChange(window) {
if (timer.running && !window.focused) { // Switched off chromes
stopTimer();
} else if (window.focused) { // Switching to a chrome
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) {
updateTimerState(tabs[0].url);
});
}
}
/****************************
CORE CONTENT SCRIPT MESSENGER
****************************/
/* Sends the appropriate message to hide.js for cleaning YouTube if url matches. */
function cleanYouTubeMessage(url, tabId) {
if (hideSidebar && videoPage.test(url)) {
chrome.tabs.sendMessage(tabId, {txt: "cleanSidebar"});
} else if (hideRecc && (youtubeHome.test(url) || youtubeTrending.test(url))) {
chrome.tabs.sendMessage(tabId, {txt: "cleanRecc"});
}
}
/***********************
CORE TIMER FUNCTIONALITY
***********************/
/* An object that keeps track of the states of the timer
** running: A boolean value indicating whether the timer is running.
** timeSpent: An int value in seconds
** interval: A variable to hold intervals to increment the timeSpent
*/
let timer = {
running : false,
timeSpent : 0,
interval : null
};
/** Update the timer to the saved time. */
chrome.storage.sync.get(today(), (result) => {
timer.timeSpent = result[today()] || 0;});
/** Return a string form of today's date based on the user's OS timezone.
** Ex: January 7, 2020 === x200107. */
function today() {
let date = new Date();
let m = date.getMonth() + 1;
m = m.toString();
if (m.length == 1) {
m = "0" + m;
}
let d = date.getDate().toString();
if (d.length == 1) {
d = "0" + d;
}
let y = date.getFullYear().toString();
y = y.substring(2);
return "x" + y + m + d;
}
/** Stores the timer state for later retrieval in the form
** day : timeSpent */
function storeTimer() {
let date = today();
chrome.storage.sync.get(today(), (result) => {
if (result[date] === undefined) {
clean();
}
});
chrome.storage.sync.set({[date]: timer.timeSpent});
}
/** Stops or starts the timer based on the current URL. */
function updateTimerState(url) {
for (website of websites) {
if (website.test(url)) {
startTimer();
return;
}
}
stopTimer();
}
/** Starts the timer. Running is set to true and timeSpent periodically increases. */
function startTimer() {
if (timer.running == false) {
timer.running = true;
chrome.browserAction.setBadgeBackgroundColor({color: "#FF0333"}); // Red
timer.interval = setInterval(runContinuous, 1000);
function runContinuous() {
// Reset the timer if running through midnight
chrome.storage.sync.get(today(), (result) => {
if (result[today()] === undefined) {
clean();
timer.timeSpent = 0;
storeTimer();
}
});
timer.timeSpent += 1;
if (timer.timeSpent % 10 == 0) {
storeTimer();
}
updateBadge();
}
}
}
/** Stops the timer and stores it. */
function stopTimer() {
if (timer.running) {
storeTimer();
timer.running = false;
chrome.browserAction.setBadgeBackgroundColor({color: "#AAAAAA"}); // Grey
clearInterval(timer.interval);
}
}
/** Updates the extension badge number. */
function updateBadge() {
let timeSpentMinutes = timer.timeSpent / 60;
let n = Math.floor(timeSpentMinutes);
if (showNumber) {
chrome.browserAction.setBadgeText({text: n.toString()});
} else {
chrome.browserAction.setBadgeText({text: ""});
}
if (timer.timeSpent != 0 && timeSpentMinutes % minutesAlertInterval == 0) {
notify(`You've spent ${timeSpentMinutes} minutes unproductively today.`);
}
}
/** Sends a notification to the user with string S. */
function notify(s) {
if (!showNotifications) {
return;
}
var options = {
type : "basic",
title : "tracker.pro",
message : s,
iconUrl : "icons/icon128.png",
silent : silent
}
chrome.notifications.create(options);
}
/******************
STORAGE MAINTENANCE
******************/
/* Tracked data older than a year will be deleted. */
function clean() {
chrome.storage.sync.get(null, (data) => {
var allKeys = Object.keys(data);
let now = parseInt(today().substring(1));
for (key of allKeys) {
if (key.charAt(0) == 'x') {
let date = parseInt(key.substring(1));
if (now - date > 10000) {
chrome.storage.sync.remove(key);
}
}
}
});
}
clean();