forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity.js
More file actions
132 lines (114 loc) · 4.12 KB
/
activity.js
File metadata and controls
132 lines (114 loc) · 4.12 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
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
'use strict';
class Activity {
addTab(tab) {
if (this.isValidPage(tab) === true) {
if (tab.id && (tab.id != 0)) {
tabs = tabs || [];
var domain = this.extractHostname(tab.url);
var isDifferentUrl = false;
if (currentTab !== tab.url) {
isDifferentUrl = true;
}
if (this.isNewUrl(domain) && !this.isInBlackList(domain)) {
var favicon = tab.favIconUrl;
if (favicon === undefined) {
favicon = 'chrome://favicon/' + domain;
}
var newTab = new Tab(domain, favicon);
tabs.push(newTab);
}
if (isDifferentUrl && !this.isInBlackList(domain)) {
this.setCurrentActiveTab(domain);
var tabUrl = this.getTab(domain);
if (tabUrl !== undefined)
tabUrl.incCounter();
this.addTimeInterval(domain);
}
}
}
}
isValidPage(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;
}
isInBlackList(domain) {
if (setting_black_list !== undefined && setting_black_list.length > 0)
return setting_black_list.find(o => isDomainEquals(this.extractHostname(o), this.extractHostname(domain))) !== undefined;
else return false;
}
isLimitExceeded(domain, tab) {
if (setting_restriction_list !== undefined && setting_restriction_list.length > 0) {
var item = setting_restriction_list.find(o => isDomainEquals(this.extractHostname(o.domain), this.extractHostname(domain)));
if (item !== undefined) {
var today = new Date().toLocaleDateString("en-US");
var data = tab.days.find(x => x.date == today);
if (data !== undefined) {
var todayTimeUse = data.summary;
if (todayTimeUse >= item.time) {
return true;
}
}
}
}
return false;
}
isNewUrl(domain) {
if (tabs.length > 0)
return tabs.find(o => o.url === domain) === undefined;
else return true;
}
getTab(domain) {
if (tabs !== undefined)
return tabs.find(o => o.url === domain);
}
extractHostname(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;
}
updateFavicon(tab) {
var domain = this.extractHostname(tab.url);
var currentTab = this.getTab(domain);
if (currentTab !== null && currentTab !== undefined) {
if (tab.favIconUrl !== undefined && tab.favIconUrl !== currentTab.favicon) {
currentTab.favicon = tab.favIconUrl;
}
}
}
setCurrentActiveTab(domain) {
this.closeIntervalForCurrentTab();
currentTab = domain;
}
clearCurrentActiveTab() {
this.closeIntervalForCurrentTab();
currentTab = '';
}
addTimeInterval(domain) {
var item = timeIntervalList.find(o => o.domain === domain);
if (item != undefined) {
item.addInterval();
} else {
var newInterval = new TimeInterval(new Date().toLocaleDateString("en-US"), domain);
timeIntervalList.push(newInterval);
newInterval.addInterval();
}
}
closeIntervalForCurrentTab() {
if (currentTab !== '') {
var item = timeIntervalList.find(o => o.domain === currentTab);
if (item != undefined)
item.closeInterval();
}
currentTab = '';
}
};