-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
403 lines (349 loc) · 14.5 KB
/
background.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
var timeSpentToday = 0;
var lastNotification = {};
var lastNotificationDate = 0;
var currentUrl = "";
var oldUrl = "";
var fullUrl = "";
var enabledOrNot = true;
var websites_json = {}
var checkTimer = null;
var changedEdits = false;
var changedTab = false;
var minimized = false;
var activeTabId;
const linkReview = ["https://addons.mozilla.org/firefox/addon/limite/"]; //{firefox add-ons}
const linkDonate = ["https://www.paypal.me/saveriomorelli", "https://ko-fi.com/saveriomorelli", "https://liberapay.com/Sav22999/donate"]; //{paypal, ko-fi}
const icons = ["icon.png", "icon_disabled.png", "icon_yellow.png", "icon_orange.png", "icon_red.png"];
function loaded() {
//catch changing of tab
browser.tabs.onActivated.addListener(tabUpdated);
browser.tabs.onUpdated.addListener(tabUpdated);
browser.windows.onFocusChanged.addListener(checkLostFocus);
}
function checkLostFocus(windowId) {
if (windowId === browser.windows.WINDOW_ID_NONE) {
// The browser window is minimized or lost focus
minimized = true;
} else {
//browser has focus
minimized = false;
}
//console.log("Minimized: " + minimized);
}
function reload() {
if (!changedTab) {
browser.tabs.query({active: true, currentWindow: true}, function (tabs) {
// since only one tab should be active and in the current window at once
// the return variable should only have one entry
let activeTab = tabs[0];
activeTabId = activeTab.id; // or do whatever you need
let activeTabUrl = activeTab.url; // or do whatever you
setUrl(getShortUrl(activeTabUrl), true);
oldUrl = currentUrl;
currentUrl = getShortUrl(activeTabUrl);
fullUrl = activeTabUrl;
browser.storage.local.get("edits", function (value) {
if (value["edits"] !== undefined && value["edits"]["counter"]) {
enabledOrNot = value["edits"]["enabled"];
//console.log("Updated || Enabled: " + enabledOrNot);
let editsToPush = {};
editsToPush["counter"] = false;
editsToPush["enabled"] = enabledOrNot;
browser.storage.local.set({"edits": editsToPush}, function () {
//console.log("Background || Reset edits = " + JSON.stringify(editsToPush));
checkStatusEnabled(enabledOrNot, true);
changedEdits = false;
});
} else {
checkStatusEnabled(enabledOrNot);
}
});
});
}
}
function checkStatusEnabled(enabled, force = false) {
if (!force) {
if (enabled) increaseTime(currentUrl);
} else {
saveUrlToData(enabled, 0);
}
if (!enabled) {
changeIcon(1);
setBadgeText("");
}
}
function tabUpdated() {
//console.log("updated");
browser.tabs.query({active: true, currentWindow: true}, function (tabs) {
// since only one tab should be active and in the current window at once
// the return variable should only have one entry
var activeTab = tabs[0];
activeTabId = activeTab.id;
var activeTabUrl = activeTab.url;
setUrl(getShortUrl(activeTabUrl), true);
oldUrl = currentUrl;
currentUrl = getShortUrl(activeTabUrl);
fullUrl = activeTabUrl;
getSavedData(activeTabUrl);
changedEdits = false;
if (checkTimer === null) {
checkTimer = setInterval(function () {
checkEverySecond(currentUrl);
}, 1000);
}
});
}
function setUrl(url, formatted = false) {
currentUrl = url;
}
function getShortUrl(url) {
let urlToReturn = url;
let urlParts, urlPartsTemp;
if (url.includes(":")) {
urlParts = url.split(":");
urlToReturn = urlParts[1];
if (isUrlSupported(url)) {
} else {
switchToOff();
return "This URL is not supported";
}
}
if (urlToReturn.includes("/")) {
urlPartsTemp = urlToReturn.split("/");
if (urlPartsTemp[0] === "" && urlPartsTemp[1] === "") {
urlToReturn = urlPartsTemp[2];
}
}
if (urlToReturn.includes(".")) {
urlPartsTemp = urlToReturn.split(".");
if (urlPartsTemp[0] === "www") {
urlToReturn = urlToReturn.substr(4);
}
}
return urlToReturn;
}
function getTheProtocol(url) {
return url.split(":")[0];
}
function saveUrlToData(enabled, time = 0) {
let urlToUse = currentUrl;
//console.log(JSON.stringify(websites_json));
let valueToUse = {};
browser.storage.local.get("websites", function (value) {
websites_json = {};
if (value["websites"] !== undefined) {
websites_json = value["websites"];
} else {
enabledOrNot = true;
}
//console.log(JSON.stringify(websites_json));
changedTab = false;
timeSpentToday = 0;
//timeSpentAlways = 0;
if (websites_json[urlToUse] !== undefined) {
valueToUse = websites_json[urlToUse];
if (websites_json[urlToUse][getToday()] !== undefined) {
timeSpentToday = websites_json[urlToUse][getToday()];
}
/*if (websites_json[urlToUse]["always"] !== undefined) {
timeSpentAlways = websites_json[urlToUse]["always"];
}*/
} else {
websites_json[urlToUse] = {};
}
if (valueToUse["category"] === undefined) valueToUse["category"] = getCategory(urlToUse);
timeSpentToday += time;
//timeSpentAlways += time;
valueToUse["enabled"] = enabled;
valueToUse[getToday()] = timeSpentToday;
//valueToUse["always"] = timeSpentAlways;
websites_json[urlToUse] = valueToUse;
if (isUrlSupported(fullUrl)) {
if (!changedEdits) {
browser.storage.local.set({"websites": websites_json}, function () {
//console.log("Background || Saved || Status: " + enabledOrNot + " || " + JSON.stringify(websites_json));
//console.log("Background || Saved || Status: " + enabledOrNot + "");
//console.log("Background || Saved || " + JSON.stringify(websites_json[currentUrl]));
});
}
}
if (timeSpentToday >= 0 && timeSpentToday < 60 * 30) {
//30 minutes || OK
changeIcon(0);
setBadgeText(((timeSpentToday - (timeSpentToday % 60)) / 60).toString() + "m");
} else if (timeSpentToday >= 60 * 30 && timeSpentToday < 60 * 60) {
//60 minutes (1 hour) || Yellow
changeIcon(2);
createNotification(2, currentUrl, "30 minutes", "You have already spent 30 minutes on this site today");
setBadgeText(((timeSpentToday - (timeSpentToday % 60)) / 60).toString() + "m", "#FFD400", "#000000");
} else if (timeSpentToday >= 60 * 60 && timeSpentToday < 60 * 60 * 3) {
//3 hours || Orange
changeIcon(3);
createNotification(3, currentUrl, "1 hour", "You have already spent 1 hour on this site today");
setBadgeText(((timeSpentToday - (timeSpentToday % (60 * 60))) / (60 * 60)).toString() + "h", "#FF7C00", "#000000");
} else if (timeSpentToday >= 60 * 60 * 3) {
//>3 hours || Red
changeIcon(4);
createNotification(4, currentUrl, "3 hours", "You have already spent 3 hours on this site today");
setBadgeText(">3h", "#FF0000");
}
})
}
function getSavedData(url) {
let urlToUse = currentUrl;
if (isUrlSupported(url)) {
browser.storage.local.get("websites", function (value) {
timeSpentToday = 0;
//timeSpentAlways = 0;
if (value["websites"] !== undefined) {
websites_json = value["websites"];
if (websites_json[urlToUse] !== undefined) {
let enabled = false;
if (websites_json[urlToUse]["enabled"] !== undefined) enabled = websites_json[urlToUse]["enabled"];
switchToOnOrOff(true, enabled);
timeSpentToday = 0;
if (websites_json[urlToUse][getToday()] !== undefined) {
timeSpentToday = websites_json[urlToUse][getToday()];
}
/*
if (websites_json[urlToUse]["always"] !== undefined) {
timeSpentAlways = websites_json[urlToUse]["always"];
} else {
timeSpentAlways = 0;
}*/
} else {
saveUrlToData(true, 0);
}
} else {
saveUrlToData(true, 0);
}
changedTab = false;
}
)
} else {
switchToOff();
changedTab = false;
}
}
function switchToOnOrOff(forced = false, value = false) {
if (forced && value) {
switchToOn();
} else {
switchToOff();
}
}
function switchToOn() {
enabledOrNot = true;
changeIcon(0);
}
function switchToOff() {
enabledOrNot = false;
changeIcon(1);
}
function isUrlSupported(url) {
let valueToReturn = false;
switch (getTheProtocol(url)) {
case "http":
case "https":
//the URL is supported
valueToReturn = true;
break;
default:
//this disable all unsupported website
valueToReturn = false;//todo | true->for testing, false->stable release
}
return valueToReturn;
}
function checkEverySecond(url) {
reload();
//console.log("Checking... || Enabled: " + enabledOrNot + " || Changed: " + changedEdits);
}
function increaseTime(url) {
if (enabledOrNot && !minimized) {
if (url === currentUrl) {
saveUrlToData(true, 1);
}
}
}
function createNotification(type, url, title, content) {
//send a notification
if (getToday() !== lastNotificationDate || lastNotification[url] !== type) {
lastNotificationDate = getToday();
lastNotification[url] = type;
browser.notifications.create({
"type": "basic",
"iconUrl": "./img/icon-48.png",
"title": title,
"message": content
});
} else {
//already sent
}
//console.log(JSON.stringify(lastNotification));
}
function getToday() {
let todayDate = new Date();
let today = "";
today = todayDate.getFullYear() + "-";
let month = todayDate.getMonth() + 1;
if (month < 10) today = today + "0" + month + "-";
else today = today + "" + month + "-";
let day = todayDate.getDate();
if (day < 10) today = today + "0" + day;
else today = today + "" + day;
return today;
}
function isInteger(value) {
if (Number.isNaN(value) === false) {
if (Number.isInteger(value)) {
return true;
}
}
return false;
}
function changeIcon(index) {
browser.browserAction.setIcon({path: "../img/" + icons[index], tabId: activeTabId});
}
function setBadgeText(text, background_color = "#0080FF", text_color = "#FFFFFF") {
browser.browserAction.setBadgeText({text: text});
browser.browserAction.setBadgeTextColor({color: text_color});
browser.browserAction.setBadgeBackgroundColor({color: background_color});
}
let categories = {
"social": ["facebook.com", "twitter.com", "instagram.com", "chat.openai.com"],
"travel": ["booking.com", "expedia.com", "airbnb.com", "hotels.com", "trivago.it"],
"news": ["bbc.com", "bbc.co.uk", "cnn.com", "rainews.it", "corriere.it", "repubblica.it", "msn.com", "news.yahoo.com", "aol.com", "ladige.it", "ildolomiti.it"],
"education": ["classroom.google.com", "edu.google.com"],
"shopping": ["amazon.com", "amazon.it", "amazon.fr", "amazon.de", "ebay.com", "eprice.it", "lafeltrinelli.it", "ibs.it", "mediaworld.it", "euronics.it", "trony.it", "unieuro.it", "justeat.it", "glovoapp.com", "deliveroo.co.uk", "deliveroo.it", "deliveroo.com", "tesco.com", "asda.com", "sainsburys.co.uk", "ah.nl"],
"search": ["google.com", "google.it", "google.co.uk", "google.fr", "google.de", "bing.com", "duckduckgo.com", "qwant.com", "baidu.com", "yandex.com", "search.yahoo.com"],
"reference": ["wikipedia.org", "wordreference.com", "dictionary.cambridge.org", "treccani.it", "oxfordlearnersdictionaries.com", "emojipedia.org", "affinity.help"],
"entertainment": ["youtube.com", "netflix.com", "primevideo.com", "spotify.com", "deezer.com", "disneyplus.com", "imdb.com", "hulu.com"],
"adults": ["youporn.com", "pornhub.com", "xnxx.com", "xvideos.com", "xhamster.com"],
"sav22999": ["saveriomorelli.com", "emojiaddon.com", "savpdfviewer.com"],
"develop": ["github.com", "gitlab.com", "addons.mozilla.org", "thunderbird.net", "addons.thunderbird.net", "stackoverflow.com", "w3.org", "w3schools.com", "developer.mozilla.org", "w3docs.com"],
"messaging": ["whatsapp.com", "web.whatsapp.com", "telegram.org", "web.telegram.org", "t.me"],
"games": [],
"health": ["apss.tn.it"],
"other": [] //must remain empty here
};
function getCategory(website) {
let valueToReturn = "";
if (website in categories["social"]) valueToReturn = "social";
else if (categories["travel"].includes(website)) valueToReturn = "travel";
else if (categories["news"].includes(website)) valueToReturn = "news";
else if (categories["education"].includes(website)) valueToReturn = "education";
else if (categories["shopping"].includes(website)) valueToReturn = "shopping";
else if (categories["search"].includes(website)) valueToReturn = "search";
else if (categories["reference"].includes(website)) valueToReturn = "reference";
else if (categories["entertainment"].includes(website)) valueToReturn = "entertainment";
else if (categories["adults"].includes(website)) valueToReturn = "adults";
else if (categories["sav22999"].includes(website)) valueToReturn = "sav22999";
else if (categories["develop"].includes(website)) valueToReturn = "develop";
else if (categories["messaging"].includes(website)) valueToReturn = "messaging";
else if (categories["games"].includes(website)) valueToReturn = "games";
else if (categories["health"].includes(website)) valueToReturn = "health";
else valueToReturn = "other";
//console.log(website + " : " + valueToReturn);//TODO: use for testing websites filter
return valueToReturn;
}
loaded();