-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
161 lines (136 loc) · 4.27 KB
/
Copy pathbackground.js
File metadata and controls
161 lines (136 loc) · 4.27 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
//declare global variables such as default blacklist
var objarray = [{urlContains: "youtube.com"}, {urlContains: "reddit.com"},
{urlContains: "twitter.com"},{urlContains: "instagram.com"},{urlContains: "facebook.com"},
{urlContains: "twitch.tv"}, {urlContains: "netflix.com"}, {urlContains: "hulu.com"}];
var urlarray = [];
//check status of ON/OFF switch and determine whether or not to monitor
//URLS
var switchOn = chrome.storage.sync.get({
switchStatus:false,
}, function(items) {
switchOn = items.switchStatus;
if(switchOn){listenURLs();}else{stopListening();}
});
//work to be done when extension is installed
//document.addEventListener('DOMContentLoaded', primeURLarray(primeURLarray2()));
chrome.runtime.onInstalled.addListener();
//function that starts the process of monitoring urls
function listenURLs(){
chrome.webNavigation.onBeforeNavigate.addListener(blockWebsite,
{url: objarray});
switchOn = true;
}
//function that ends process of monitoring urls
function stopListening(){
chrome.webNavigation.onBeforeNavigate.removeListener(blockWebsite);
switchOn = false;
}
//action of preventing the user from accessing a blacklisted website during studymode
function blockWebsite(callback){
chrome.tabs.query({active: true, currentWindow:true}, function(tabs)
{chrome.tabs.remove(tabs[0].id, function() {
});
});
}
//update local list of blacklisted Websites
//removes then readds listeners during this process
function primeURLarray(callback){
if(switchOn){
chrome.webNavigation.onBeforeNavigate.removeListener(blockWebsite);
}
chrome.storage.sync.get({
blockList:[ "youtube.com", "reddit.com",
"twitter.com","instagram.com","facebook.com",
"twitch.tv", "netflix.com", "hulu.com"],
}, function(result) {
urlarray = result;
urlarray = urlarray.blockList;
});
setTimeout(callback, 500);
}
//helper method that accompanies primeURLarray
//creates the specifier:URL format that the Chrome API needs
function primeURLarray2(){
objarray = [];
for(var i = 0; i < urlarray.length; i++){
var tempObj = {urlContains: urlarray[i]};
objarray.push(tempObj);
}
if(switchOn){
chrome.webNavigation.onBeforeNavigate.addListener(blockWebsite,
{url: objarray});
}
}
/////////////////////////////////////////////////////////////////////////////
var x;
var sec = 1;
var min = 0;
var hour = 0;
var startstop = 0; // boolean condition for whether button should display start or stop
var studyDataArr = [0, 0, 0]; //global array for total study data
var timeRun = 0; //total time stopwatch has been running in seconds
function startStop() { /* Toggle StartStop */
startstop = startstop + 1;
if (startstop === 1) {
reset();
start();
}
else if (startstop === 2) {
startstop = 0;
stop();
//loadData();
}
}
// var sec = 1;
// var min = 0;
// var hour = 0;
function printTime(i) {
if (i < 10) { // if condition that adds extra 0 integer if number is singular digit
i = "0" + i; // i would be converted to a string
}
return i;
}
//background timer driver
function timer() {
/* Main Timer */
secOut = printTime(sec);
minOut = printTime(min);
hourOut = printTime(hour);
sec = sec+1;
if (sec == 60) {
min = min+1;
sec = 0;
}
if (min == 60) {
min = 0;
hour = hour+1;
}
var time_string = String(minOut)+":"+String(secOut);
chrome.browserAction.setBadgeText({text: time_string});
timeRun = timeRun + 1;
}
//starts timing process
function start() {
x = setInterval(timer, 1000);
}
//stops timing process
function stop() {
clearInterval(x);
reset();
}
//resets variables to 0 and updates aggregated time tracker in cloud
function reset() {
sec = 0;
min = 0;
hour = 0;
var temp = timeRun;
timeRun = 0;
//add all aggregated time + timeRun and restore to cloud
chrome.storage.sync.get( {aggTime:0}, function(item){
chrome.storage.sync.set({aggTime:(temp+item.aggTime)})
});
}
//returns total # of seconds timer has run
function getSeconds(){
chrome.storage.sync.set({totalTime:timeRun});
}