-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
503 lines (329 loc) · 8.63 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
503
/**
* Constants for us to use
**/
var CONSTANTS = {
URL: {
API: 'https://api.passmarked.com',
WEB: 'https://passmarked.com'
},
CACHE: 60,
INSTALL_KEY: 'install',
STYLE: {
CANVAS: [
'cursor: pointer'
].join(';')
}
};
/**
* Sends a "install notice" to the Passmarked servers,
* which is also a welcome page for new users.
**/
function installNotice() {
// check if not already set ...
chrome.storage.sync.get(CONSTANTS.INSTALL_KEY, function(result) {
// and .. ?
if(!result.install) {
// get current timestamp
var now = new Date().getTime();
// params to save
params = {};
// set our initial items
params[CONSTANTS.INSTALL_KEY] = JSON.stringify({
timestamp: now
});
// set as a note
chrome.storage.sync.set(params, function() {
// -- ENABLE TO VIEW THE CONTENT OF THE SYNC DATA STORAGE OBJECT
// chrome.storage.sync.get(null, function (data) { console.info(data) });
// open our welcome page with meta data
chrome.tabs.create({
url: CONSTANTS.URL.WEB + '/welcome?' + [
'client=chrome',
'timestamp=' + now
].join('&')
});
});
}
});
}
/**
* Returns a value from the cache but also checks the
* the timestamp first and clears cache if it's too old
**/
function byCache(key, fn) {
// the timestamp key
var timestamp_key = key + '.timestamp';
// get by the key
chrome.storage.local.get([ key, timestamp_key ], function(result) {
// did we find anything
if(result) {
// current time
var current_timestamp = new Date().getTime();
// check the timestamp
if( result[timestamp_key] &&
( current_timestamp - result[timestamp_key] ) <= 1000 * 60 * CONSTANTS.CACHE ) {
// output our item ..
fn( JSON.parse(result[key]) )
} else {
// remove it
chrome.storage.local.remove([ key, timestamp_key ], function() {
// signal done and that nothing was found from cache ...
fn(null);
});
}
} else fn(null);
});
}
/**
* Sets to the cache along with the timestamp
**/
function setCache(key, value, fn) {
// the timestamp key
var timestamp_key = key + '.timestamp';
// params to set
var params = {};
// add in
params[key] = JSON.stringify(value);
params[timestamp_key] = new Date().getTime();
// get by the key
chrome.storage.local.set(params, function(){
// signal that we are done
fn(null);
});
}
/**
* Returns the score info from the server
* using the cache and everything involved
**/
function getReportedInfo(domain, fn) {
// key for the cache
var cacheKey = domain.toLowerCase();
// key for our local cache
byCache(cacheKey, function(cached_details){
if(cached_details) {
// returns the cached items
fn(cached_details);
} else {
// open a XHR for the request
var xhr = new XMLHttpRequest();
// open up a connection
xhr.open("GET", CONSTANTS.URL.API + '/v1/query?' + [
'source=chrome.ext',
'domain=' + domain
].join('&'), false);
// handle the response
xhr.onreadystatechange = function() {
// handle when done
if (xhr.readyState == 4) {
// get our response
var result = xhr.responseText;
// can we parse it ?
var jsonObj = null;
// try to parse it
try { jsonObj = JSON.parse(result); } catch(err) {}
// could we parse ?
if(jsonObj) {
// save the url
setCache(domain, jsonObj, function(){
// return with info
fn(jsonObj);
});
} else {
// nope ...
fn(null);
}
}
};
// send the request
xhr.send();
}
});
}
/**
* Sets a Blank icon or just makes sure it's showing
**/
function showBlankIcon(tabId) {
// set the icon
chrome.pageAction.setIcon({
tabId: tabId,
path: {
38: 'faces/face.png',
19: 'faces/face.png'
}
}, function(){
// show the page action to the tab this is active on
chrome.pageAction.show(tabId);
});
}
/**
* Sets a Icon that has a score showing
**/
function showScoreIcon(tabId, score) {
// sanity check if we should render blank
// icon for a 'null' score
if(!score) {
// show a blank icon rather then
showBlankIcon(tabId);
return;
}
// pad the score
var paddedScore = '' + Math.floor(score).toString();
if(paddedScore.length == 1)
paddedScore = '0' + paddedScore;
// set the icon
chrome.pageAction.setIcon({
tabId: tabId,
path: {
38: 'faces/' + paddedScore + '.png',
19: 'faces/' + paddedScore + '.png'
}
}, function(){
// show the page action to the tab this is active on
chrome.pageAction.show(tabId);
});
}
/**
* Returns a color according to the score
**/
function colorByScore(score) {
// local variable
var parsedScore = null;
var baseColor = null;
// parse to int
try { parsedScore = parseInt(score); } catch(err) {}
// check we found a score ...
if(!parsedScore) baseColor = 'd44937';
else if(parsedScore < 45) baseColor = 'f72d49';
else if(parsedScore < 70) baseColor = 'f96b25';
else if(parsedScore >= 70) baseColor = '78cbd1';
// returns the color according to score
return colorLuminance(baseColor, -(1 - ( parsedScore/100 )) );
}
/**
* Returns color darker or lighter,
* -0.1 = 10% darker, 0.1 = 10% lighter
**/
function colorLuminance(hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
var rgb = "#", c, i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i*2,2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ("00"+c).substr(c.length);
}
// returns the rgb
return rgb;
}
/**
* Returns a domain by the url given
**/
function getDomainByUrl(url) {
// returns the domain by the full url
return URI(url).hostname();
}
/**
* Decides if we should show the icon on a url
**/
function allowActionByUrl(url) {
// check if it's blank,
// more a sanity check
// than anything else
if(!url) return false;
// the lowered url
url = (url || '').toLowerCase()
// check if this is not a internal chrome page
if( url.indexOf('http://') != 0 &&
url.indexOf('https://') != 0 ) return false;
// check for local host
if(url.indexOf('://localhost') != -1) return false;
// default is true
return true;
}
/**
* Handle a click on the page icon
**/
chrome.pageAction.onClicked.addListener(function(tab){
// handle it
var url = tab.url;
// get the domain
var hostname = getDomainByUrl(url);
// check if not the password result website
if(url.indexOf('passmarked.com') != -1 && url.replace('http:').split('/').length > 4) {
// do not allow the result pages to be checked ..
chrome.tabs.create({
url: CONSTANTS.URL.WEB + '/dawg?' + [
'url=' + encodeURIComponent(url),
'source=chrome.ext'
].join('&')
}, function() {
// cool open, will add here if anything is needed ...
});
} else {
// get all the data
getReportedInfo( hostname, function(report_overview_obj){
// did we find one ?
chrome.tabs.create({
url: CONSTANTS.URL.API + '/v1/redirect?' + [
'url=' + encodeURIComponent(url),
'source=chrome.ext'
].join('&')
}, function() {
// cool open, will add here if anything is needed ...
});
} );
}
});
/**
* Handles when a url is created
**/
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
// check if we should worry
if(!allowActionByUrl(tab.url)) return;
// get the domain
var domain = getDomainByUrl(tab.url);
// check if we have a score for this domain
getReportedInfo(domain, function(result){
// did we find a item for the page ?
if( result && result.count > 0 ) {
// render the actual score we have
showScoreIcon(tabId, result.score);
} else {
// render the blank icon for this page
showBlankIcon(tabId);
}
});
});
/**
* Listens for commands from our website
**/
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
// is this from a website ?
if(sender.tab) {
if(request.command == 'installed') {
// get the install time
chrome.storage.sync.get([ CONSTANTS.INSTALL_KEY ], function(result) {
// remove from storage
sendResponse({ status: "ok", timestamp: result[CONSTANTS.INSTALL_KEY] });
});
} else if(request.command == 'bust') {
// the timestamp key
var timestamp_key = request.key + '.timestamp';
// remove it
chrome.storage.local.remove([ request.key, timestamp_key ], function() {
// signal done and that nothing was found from cache ...
// remove from storage
sendResponse({ status: "ok"});
});
}
}
}
);
// run our install notice
installNotice();