-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackground.js
213 lines (195 loc) · 5.85 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
chrome.runtime.onInstalled.addListener(function initialization(){
turnFilteringOff();
chrome.storage.sync.set({'blockingMethod': "close_tab"});
let timerData = { isTimerEnabled: false, blockUntilMilliseconds: 0};
chrome.storage.sync.set({'timerData': timerData});
chrome.storage.sync.get('blockedSites', function(data) {
blockedSites = data.blockedSites;
if(typeof blockedSites != "undefined" && blockedSites != null
&& blockedSites.length != null && blockedSites.length > 0){
var defaultListConfirm = confirm("We have detected that our extension"
+ " was once installed on this device.\nDo you want to load your old filter list?");
if (defaultListConfirm) {
console.log("User confirmed keeping a previous filter list");
}
else {
console.log("User cancelled loading a previous filter list.");
addDefaultFilters();
}
}
else {
console.log("User didn't have any previous filters");
addDefaultFilters();
}
});
});
function addDefaultFilters(){
var blockedSites = ["://www.facebook.com","://www.twitter.com","://www.instagram.com","://www.youtube.com"];
chrome.storage.sync.set({'blockedSites': blockedSites}, function() {
console.log('Default blocked sites have been loaded.');
});
};
chrome.runtime.onStartup.addListener(function() {
chrome.storage.sync.get('isEnabled', function (data) {
if(data.isEnabled){
icon = 'on.png';
}
else if(!data.isEnabled){
icon = 'off.png';
}else{
icon = 'icon.png';
}
chrome.browserAction.setIcon({path:{"16": icon}});
});
});
chrome.browserAction.onClicked.addListener(function toggleBlocking(){
chrome.storage.sync.get('timerData', function (data) {
if(!data.timerData.isTimerEnabled){
chrome.storage.sync.get('isEnabled', function(data){
if(data.isEnabled){
turnFilteringOff();
}
else{
turnFilteringOn();
}
});
}
else{
if(!updateTimer(data.timerData)){
var now = new Date().getTime();
var timeLeft = data.timerData.blockUntilMilliseconds - now;
var hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
alert("Timer mode enabled! " + hours + " hours "+ minutes + " minutes " + seconds + " seconds left.");
}
}
});
});
/**
* This function, given timerData, checks if the time of Timer mode is up.
* If it is, it switched the Mode and filtering off. Else it returns false.
*
* @param {Object} 'timerData' from chrome.storage.sync.get.
*
* @return {boolean} true if the time is up and filtering was turned off
* false it time is not up yet
*/
function updateTimer(timerData){
let timeLeft = timerData.blockUntilMilliseconds - Date.now();
if (timeLeft <= 0){ //unblock
timerData.isTimerEnabled = false;
chrome.storage.sync.set({'timerData': timerData}, function() {
turnFilteringOff();
});
return true;
}
return false;
}
chrome.tabs.onUpdated.addListener(function blockIfEnabled(tabId, info, tab) {
chrome.storage.sync.get('isEnabled', function (data) {
if (data.isEnabled) {
chrome.storage.sync.get('timerData', function (data) {
if(data.timerData.isTimerEnabled){
if(!updateTimer(data.timerData)){
runPageThroughFilter(tab);
}
}
else{
runPageThroughFilter(tab);
}
});
}
});
});
function runPageThroughFilter(tab){
chrome.storage.sync.get('blockedSites', function (data) {
data.blockedSites.forEach(function (site) {
if (tab.url.includes(site)) {
denyPage(tab.id);
}
});
});
};
chrome.contextMenus.create({
id: "baFilterListMenu",
title: "Show filter list",
contexts: ["browser_action"]
});
chrome.contextMenus.create({
id: "baAddToFilterList",
title: "Block this:",
contexts: ["browser_action"]
});
chrome.contextMenus.create({
parentId: "baAddToFilterList",
id: "baAddSiteToFilterList",
title: "Page",
contexts: ["browser_action"]
});
chrome.contextMenus.create({
parentId: "baAddToFilterList",
id: "baAddDomainToFilterList",
title: "Domain",
contexts: ["browser_action"]
});
chrome.contextMenus.create({
id: "baTimerMode",
title: "Timer mode setup",
contexts: ["browser_action"]
});
chrome.contextMenus.create({
id: "pgAddToFilterList",
title: "Block this:",
contexts: ["page"]
});
chrome.contextMenus.create({
parentId: "pgAddToFilterList",
id: "pgAddSiteToFilterList",
title: "Page",
contexts: ["page"]
});
chrome.contextMenus.create({
parentId: "pgAddToFilterList",
id: "pgAddDomainToFilterList",
title: "Domain",
contexts: ["page"]
});
chrome.contextMenus.onClicked.addListener(function contextMenuHandler(info, tab) {
switch(info.menuItemId) {
case "baFilterListMenu":
chrome.tabs.create({ url: '/filterList.html'});
break;
case "baTimerMode":
chrome.tabs.create({ url: '/timerModeSetup.html'});
break;
case "baAddSiteToFilterList":
case "pgAddSiteToFilterList":
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
let urls = tabs.map(x => x.url);
addUrlToBlockedSites(urls[0], tab);
});
break;
case "baAddDomainToFilterList":
case "pgAddDomainToFilterList":
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
let urls = tabs.map(x => x.url);
var domain = urls[0].match(/^[\w]+:\/{2}([\w\.:-]+)/)[1];
addUrlToBlockedSites(domain, tab);
});
break;
}
});
function addUrlToBlockedSites(url, tab){
chrome.storage.sync.get('blockedSites', function (data){
data.blockedSites.push(url); // urls.hostname
chrome.storage.sync.set({'blockedSites':data.blockedSites}, function(data){
console.log(url + ' added to blocked sites');
chrome.storage.sync.get('isEnabled', function(data) {
if(data.isEnabled){
denyPage(tab.id);
}
});
});
});
}