-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
509 lines (340 loc) · 9.78 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
var URLDATA;
var hitCount;
/**
* todo:Switch the specification of URL matching to check more than the Root domain.
*/
/**
* Runs whenever Chrome first starts or whenever the extension is reloaded.
*/
chrome.runtime.onInstalled.addListener(
function () {
console.log("Tab Tracker background page starting");
chrome.storage.local.get("URLS", function (callback) {
if (callback.URLS != null) {
URLDATA = callback.URLS;
} else {
console.log("NO DATA FOUND");
}
chrome.browserAction.setBadgeBackgroundColor({color: "#000000"});
calculateHits();
})
}
);
/**
* Listens for any time when a tab is updated within in the Chrome instance
*/
chrome.tabs.onUpdated.addListener(
function (tabId, changeInfo, tab) {
//Only register events that are completed
if (changeInfo.status == "complete") {
var currentURL = new URI(tab.url);
currentURL = currentURL.host();
console.log(currentURL);
fetchSyncedURLS(function (data) {
if (data != null) {
for (i = 0; i < data.length; i++) {
if (data[i].url == currentURL) {
data[i].hits++;
if (data[i].dates == null) {
data[i].dates = [Date.now()]
} else {
data[i].dates.push(Date.now());
}
URLDATA = data;
syncURLS();
calculateHits();
}
}
}
})
}
}
);
/**
* Pulls the root domain and prefix from a URL and returns it
* @param inputUrl
* @returns {*}
*/
function getURI(inputUrl) {
return new URI(inputUrl);
}
/**
* urlObject container for information about user data related to a specific, blocked URL that they have
* specified.
* @param url
*/
function urlObject(url) {
this.hits = 0;
d = new Date();
this.dates = [];
this.startDate = d.valueOf();
if (url != null) {
this.url = url;
} else {
this.url = "www.facebook.com";
}
}
/**
* Adds the URL to the list of URLS that are actively being tracked by the
* application.
* @param url
* @param callback, Function to be fired after the new URL object is created
* used by the options menu to display a new card using the
* newly created URL object without reload.
*
*/
function addURL(url, callback) {
//CHECK IF URL IS ALREADY INCLUDED
//IF NOT, INCLUDE IT IN THE URL
var URL_OBJ = new urlObject(url);
if (callback != null) {
callback(URL_OBJ);
}
if (URLDATA != null) {
console.log("Adding new object to URLDATA");
URLDATA.push(URL_OBJ);
} else {
console.log("Creating a new URLDATA array");
URLDATA = [URL_OBJ];
}
syncURLS();
}
/**
* Returns whether the app is currently tracking a given URL
*
* @param url
*/
function checkIfTracking(URL) {
}
/**
* Pushes the local URL data to the server
* @param localURLS
*/
function syncURLS() {
console.log("SYNCING DATA. . . ");
chrome.storage.local.set({"URLS": URLDATA}, function (callback) {
console.log("SYNCED ");
chrome.storage.local.get("URLS", function (callback) {
URLDATA == callback.URLS;
calculateHits();
})
})
}
/**
* Pulls synced URLS from the server. To use inside of the
* options page, the page passes through the field that it
* wants the updated values to be assigned to.
*
* @param callback
*/
function fetchSyncedURLS(callback) {
chrome.storage.local.get("URLS", function (data) {
URLDATA = data.URLS;
callback(data.URLS);
})
}
/**
* List tracked URLS
*/
function listURLS() {
fetchSyncedURLS(
function (URLS) {
for (i = 0; i < URLS.length; i++) {
console.log(URLS[i]);
}
}
)
}
function buildHitData(URL, callback) {
return fetchSyncedURLS(function (data) {
var sortedHits;
var hits;
contains = false;
/**
* Fetches the current URL's data from the cloud storage
*/
for (i = 0; i < data.length; i++) {
if (data[i].url == URL) {
hits = data[i].dates;
contains = true;
break;
}
}
if (!contains) {
return null;
}
comparator = new Date(hits[0]);
comparatorHitCount = 1;
/**
* Strip all time data less than hour
*/
comparator.setMinutes(00, 00, 00);
/**
* Builds an array of blank hours to avoid scaling the browsing history in the graph
*/
var between = [];
var date1 = new Date(hits[0]);
date1.setMinutes(00, 00, 00);
var date2 = new Date(hits[hits.length - 1]);
date2.setMinutes(00, 00, 00);
while (date1.valueOf() < date2.valueOf()) {
date1.setHours(date1.getHours() + 1);
between.push(new Date(date1.valueOf()));
}
/**
* Group hit data into hours
*/
for (i = 1; i < hits.length; i++) {
current = new Date(hits[i]);
current.setMinutes(00, 00, 00);
if (current.getTime() == comparator.getTime()) {
comparatorHitCount++;
} else {
bucket = {};
bucket.hits = comparatorHitCount;
bucket.date = comparator;
comparatorHitCount = 1;
comparator = current;
if (sortedHits == null) {
sortedHits = [bucket];
} else {
sortedHits.push(bucket);
}
}
}
bucket = {};
bucket.hits = comparatorHitCount;
bucket.date = comparator;
if (sortedHits == null) {
sortedHits = [bucket];
} else {
sortedHits.push(bucket);
}
/**
* Weave together the blank and sorted legitimate data
*/
finalData = [sortedHits[0]];
j = 1;
for (i = 0; i < between.length; i++) {
if (between[i].valueOf() == sortedHits[j].date.valueOf()) {
finalData.push(sortedHits[j]);
j++;
} else {
bucket = {};
bucket.hits = 0;
bucket.date = between[i];
finalData.push(bucket);
}
}
lastDate = finalData[finalData.length-1].date;
currentDate = new Date();
currentDate.setMinutes(0,0,0);
if( !(currentDate.getTime() == lastDate.getTime()) ) {
bucket = {};
bucket.hits = 0;
bucket.date = currentDate;
finalData.push(bucket);
}
callback(finalData);
})
}
/**
* Accepts a specific URL and resets it's counter
* @param URL
*/
function resetURL(URL) {
fetchSyncedURLS(function (data) {
for (i = 0; i < data.length; i++) {
if (data[i].url == URL) {
data[1].hits = 0;
data[1].startDate = new Date().valueOf();
URLDATA = data;
syncURLS();
}
}
})
}
/**
* Accepts a specific URL and removes it from the database of tracked URLS
* @param URL
*/
function removeURL(URL) {
fetchSyncedURLS(function (data) {
for (i = 0; i < data.length; i++) {
if (data[i].url == URL) {
data.splice(i, 1);
URLDATA = data;
syncURLS();
calculateHits()
}
}
})
}
/**
* Flushes all of the current URLS from the local and cloud storage
*
*/
function resetURLS() {
URLDATA = null;
chrome.storage.local.remove("URLS");
calculateHits();
}
/**
* Iterates over the hit counts for all tracked URLS and updates the badge view
* for the browser action.
*/
function calculateHits() {
hitCount = 0;
if (URLDATA != null) {
for (i = 0; i < URLDATA.length; i++) {
hitCount += URLDATA[i].hits;
}
}
chrome.browserAction.setBadgeText({text: hitCount.toString()});
if (false ) {
chrome.notifications.create("Tab Tracker", {
type: "basic",
iconUrl: "notification.png",
title: "Friendly Reminder",
message: "You've visited a blocked site " + hitCount.toString() + " times."
}, function () {
console.log("Notification Created")
});
}
}
function simulateData() {
var sampleURL = new urlObject("www.reddit.com");
var startDate = new Date(1422750343000);
var current = new Date();
sampleURL.startDate = startDate.valueOf();
startDate.setMinutes(00 , 00 , 00);
current.setMinutes( 00 , 00 , 00);
while (startDate.valueOf() != current.valueOf()) {
startDate.setMinutes( startDate.getMinutes() + 1);
if(Math.random() > .97) {
sampleURL.dates.push(startDate.valueOf());
}
}
sampleURL.hits = sampleURL.dates.length;
URLDATA.push(sampleURL);
console.log(sampleURL);
syncURLS();
}
/**
* Exclude the protocol from the String URL
* @param url
*/
function checkIfTracking(url, callback){
fetchSyncedURLS(function (d) {
tracking = false;
if (d != null) {
for (i = 0; i < d.length; i++) {
if (url == d[i].url) {
tracking = true;
break;
}
}
}
callback(tracking);
})
}