-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
179 lines (121 loc) · 4.3 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
var myBlockedURLs;
myBlockedURLs = {urls: [
"*://www.ignore-this-domain.com/*"
]};
var initializeExtension = function (){
// Initialize Array
console.log("Starting Up!");
chrome.storage.sync.get("urlStore", function(results){
if (results.urlStore === undefined){
// Do nothing
console.log("urlStore is undefined")
// Populate with defaults
var defaultURLs = ["www.youtube.com","www.facebook.com","cnn.com","news.ycombinator.com","www.digg.com", "www.drudgereport.com", "twitter.com", "espn.go.com", "www.pinterest.com"];
chrome.storage.sync.set({"urlStore": defaultURLs}, function(){
chrome.storage.sync.get("urlStore", function(results){
console.log("Retrieved urlStore -->");
console.log(results.urlStore);
createBlackList(formatBlackList(results.urlStore));
});
})
} else {
// if it exists, do something
console.log("urlStore is defined")
chrome.storage.sync.get("urlStore", function(results){
console.log("Retrieved urlStore -->");
console.log(results.urlStore);
createBlackList(formatBlackList(results.urlStore));
});
}
})
// Initialize Toggle
chrome.storage.sync.get("toggle", function(results){
console.log("Initialized()")
console.log(results.toggle)
if (results.toggle === undefined){
chrome.storage.sync.set({"toggle": true},function(){
console.log("Toggle Initialized to FALSE");
})
} else if (results.toggle == false) {
console.log("Trigger Blocked URLs on Start")
if (chrome.webRequest.onBeforeRequest.hasListener(onBeforeRequestCallback)){
} else {
chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequestCallback);
chrome.storage.sync.get("urlStore", function(results){
console.log("Retrieved urlStore -->");
console.log(results.urlStore);
createBlackList(formatBlackList(results.urlStore));
chrome.webRequest.onBeforeRequest.addListener(
onBeforeRequestCallback,
myBlockedURLs,
["blocking"]);
console.log("Triggered")
});
}
}
})
}
var addToBlockedList = function(){
console.log("Refresh for Blocked List!")
if (chrome.webRequest.onBeforeRequest.hasListener(onBeforeRequestCallback)){
chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequestCallback);
chrome.storage.sync.get("urlStore", function(results){
console.log("Retrieved urlStore -->");
console.log(results.urlStore);
createBlackList(formatBlackList(results.urlStore));
chrome.webRequest.onBeforeRequest.addListener(
onBeforeRequestCallback,
myBlockedURLs,
["blocking"]);
console.log("Triggered")
});
} else {
}
}
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse){
if(request.msg == 'addToBlockedList') addToBlockedList();
});
var formatBlackList = function(blob){
var formattedURLs = [];
var i=0;
for (i=0;i<blob.length;i++){
formattedURLs.push("*://"+blob[i]+"/*")
}
// console.log("Formatted URLS -->");
// console.log(formattedURLs);
return formattedURLs;
}
var createBlackList = function(blob){
var i = 0;
myBlockedURLs.urls = [];
for (i=0; i<blob.length;i++){
myBlockedURLs.urls.push(blob[i]);
// console.log("Blocked URL --> " + myBlockedURLs.urls[i]);
}
// console.log("Black List -->");
// console.log(myBlockedURLs);
return myBlockedURLs;
}
var onBeforeRequestCallback = function(details) { return {cancel: true}; };
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse){
if(request.t){
console.log("Focus - Toggled On");
chrome.storage.sync.get("urlStore", function(results){
createBlackList(formatBlackList(results.urlStore));
});
chrome.webRequest.onBeforeRequest.addListener(
onBeforeRequestCallback,
myBlockedURLs,
["blocking"]);
}else{
chrome.storage.sync.get("urlStore", function(results){
createBlackList(formatBlackList(results.urlStore));
});
chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequestCallback);
console.log("Focus - Toggled Off");
}
}
)
initializeExtension();