-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
163 lines (153 loc) · 4.66 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
var dateObject = 0;
var timeStart = 0;
var start = 0;
var lastUpdate = 0;
var useTime = 0;
var pageTitle = "test";
var socialTime = 0;
var productiveTime = 0;
var readingTime = 0;
var setSocial = 0;
var pages = {
Social: [".tv", "facebook", "twitter", "instagram", "youtube", "reddit", "messenger","twitch","snapchat", "netflix", "imgur", "vimeo", "vsco", "tumblr", "crunchyroll", "pinterest", "flickr", "vine"],
Productivity: [".edu","stackoverflow", "mail", "outlook", "docs", "wolframalpha","chegg", "onQ", "Queen's University","Western University", "University of Waterloo", "github", "udemy", "udacity", "codecademy", "coursera", "lynda", "Education", "linkedin", "slack", "yahoo", "hotmail", "office365"],
Reading:["sciencedirect",".info", "researchgate", "scholar", "wikipedia", "science","academic","worldwidescience", "books","journal", "research", "audible", "khanacademy", "encyclopedia", "edX",".org",".gov",".gc"]
};
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install"){
chrome.storage.sync.set({Social: socialTime});
chrome.storage.sync.set({Productivity: productiveTime});
chrome.storage.sync.set({Reading: readingTime});
}
});
chrome.alarms.create('Update', {periodInMinutes: 1});
chrome.alarms.create('screenAlarm', {periodInMinutes: 2});
chrome.tabs.onUpdated.addListener(tabUpdated);
chrome.tabs.onRemoved.addListener(tabClosed);
chrome.tabs.onHighlighted.addListener(tabSwitch);
chrome.alarms.onAlarm.addListener(function(alarm){
if(alarm.name == 'Update'){
chrome.tabs.query({'active':true, 'lastFocusedWindow': true}, function(tabs){
update();
});
}else if(alarm.name == 'socialAlarm'){
window.alert("You have been on Social Media for quite a while! Time for a break?");
setSocial = 0;
setSocialAlarm();
}else if(alarm.name == 'screenAlarm'){
window.alert("You have been browsing for a while. Try going for a quick walk!");
}
});
function update(){
data = { name: pageTitle };
saveTime(data);
startTime();
}
function tabUpdated(tab){
chrome.tabs.query({'active':true, 'lastFocusedWindow': true}, function(tabs){
dateObject = new Date();
if(dateObject.getTime() - lastUpdate > 10000){
prev = pageTitle
pageTitle = tabs[0].url;
setSocialAlarm();
if(pageTitle.localeCompare(prev) == 0 || start == 0){
startTime();
}else{
var data = { name: prev};
saveTime(data);
}
dateObject = new Date();
lastUpdate = dateObject.getTime();
}
});
}
function tabSwitch(tab){
data = { name: pageTitle };
saveTime(data);
chrome.tabs.query({'active':true, 'lastFocusedWindow': true}, function(tabs){
pageTitle = tabs[0].url;
setSocialAlarm();
startTime();
});
}
function tabClosed(tab){
var data = { name: pageTitle };
saveTime(data);
}
function startTime(){
dateObject = new Date();
timeStart = dateObject.valueOf();
start = 1;
}
function saveTime(data){
dateObject = new Date();
useTime = dateObject.valueOf() - timeStart;
start = 0;
tmp = data.name;
for(var i in pages.Social){
if(tmp.includes(pages.Social[i])){
chrome.storage.sync.get(['Social'], function(result){
socialTime = result.Social;
socialTime = socialTime + useTime;
chrome.storage.sync.set({Social: socialTime},function(){
console.log("Social");
console.log(data.name);
console.log(socialTime);
});
});
}
}
for(var i in pages.Productivity){
if(tmp.includes(pages.Productivity[i])){
chrome.storage.sync.get(['Productivity'], function(result){
productiveTime = result.Productivity;
console.log("loaded");
console.log(result);
console.log(useTime);
productiveTime = productiveTime + useTime;
chrome.storage.sync.set({Productivity: productiveTime},function(){
console.log("Productivity");
console.log(data.name);
console.log(productiveTime);
});
});
}
}
for(var i in pages.Reading){
if(tmp.includes(pages.Reading[i])){
chrome.storage.sync.get(['Reading'], function(result){
readingTime = result.Reading;
console.log("loaded");
console.log(result);
console.log(useTime);
readingTime = readingTime + useTime;
chrome.storage.sync.set({Reading: readingTime},function(){
console.log("Reading");
console.log(data.name);
console.log(readingTime);
});
});
}
}
}
function setSocialAlarm(){
var set = 0;
console.log(setSocial);
for(var i in pages.Social){
if(pageTitle.toUpperCase().includes(pages.Social[i].toUpperCase())){
if(setSocial == 1){
set = 1;
}else{
chrome.alarms.create('socialAlarm', {delayInMinutes: 2});
console.log("social alarm set");
setSocial = 1;
set = 1;
}
}
}
if(!set){
setSocial = 0;
chrome.alarms.clear("socialAlarm");
console.log("alarm removed");
}
}