Repository navigation
Expand file tree
/
Copy pathcontentscript.js
More file actions
71 lines (61 loc) · 1.86 KB
/
Copy pathcontentscript.js
File metadata and controls
71 lines (61 loc) · 1.86 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
var totalSeconds = 0;
var secondsLabel = 0;
var minutesLabel = 0;
setInterval(setTime, 1000);
function getLocalStorage() {
return JSON.parse(localStorage.getItem("sites"));
}
function setLocalStorage(data) {
localStorage.setItem("sites",JSON.stringify(data));
}
var sites = Array();
function updateSites() {
sites = getLocalStorage();
}
document.addEventListener('DOMContentLoaded', function () {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
});
var totalTimer = 0;
var notificaitonCount = 0;
function setTime(){
updateSites();
++totalTimer;
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
for( var i = 0; i < sites.length; i++) {
if(tabs[0].url !== undefined || tabs[0].url !== null) {
if(tabs[0].url.toString().indexOf(sites[i].url) != -1) {
++sites[i].timeout;
myNewUrl = "timeout.html";
if(sites[i].timeout > sites[i].maxTimeout) {
if(sites[i].severity === "Severe") {
chrome.tabs.update(tabs[0].id, {url: myNewUrl});
}
else if(sites[i].severity === "Moderate") {
if (Notification.permission !== "granted")
Notification.requestPermission();
else if((notificaitonCount%300) === 0){
var notificationTitle = "You are spending too much time on " + sites[i].url;
var notificationText = "You have spent " + sites[i].timeout + " seconds";
var notification = new Notification(notificationTitle, {
icon: '/icon.png',
body: notificationText
});
notification.onclick = function () {
window.open("/dashboard.html");
};
}
++notificaitonCount;
}
}
setLocalStorage(sites);
}
}
}
});
localStorage.setItem("totalTimer",totalTimer);
}