-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
343 lines (304 loc) · 9.32 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Safari only. Carries out the whitelist/delisting when the browser button is pressed. Also calls up the options page.
*/
function performCommand(event)
{
if (SAFARI)
{
if (event.command === "browserAction") {
// Find out what the core domain of the URL is first
var currentURL = event.target.browserWindow.activeTab.url;
var splitURL = currentURL.toLowerCase().match(/^http[s]?:\/\/[^\.]+\.[^\/]+/i);
var coreWebsiteAddress = null;
if (splitURL != null && splitURL.length > 0)
{
coreWebsiteAddress = splitURL[0];
}
currentURL = coreWebsiteAddress;
if (currentURL)
{
if (isAllowed(currentURL))
{
revokeUrl(currentURL);
}
else
{
permitUrl(currentURL);
}
for (var i = 0; i < safari.application.browserWindows.length; i++)
{
var currWindow = safari.application.browserWindows[i];
for (var j = 0; j < currWindow.tabs.length; j++)
{
if (currWindow.tabs[j].url && patternMatches(currWindow.tabs[j].url, currentURL))
{
currWindow.tabs[j].url = currWindow.tabs[j].url;
}
}
}
}
}
else if (event.command === "showOptions")
{
var optionsURL = safari.extension.baseURI + "options.html";
// First close any other open BPUB options windows
for (var i = 0; i < safari.application.browserWindows.length; i++)
{
var currWindow = safari.application.browserWindows[i];
for (var j = 0; j < currWindow.tabs.length; j++)
{
if (currWindow.tabs[j].url === optionsURL)
{
currWindow.tabs[j].close();
}
}
}
var newTab = safari.application.activeBrowserWindow.openTab();
newTab.url = optionsURL;
}
}
}
/*
Safari only. Function is called by the browser when switching tabs or an even the requires a browser update.
*/
function validateCommand(event)
{
if (SAFARI)
{
if (event.command === "browserAction") {
// Don't think we ever need to disable the browser button
//event.target.disabled = !event.target.browserWindow.activeTab.url;
var currentURL = event.target.browserWindow.activeTab.url;
if (currentURL)
{
var itemArray = safari.extension.toolbarItems;
for (var i = 0; i < itemArray.length; ++i) {
var item = itemArray[i];
if (item.browserWindow.activeTab === event.target.browserWindow.activeTab
&& item.identifier === "com.optimalcycling.safari.betterpopupblocker-E6486QF2HJ browserActionButton")
{
if (urlsGloballyAllowed || isAllowed(currentURL))
item.image = safari.extension.baseURI + "IconAllowed.png";
else
item.image = safari.extension.baseURI + "IconForbidden.png";
break;
}
}
}
}
}
}
/*
For Safari only. Used to direct messages for functions like canLoad.
*/
function handleMessage(event)
{
if (SAFARI)
{
switch (event.name) {
case "canLoad":
var data = event.message;
if (data.type === "get settings block start")
{
event.message = generateAllSettings(data.url, event.target.url);
}
break;
case "window pop up blocked":
break;
}
}
}
/*
Safari only. Updates the browser button for the tabs having the url of "currentURL".
*/
function updateButtons(currentURL)
{
if (SAFARI)
{
if (currentURL)
{
var itemArray = safari.extension.toolbarItems;
for (var i = 0; i < itemArray.length; ++i) {
var item = itemArray[i];
if (item.browserWindow.activeTab.url === currentURL
&& item.identifier === "com.optimalcycling.safari.betterpopupblocker-E6486QF2HJ browserActionButton")
{
if (urlsGloballyAllowed || isAllowed(currentURL))
item.image = safari.extension.baseURI + "IconAllowed.png";
else
item.image = safari.extension.baseURI + "IconForbidden.png";
}
}
}
}
}
/*
Reloads the tabs that match the pattern "urlPattern"
urlPattern: The urlPattern pattern we just whitelisted or removed to be matched with the urlPatterns in the tabs.
Specifying "***NOTINWHITELIST***" will result in only urls not whitelisted being reloaded.
*/
function refreshTabs(urlPattern)
{
chrome.windows.getAll({populate: true}, function(windowsArray) {
for (var i = 0; i < windowsArray.length; i++)
{
var currWindow = windowsArray[i];
for (var j = 0; j < currWindow.tabs.length; j++)
{
if (urlPattern === "***NOTINWHITELIST***")
{
if (currWindow.tabs[j].url && !isAllowed(currWindow.tabs[j].url))
{
//chrome.tabs.update(currWindow.tabs[j].id, {url: currWindow.tabs[j].url});
// Reloading by chrome.tabs.executeScript(...). Works with or without javascript enabled.
chrome.tabs.executeScript(currWindow.tabs[j].id, {code:"if(window.location.href.indexOf('http') == 0) {window.location.reload();}"});
}
}
else
{
if (currWindow.tabs[j].url && urlPattern && patternMatches(currWindow.tabs[j].url, urlPattern))
{
//chrome.tabs.update(currWindow.tabs[j].id, {url: currWindow.tabs[j].url});
// Reloading by chrome.tabs.executeScript(...). Works with or without javascript enabled.
chrome.tabs.executeScript(currWindow.tabs[j].id, {code:"if(window.location.href.indexOf('http') == 0) {window.location.reload();}"});
}
}
}
}
});
}
/*
Generates a json object with all the applicable settings for a website of "url".
*/
function generateAllSettings(url, topWindowUrl)
{
var theSettings = {enabled: (isAllowed(url) || urlsGloballyAllowed),
blockWindowOpen: config.get('blockWindowOpen'),
closeAllPopUpWindows: config.get('closeAllPopUpWindows'),
blockWindowPrompts: config.get('blockWindowPrompts'),
blockJSContextMenuAndClickIntercept: config.get('blockJSContextMenuAndClickIntercept'),
blockWindowMovingAndResize: config.get('blockWindowMovingAndResize'),
blockUnescapeEval: config.get('blockUnescapeEval'),
blockJSSelection: config.get('blockJSSelection'),
blockJSTimers: config.get('blockJSTimers'),
blockJSPrint: config.get('blockJSPrint'),
blockOnUnload: config.get('blockOnUnload'),
blockWindowTargets: config.get('blockWindowTargets'),
extendedTooltips: config.get('extendedTooltips'),
stripJSFromLinkLocations: config.get('stripJSFromLinkLocations'),
blockCreateEvents: config.get('blockCreateEvents')};
// We use the more restrictive of the two policies for iframes compared to the top window
if (url !== topWindowUrl)
{
if (!urlsGloballyAllowed && theSettings.enabled && !isAllowed(topWindowUrl))
{
theSettings.enabled = false;
}
}
return theSettings;
}
/*
Listens for requests from the content scripts on both the Google Chrome and the Apple Safari extensions.
*/
function showBlockedBlink(blinkCount, tabId, currentURL, currentTarget)
{
if (SAFARI)
{
if (currentURL && currentTarget)
{
var itemArray = safari.extension.toolbarItems;
for (var i = 0; i < itemArray.length; ++i) {
var item = itemArray[i];
if (item.browserWindow.activeTab === currentTarget
&& item.identifier === "com.optimalcycling.safari.betterpopupblocker-E6486QF2HJ browserActionButton")
{
item.image = safari.extension.baseURI + "IconForbiddenBlocked.png";
var currItem = item;
setTimeout(function()
{
currItem.image = safari.extension.baseURI + "IconForbidden.png";
if (currentURL !== currItem.browserWindow.activeTab.url)
{
if (urlsGloballyAllowed || isAllowed(currItem.browserWindow.activeTab.url))
currItem.image = safari.extension.baseURI + "IconAllowed.png";
else
currItem.image = safari.extension.baseURI + "IconForbidden.png";
}
}, 400);
break;
}
}
}
lastAnimatedTab = null;
}
else
{
//console.log("blink " + blinkCount);
if (blinkCount % 2 === 0)
chrome.pageAction.setIcon({path: "IconForbiddenBlocked.png", tabId: tabId});
else
chrome.pageAction.setIcon({path: "IconForbidden.png", tabId: tabId});
chrome.pageAction.show(tabId);
blinkCount++;
if (blinkCount < maxBlinks)
{
animationTimer = setTimeout(function() { showBlockedBlink(blinkCount, tabId); }, 400);
}
else
{
chrome.pageAction.setIcon({path: "IconForbidden.png", tabId: tabId});
chrome.pageAction.show(tabId);
lastAnimatedTab = null;
animationTimer = null;
chrome.tabs.get(tabId, function (tab) {
if (tab.url !== currentURL)
{
if (config.get('showPageActionButton'))
{
if (urlsGloballyAllowed || isAllowed(tab.url))
{
chrome.pageAction.setIcon({path: "IconAllowed.png", tabId: tab.id});
}
else
{
chrome.pageAction.setIcon({path: "IconForbidden.png", tabId: tab.id});
}
chrome.pageAction.show(tab.id);
}
else
{
chrome.pageAction.hide(tab.id);
}
}
});
}
}
}
chrome.extension.onRequest.addListener(function(msg, src, send) {
if (msg.type === "get settings block start" || msg.type === "get settings block idle")
{
var theSettings = generateAllSettings(msg.url, src.tab.url);
send(theSettings);
}
else if (msg.type === "window pop up blocked" && config.get("showBlockedBlinks"))
{
if (lastAnimatedTab && lastAnimatedTab === src.tab.id)
{
}
else
{
lastAnimatedTab = src.tab.id;
showBlockedBlink(0, src.tab.id, src.tab.url, src.target);
}
send({});
}
else if (msg.type === "safari validate")
{
updateButtons(msg.url);
send({});
}
else
{
send({});
}
});