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
162 lines (143 loc) · 5.36 KB
/
activity.js
File metadata and controls
162 lines (143 loc) · 5.36 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
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
'use strict';
class Activity {
addTab(tab) {
if (this.isValidPage(tab) === true) {
if (tab.id && (tab.id != 0)) {
tabs = tabs || [];
var url = new Url(tab.url);
var isDifferentUrl = false;
if (!url.isMatch(currentTab)) {
isDifferentUrl = true;
}
if (this.isNewUrl(url) && !this.isInBlackList(url)) {
var favicon = tab.favIconUrl;
if (favicon === undefined) {
favicon = 'chrome://favicon/' + url.host;
}
var newTab = new Tab(url, favicon);
tabs.push(newTab);
}
if (isDifferentUrl && !this.isInBlackList(url)) {
this.setCurrentActiveTab(url);
var tabUrl = this.getTab(url);
if (tabUrl !== undefined)
tabUrl.incCounter();
this.addTimeInterval(url);
}
}
} else this.closeIntervalForCurrentTab();
}
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 => o.isMatch(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 => o.url.isMatch(domain));
if (item !== undefined) {
var data = tab.days.find(x => x.date == todayLocalDate());
if (data !== undefined) {
var todayTimeUse = data.summary;
if (todayTimeUse >= item.time) {
return true;
}
}
}
}
return false;
}
wasDeferred(domain){
if (deferredRestrictionsList != undefined){
let defItem = deferredRestrictionsList.find(x => new Url(x.site).isMatch(domain));
if (defItem != null){
let time = defItem.dateOfDeferred;
if (time + DEFERRED_TIMEOUT > new Date().getTime()){
return true;
}
else {
let index = deferredRestrictionsList.indexOf(defItem);
if (index > -1)
deferredRestrictionsList.splice(index, 1);
return false;
}
}
}
return false;
}
isNewUrl(domain) {
if (tabs.length > 0)
return tabs.find(o => o.url.isMatch(domain)) === undefined;
else return true;
}
getTab(domain) {
if (tabs !== undefined)
return tabs.find(o => o.url.isMatch(domain));
}
updateFavicon(tab) {
if (!this.isValidPage(tab)){
return;
}
var url = new Url(tab.url);
var currentTab = this.getTab(url);
if (currentTab !== null && currentTab !== undefined) {
if (tab.favIconUrl !== undefined && tab.favIconUrl !== currentTab.favicon) {
currentTab.favicon = tab.favIconUrl;
}
}
}
setCurrentActiveTab(domain) {
this.closeIntervalForCurrentTab();
currentTab = domain;
this.addTimeInterval(domain);
}
addTimeInterval(domain) {
var item = timeIntervalList.find(o => o.url.isMatch(domain) && o.day == todayLocalDate());
if (item != undefined) {
if (item.day == todayLocalDate())
item.addInterval();
else {
var newInterval = new TimeInterval(todayLocalDate(), domain);
newInterval.addInterval();
timeIntervalList.push(newInterval);
}
} else {
var newInterval = new TimeInterval(todayLocalDate(), domain);
newInterval.addInterval();
timeIntervalList.push(newInterval);
}
}
closeIntervalForCurrentTab(preserveCurrentTab) {
if (currentTab && timeIntervalList != undefined) {
var item = timeIntervalList.find(o => o.url.isMatch(currentTab) && o.day == todayLocalDate());
if (item != undefined)
item.closeInterval();
}
if (!preserveCurrentTab) {
currentTab = null;
}
}
isNeedNotifyView(domain, tab){
if (setting_notification_list !== undefined && setting_notification_list.length > 0) {
var item = setting_notification_list.find(o => o.url.isMatch(domain));
if (item !== undefined) {
var today = todayLocalDate();
var data = tab.days.find(x => x.date == today);
if (data !== undefined) {
var todayTimeUse = data.summary;
if (todayTimeUse == item.time || todayTimeUse % item.time == 0) {
return true;
}
}
}
}
return false;
}
};