-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
323 lines (287 loc) · 8.25 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
var pageCount = 0;
var currentTabId = null;
var currentSite = null;
var domain = /^[\w-]+:\/*\[?([\w\.:-]+)\]?(?::\d+)?/;
function getSiteFromUrl(url) {
var match = url.match(domain);
if (match) {
console.log(match[1]);
return match[1];
}
return null;
}
function updateTime(site, seconds) {
// SOCIAL
var loc = site.indexOf('twitter.com');
if(loc != -1){
var val = parseInt(localStorage['twitter']) + parseInt(seconds);
localStorage['twitter'] = val;
}
loc = site.indexOf('facebook.com');
if(loc != -1){
var val = parseInt(localStorage['facebook']) + parseInt(seconds);
localStorage['facebook'] = val;
}
loc = site.indexOf('instagram.com');
if(loc != -1){
var val = parseInt(localStorage['instagram']) + parseInt(seconds);
localStorage['instagram'] = val;
}
// ACADEMIC
loc = site.indexOf('.edu');
if(loc != -1){
var val = parseInt(localStorage['.edu']) + parseInt(seconds);
localStorage['.edu'] = val;
}
loc = site.indexOf('piazza.com');
if(loc != -1){
var val = parseInt(localStorage['piazza']) + parseInt(seconds);
localStorage['piazza'] = val;
}
loc = site.indexOf('wikipedia.org');
if(loc != -1){
var val = parseInt(localStorage['wikipedia']) + parseInt(seconds);
localStorage['wikipedia'] = val;
}
// NEWS
loc = site.indexOf('wsj.com');
if(loc != -1){
var val = parseInt(localStorage['WSJ']) + parseInt(seconds);
localStorage['WSJ'] = val;
}
loc = site.indexOf('latimes.com');
if(loc != -1){
var val = parseInt(localStorage['latimes']) + parseInt(seconds);
localStorage['latimes'] = val;
}
loc = site.indexOf('bbc.com');
if(loc != -1){
var val = parseInt(localStorage['bbc']) + parseInt(seconds);
localStorage['bbc'] = val;
}
// SHOPPING
loc = site.indexOf('amazon.com');
if(loc != -1){
var val = parseInt(localStorage['amazon']) + parseInt(seconds);
localStorage['amazon'] = val;
}
loc = site.indexOf('ebay.com');
if(loc != -1){
var val = parseInt(localStorage['ebay']) + parseInt(seconds);
localStorage['ebay'] = val;
}
loc = site.indexOf('gap.com');
if(loc != -1){
var val = parseInt(localStorage['gap']) + parseInt(seconds);
localStorage['gap'] = val;
}
// ENTERTAINMENT JUNKIE
loc = site.indexOf('netflix.com');
if(loc != -1){
var val = parseInt(localStorage['netflix']) + parseInt(seconds);
localStorage['netflix'] = val;
}
loc = site.indexOf('youtube.com');
if(loc != -1){
var val = parseInt(localStorage['youtube']) + parseInt(seconds);
localStorage['youtube'] = val;
}
loc = site.indexOf('hulu.com');
if(loc != -1){
var val = parseInt(localStorage['hulu']) + parseInt(seconds);
localStorage['hulu'] = val;
}
loc = site.indexOf('eonline.com');
if(loc != -1){
var val = parseInt(localStorage['eonline']) + parseInt(seconds);
localStorage['eonline'] = val;
}
loc = site.indexOf('variety');
if(loc != -1){
var val = parseInt(localStorage['variety']) + parseInt(seconds);
localStorage['variety'] = val;
}
// BUSINESS
loc = site.indexOf('chase.com');
if(loc != -1){
var val = parseInt(localStorage['chase']) + parseInt(seconds);
localStorage['chase'] = val;
}
loc = site.indexOf('linkedin.com');
if(loc != -1){
var val = parseInt(localStorage['linkedin']) + parseInt(seconds);
localStorage['linkedin'] = val;
}
loc = site.indexOf('forbes.com');
if(loc != -1){
var val = parseInt(localStorage['forbes']) + parseInt(seconds);
localStorage['forbes'] = val;
}
// INQUISITIVE
loc = site.indexOf('google.com');
if(loc != -1){
var val = parseInt(localStorage['google']) + parseInt(seconds);
localStorage['google'] = val;
}
loc = site.indexOf('yahoo.com');
if(loc != -1){
var val = parseInt(localStorage['yahoo']) + parseInt(seconds);
localStorage['yahoo'] = val;
}
loc = site.indexOf('bing.com');
if(loc != -1){
var val = parseInt(localStorage['bing']) + parseInt(seconds);
localStorage['bing'] = val;
}
loc = site.indexOf('ask.com');
if(loc != -1){
var val = parseInt(localStorage['ask']) + parseInt(seconds);
localStorage['ask'] = val;
}
console.log("Time: " + seconds);
// var sites = JSON.parse(localStorage.sites);
// if (!sites[site]) {
// sites[site] = 0;
// }
// sites[site] = sites[site] + seconds;
// localStorage.sites = JSON.stringify(sites);
}
function updateCounter(){
var tabInt = 0;
if (currentTabId == null) {
return;
}
chrome.tabs.get(currentTabId, function(tab) {
/* Make sure we're on the focused window, otherwise we're recording bogus stats. */
chrome.windows.get( tab.windowId, function(window) {
if (!window.focused) {
return;
}
var site = getSiteFromUrl(tab.url);
if (site == null) {
console.log("Unable to update counter. Malformed url.");
return;
}
/* We can't update any counters if this is the first time visiting any
* site. This happens on browser startup. Initialize some variables so
* we can perform an update next time. */
if (currentSite == null) {
currentSite = site;
startTime = new Date();
return;
}
/* Update the time spent for this site by comparing the current time to
* the last time we were ran. */
var now = new Date();
var delta = now.getTime() - startTime.getTime();
updateTime(currentSite, delta/1000);
/* This function could have been called as the result of a tab change,
* which means the site may have changed. */
currentSite = site;
startTime = now;
});
});
}
function initialize(){
if (!localStorage.sites) {
localStorage.sites = JSON.stringify({});
}
if (localStorage['twitter'] == null){
localStorage['twitter'] = 0;
}
if (localStorage['facebook'] == null){
localStorage['facebook']=0;
}
if (localStorage['instagram'] == null){
localStorage['instagram'] = 0;
}
if (localStorage['.edu'] == null){
localStorage['.edu']=0;
}
if (localStorage['piazza'] == null){
localStorage['piazza'] = 0;
}
if (localStorage['wikipedia'] == null){
localStorage['wikipedia']=0;
}
if (localStorage['wsj'] == null){
localStorage['wsj'] = 0;
}
if (localStorage['latimes'] == null){
localStorage['latimes']=0;
}
if (localStorage['bbc'] == null){
localStorage['bbc'] = 0;
}
if (localStorage['amazon'] == null){
localStorage['amazon']=0;
}
if (localStorage['ebay'] == null){
localStorage['ebay'] = 0;
}
if (localStorage['gap'] == null){
localStorage['gap']=0;
}
if (localStorage['netflix'] == null){
localStorage['netflix'] = 0;
}
if (localStorage['youtube'] == null){
localStorage['youtube']=0;
}
if (localStorage['hulu'] == null){
localStorage['hulu'] = 0;
}
if (localStorage['chase'] == null){
localStorage['chase']=0;
}
if (localStorage['linkedin'] == null){
localStorage['linkedin'] = 0;
}
if (localStorage['forbes'] == null){
localStorage['forbes']=0;
}
if (localStorage['google'] == null){
localStorage['google'] = 0;
}
if (localStorage['yahoo'] == null){
localStorage['yahoo']=0;
if (localStorage['bing'] == null){
localStorage['bing']=0;
}
if (localStorage['ask'] == null){
localStorage['ask']=0;
}
if (localStorage['eonline'] == null){
localStorage['eonline'] = 0;
}
if (localStorage['variety'] == null){
localStorage['variety'] = 0;
}
}
chrome.tabs.onActivated.addListener(
function(tab) {
// console.log("Tab changed");
currentTabId = tab.tabId;
updateCounter();
});
chrome.windows.onFocusChanged.addListener(
function(windowId) {
if(windowId != null){
// console.log("Detected window focus changed.");
chrome.tabs.getSelected(windowId,
function(tab) {
// console.log("Window/Tab changed");
currentTabId = tab.id;
updateCounter();
});
}
});
}
document.addEventListener('DOMContentLoaded', function () {
initialize();
});
/*Add this to popup.js
var b = localStorage['bing'];
var ask = localStorage['ask'];
var eon = localStorage['eonline'];
var v = localStorage['variety'];*/