-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetracker.js
241 lines (237 loc) · 8.33 KB
/
timetracker.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
function TimeTracker() {
// this.curr = new Object();
this.today = new Object();
this.pastDays = new Array();
this.maxDaysToKeep = 30;
asyncHelper = (data, c, newObj) => {
try {
let m = parseInt(localStorage.getItem("counter"));
localStorage.setItem("counter", m + 1);
// alert( localStorage.getItem( "counter" ) );
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
// alert(c+" "+this.responseText);
localStorage.setItem("counter", parseInt(localStorage.getItem("counter")) - 1);
// alert( localStorage.getItem( "counter" ) );
try {
newObj[c] = JSON.parse(this.responseText).msg;
localStorage.setItem("visited", JSON.stringify(newObj));
}
catch (err) { console.log("Error"); }
}
});
xhr.open("POST", "http://localhost:5000/api/getcategory");
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.send(data);
}
catch (c) { alert("Unable to reach server") }
}
asyncLocal = (c) => {
let tObj = localStorage.getItem("visited");
let mainObj = JSON.parse(localStorage.getItem("visited"));
if (tObj == null) {
// alert('Once '+c);
let newObj = {};
newObj[c] = "true"
localStorage.setItem("visited", JSON.stringify(newObj));
var data = `url=${c}`;
localStorage.setItem("counter", 0);
// alert( localStorage.getItem( "counter" ) );
asyncHelper(data, c, newObj);
}
else if (!(c in mainObj)) {
// alert(c);
let m = parseInt(localStorage.getItem("counter"));
let tempObj = mainObj;
tempObj[c] = "true";
localStorage.setItem("visited", JSON.stringify(tempObj));
var data = `url=${c}`;
setTimeout(function () { asyncHelper(data, c, tempObj); }, 3 * m * 10000);
}
}
closeCurrent = (c) => {
let done = JSON.parse(localStorage.getItem("done"));
let mainObj = JSON.parse(localStorage.getItem("visited"));
if (done != null && mainObj != null) {
// alert(JSON.stringify(done));
if (mainObj[c] in done) {
// alert('Closing area');
chrome.tabs.query(
{ currentWindow: true, active: true },
function (tabArray) {
if (mainObj[getDomainNew(tabArray[0].url)] in done) {
chrome.tabs.remove(tabArray[0].id)
}
}
)
}
}
}
function a(d) {
var b = null;
try {
b = JSON.parse(localStorage.getItem(d))
} catch (c) { }
return b
}
this._loadTrackingInfo = function () {
var b = null;
if (b = a("today")) {
this.today = b
}
if (b = a("TimeTrackerStartTime")) {
this.startTime = new Date(b)
} else {
this.startTime = new Date();
this._saveNewStartTime()
}
if (b = a("pastDays")) {
this.pastDays = b
}
this.resetTime = getStartOfNextDay(this.startTime);
if (DEBUG) {
window.console.log("Time tracker: Start time is " + this.startTime.toLocaleString() + " Next reset time would be " + this.resetTime.toLocaleString())
}
this.lastSaveTime = new Date().getTime()
};
this._saveNewStartTime = function () {
if (DEBUG) {
window.console.log("Time tracker: save new start time to local storage")
}
localStorage.setItem("TimeTrackerStartTime", JSON.stringify(this.startTime.getTime()))
};
this._savePastDaysTrackingInfo = function () {
if (DEBUG) {
window.console.log("Time tracker: save past days tracking info to local storage")
}
localStorage.setItem("pastDays", JSON.stringify(this.pastDays))
};
this._saveDayTrackingInfo = function () {
if (DEBUG) {
window.console.log("Time tracker: save today's tracking info to local storage")
}
localStorage.setItem("today", JSON.stringify(this.today));
// localStorage.setItem("curr", JSON.stringify(this.curr));
this.lastSaveTime = new Date().getTime()
};
this.useSeconds = function (d, b) {
var c = getDomainNew(b); //Comment by Vishal //getDomain(b);
//Added By Vishal
try {
if(!(c in sEs))
{
closeCurrent(c);
asyncLocal(c);
}
}
catch (err) {
console.log(err, c);
}
// .. Till Here
if (c in this.today) {
//Added by Vishal
// let obj = JSON.parse(localStorage.getItem("visited"));
// if ((c in obj) && (obj[c] !== true)) {
// this.curr[obj[c]] += (d || 1);
// }
// .. Till Here
this.today[c] += d
} else {
//Added by Vishal
// let obj = JSON.parse(localStorage.getItem("visited"));
// if ((c in obj) && (obj[c] !== true)) {
// this.curr[obj[c]] = (d || 1);
// }
// .. Till Here
this.today[c] = d
}
this._houseKeeping()
};
this._addDayToPast = function (c) {
if (DEBUG) {
window.console.log("Time tracker: Add one day to past")
}
var b = this.pastDays.unshift(c);
if (b > this.maxDaysToKeep) {
this.pastDays.pop()
}
};
this._houseKeeping = function () {
var b = new Date();
if (b.getTime() > this.resetTime.getTime()) {
this._addDayToPast(this.today);
this.today = new Object();
extraDays = Math.floor((b.getTime() - this.resetTime.getTime()) / millisecondsOfADay());
for (var c = 0; c < extraDays; c++) {
this._addDayToPast(null)
}
this.startTime = b;
this.resetTime = getStartOfNextDay(this.startTime);
if (DEBUG) {
window.console.log("Time tracker: New start time is " + this.startTime.toLocaleString() + " Next reset time would be " + this.resetTime.toLocaleString())
}
this._saveNewStartTime();
this._saveDayTrackingInfo();
this._savePastDaysTrackingInfo()
}
if (b.getTime() > this.lastSaveTime + 30 * 1000) {
this._saveDayTrackingInfo()
}
};
this.resetData = function () {
this.today = new Object();
// this.curr = newObj();
this.pastDays = new Array();
this._saveDayTrackingInfo();
this._savePastDaysTrackingInfo()
};
this.getUsage = function (b, c) {
if (b == null) {
b = "day"
} else {
b = b.toLowerCase()
}
var g = 0;
if (b == "week") {
g = 6
} else {
if (b == "month") {
g = 29
} else {
if (b == "day") {
g = 0
} else {
throw "getUsage(range): Unknown range specifier!"
}
}
}
var f = new Object();
for (var j in this.today) {
f[j] = this.today[j]
}
for (var e = 0; e < Math.min(this.pastDays.length, g); e++) {
if (null == this.pastDays[e]) {
continue
}
for (var j in this.pastDays[e]) {
if (j in f) {
f[j] += this.pastDays[e][j]
} else {
f[j] = this.pastDays[e][j]
}
}
}
var h = [];
for (var d in f) {
h.push([d, f[d]])
}
return h.sort(function (k, i) {
return k[1] < i[1] ? 1 : (k[1] > i[1] ? -1 : 0)
}).slice(0, c)
};
this._loadTrackingInfo();
this._houseKeeping()
};