forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
66 lines (56 loc) · 1.82 KB
/
background.js
File metadata and controls
66 lines (56 loc) · 1.82 KB
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
window.onload = function () {
window.timer = new WebTimer();
chrome.tabs.onActivated.addListener(function (info) {
chrome.tabs.get(info.tabId, function (tab) {
window.timer.addTab(tab);
});
});
};
var WebTimer = function () { };
WebTimer.prototype = {
tabs: [],
activeTab: {},
addTab: function (tab) {
if (this.isValidPage(tab) === true) {
if (tab.id && (tab.id != 0)) {
this.tabs = this.tabs || [];
var domain = this.extractRootDomain(tab.url);
if (!this.tabs.includes(domain)){
this.tabs.push(domain);
}
this.activeTab = domain;
}
}
},
extractRootDomain: function (url) {
var domain = this.extractHostname(url),
splitArr = domain.split('.'),
arrLen = splitArr.length;
if (arrLen > 2) {
domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) {
domain = splitArr[arrLen - 3] + '.' + domain;
}
}
return domain;
},
extractHostname: function(url) {
var hostname;
if (url.indexOf("//") > -1) {
hostname = url.split('/')[2];
}
else {
hostname = url.split('/')[0];
}
hostname = hostname.split(':')[0];
hostname = hostname.split('?')[0];
return hostname;
},
isValidPage: function (tab) {
if (!tab || !tab.url || (tab.url.indexOf('http:') == -1 && tab.url.indexOf('https:') == -1)
|| tab.url.indexOf('chrome://') !== -1
|| tab.url.indexOf('chrome-extension://') !== -1)
return false;
return true;
}
};