forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebTimer.js
More file actions
66 lines (55 loc) · 1.83 KB
/
webTimer.js
File metadata and controls
66 lines (55 loc) · 1.83 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
var WebTimer = function () { };
WebTimer.prototype = {
tabs: [],
currentTab: {},
addTab: function (tab) {
if (this.isValidPage(tab) === true) {
if (tab.id && (tab.id != 0)) {
this.tabs = this.tabs || [];
var domain = this.extractHostname(tab.url);
if (this.isNewUrl(domain)) {
var newTab = new Tab(domain, tab.favIconUrl);
this.tabs.push(newTab);
}
this.startTimeTracker(this.getTab(domain));
this.currentTab = domain;
this.stopTimeTracker(this.getTab(this.currentTab));
}
}
},
isNewUrl: function (domain) {
if (this.tabs.length > 0)
return this.tabs.find(o => o.url === domain) === undefined;
else return true;
},
getTab: function (domain) {
return this.tabs.find(o => o.url === 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;
},
startTimeTracker: function (tab) {
tab.start();
chrome.storage.sync.set({'tabs': JSON.stringify(this.tabs)});
},
stopTimeTracker: function (tab) {
tab.stop();
chrome.storage.sync.set({'tabs': JSON.stringify(this.tabs)});
}
};