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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
//
// Copyright (c) 2012 Stefan Bolte <portix@gmx.net>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
/*
* Block requests from thirdparty domains
*
* Extension that blocks requests from thirdparty domains with whitelisting
* support, either permanently or just for the session.
* It is also possible to block requests from certain domains on all sites, they
* have highest precedence and will also be blocked on sites where
* thirdparty requests are allowed in general.
*
*
* To use this extension load it with a userscript in
* $HOME/.config/dwb/userscripts/, e.g.
*
* ------------------------------------------------------------------------------
* |#!javascript |
* | |
* |extensions.load("requestpolicy"); |
* ------------------------------------------------------------------------------
*
*
* Configuration options:
*
* shortcut : Shortcut to block / allow requests, default "erp"
*
* unblockCurrent : Shortcut to unblock always blocked requests, shows only
* domains from the current site, default "erC"
*
* unblockAll : Shortcut to unblock always blocked requests, shows all
* blocked domains, default "erA"
*
* whiteList : A path to the whitelisting file, default is
* $XDG_CONFIG_HOME/dwb/<profile>/requestpolicy.json
*
* autoreload : Whether to automatically reload the website after the
* persistentList has changed, default false
*
* notify : Whether to notify about blocked request, default false
*
*
* Example (loading config with extensions.load())
*
* ------------------------------------------------------------------------------
* |extensions.load("requestpolicy", { |
* | whiteList : system.getEnv("HOME") + "/.dwb_request_policy", |
* | autoreload : true |
* |}); |
* ------------------------------------------------------------------------------
*
* Example extensionrc:
*
* ------------------------------------------------------------------------------
* |return { |
* | foo : { ... }, |
* | |
* | requestpolicy : { |
* | autoreload : true |
* | shortcut : "rp", |
* | notify : false |
* | }, |
* | bar : { ... } |
* |} |
* ------------------------------------------------------------------------------
*
* */
/*<INFO
Block requests from thirdparty domains with whitelisting support, permanently or just for a session
INFO>*/
var me = "requestpolicy";
var defaultConfig = {
//<DEFAULT_CONFIG
// path to a whitelist
whiteList : data.configDir + "/" + data.profile + "/requestpolicy.json",
// shortcut to block/allow requests
shortcut : "erp",
// shortcut to unblock requests from current site that are blocked on all
// sites
unblockCurrent : "erC",
// shortcut to unblock requests that are blocked on all sites
unblockAll : "erA",
// reload current site after blocking / unblocking a request
autoreload : false,
// notify about blocked requests
notify : false
//>DEFAULT_CONFIG
};
var config = {};
var sigs = {
resource : -1,
navigation : -1,
loadFinished : -1
};
var persistentList = null;
var tmpList = {};
var getPrivate = (function() {
var priv = "_requestPolicy" + parseInt(Math.random() * 9999999999, 10);
return function (wv) {
var o = wv[priv];
if (o === undefined) {
o = { domains : [], blocked : 0 };
Object.defineProperty(wv, priv, {
value : o,
writable : true
});
}
return o;
};
})();
function listAdd(o, key, value, doWrite) {
if (!o[key])
o[key] = [];
if (o[key].fastIndexOf(value) == -1)
o[key].push(value);
if (doWrite)
io.write(config.whiteList, "w", JSON.stringify(persistentList));
}
function listRemove(o, firstParty, domain, doWrite) {
var idx;
if (o[firstParty] && (idx = o[firstParty].fastIndexOf(domain)) != -1) {
o[firstParty].splice(idx, 1);
if (o[firstParty].length === 0)
delete o[firstParty];
if (doWrite)
io.write(config.whiteList, "w", JSON.stringify(persistentList));
return true;
}
return false;
}
// MENU {{{
function showMenu() {
var tmpWhiteListed, whiteListed;
var isWhiteListed = false;
var dom, i, l, domains, labels, currentDomain;
var domain = tabs.current.mainFrame.domain;
if (domain === null)
return;
domains = getPrivate(tabs.current).domains;
labels = [];
currentDomain = tabs.current.mainFrame.domain;
for (i=0, l=domains.length; i<l; ++i){
(function(dom) {
if (persistentList._alwaysBlock && persistentList._alwaysBlock.fastIndexOf(dom) != -1)
return;
whiteListed = persistentList[domain] && persistentList[domain].fastIndexOf(dom) != -1;
tmpWhiteListed = tmpList[domain] && tmpList[domain].fastIndexOf(dom) != -1;
if (!persistentList._always || persistentList._always.fastIndexOf(dom) == -1) {
if (!whiteListed && !tmpWhiteListed) {
labels.push({
left : "[" + dom + "] allow",
action : function () {
listAdd(persistentList, currentDomain, dom, true);
}
});
labels.push({
left : "[" + dom + "] allow temporarily",
action : function() {
listAdd(tmpList, currentDomain, dom, false);
}
});
}
else {
labels.push({
left : "[" + dom + "] block",
action : function() {
listRemove(persistentList, currentDomain, dom, true);
listRemove(tmpList, currentDomain, dom, false);
}
});
}
}
isWhiteListed = isWhiteListed || whiteListed || tmpWhiteListed;
if (!persistentList._always || persistentList._always.fastIndexOf(dom) == -1) {
labels.push({
left : "[" + dom + "] allow on all sites",
action : function() {
listAdd(persistentList, "_always", dom, true);
}
});
}
else {
labels.push({
left : "[" + dom + "] don't allow on all sites",
action : function() {
listRemove(persistentList, "_always", dom, true);
}
});
}
labels.push({
left : "[" + dom + "] block on all sites",
action : function () {
listAdd(persistentList, "_alwaysBlock", dom, true);
}
});
})(domains[i]);
}
var allAllowed = (persistentList._all && persistentList._all.fastIndexOf(domain) != -1) ||
(tmpList._all && tmpList._all.fastIndexOf(domain) != -1);
if (isWhiteListed || allAllowed) {
labels.unshift({
left : "Block all requests on " + domain,
action : function () {
delete persistentList[currentDomain];
listRemove(persistentList, "_all", currentDomain, false);
// necessary if persistentList.currentDomain exists
io.write(config.whiteList, "w", JSON.stringify(persistentList));
delete tmpList[currentDomain];
listRemove(tmpList, "_all", currentDomain, false);
}
});
}
if (allAllowed) {
labels.unshift({
left : "Don't allow all requests on " + domain,
action : function() {
listRemove(tmpList, "_all", currentDomain, false);
listRemove(persistentList, "_all", currentDomain, true);
}
});
}
else {
labels.unshift({
left : "Temporarily allow all requests on " + domain,
action : function() {
listAdd(tmpList, "_all", currentDomain, false);
}
});
labels.unshift({
left : "Allow all requests on " + domain,
action : function() {
listAdd(persistentList, "_all", currentDomain, true);
}
});
}
tabComplete("Requestpolicy:", labels, function (response) {
var i, l, len;
for (i=0, len = labels.length; i<len; ++i) {
l = labels[i];
if (l.left == response) {
l.action();
if (config.autoreload) {
tabs.current.reload();
}
}
}
}, true);
}//}}}
function unblockCurrent() {
if (!persistentList._alwaysBlock) {
io.notify("No domains to unblock");
return;
}
var domains = getPrivate(tabs.current).domains;
//var domains = persistentList._alwaysBlock;
var labels = [], i, l;
for (i=0, l = domains.length; i<l; i++) {
if (persistentList._alwaysBlock.fastIndexOf(domains[i]) != -1) {
labels.push({ left : domains[i] });
}
}
if (labels.length > 0) {
tabComplete("Unblock:", labels, function(response) {
listRemove(persistentList, "_alwaysBlock", response, true);
if (config.autoreload)
tabs.current.reload();
}, true);
}
else {
io.notify("No domains to unblock");
}
}
function unblockAll() {
if (!persistentList._alwaysBlock) {
io.notify("No domains to unblock");
return;
}
var i, l, labels = [];
var domains = persistentList._alwaysBlock;
for (i=0, l=domains.length; i<l; ++i) {
labels.push({ left : domains[i] });
}
if (labels.length > 0) {
tabComplete("Unblock:", labels, function(response) {
listRemove(persistentList, "_alwaysBlock", response, true);
if (config.autoreload)
tabs.current.reload();
}, true);
}
}
function blockRequest(wv, request, priv, domain) {
request.uri = "about:blank";
priv.blocked++;
if (config.notify && wv == tabs.current)
io.notify("RP: blocked " + domain);
return true;
}
// SIGNALS {{{
var resourceCB = (function () {
var regexEmpty = /^\s*$/;
return function resourceCB(wv, frame, request, response) {
var o, message, domain, firstParty;
if (regexEmpty.test(request.uri))
return false;
message = request.message;
if (!message)
return false;
firstParty = util.domainFromHost(message.firstParty.host);
domain = util.domainFromHost(message.uri.host);
if (firstParty == domain)
return false;
o = getPrivate(wv);
if (o.domains.fastIndexOf(domain) == -1)
o.domains.push(domain);
// Check for requests that are always blocked
if (persistentList._alwaysBlock && persistentList._alwaysBlock.fastIndexOf(domain) != -1)
return blockRequest(wv, request, o, domain);
// Check if domain is always allowed
if ((persistentList._all && persistentList._all.fastIndexOf(firstParty) != -1) ||
(tmpList._all && tmpList._all.fastIndexOf(firstParty) != -1))
return false;
// Check request is always allowed
if (persistentList._always && persistentList._always.fastIndexOf(domain) != -1)
return false;
// Check if request is whitelisted
if ( (!persistentList[firstParty] || persistentList[firstParty].fastIndexOf(domain) == -1) &&
(!tmpList[firstParty] || tmpList[firstParty].fastIndexOf(domain) == -1))
return blockRequest(wv, request, o, domain);
};
})();
function navigationCB(wv, frame) {
if (frame == wv.mainFrame) {
var o = getPrivate(wv);
o.domains = [];
o.blocked = 0;
}
}
function loadFinishedCB(wv) {
if (wv != tabs.current)
return;
var blocked = getPrivate(wv).blocked;
if (blocked > 0)
io.notify("RP: blocked " + blocked + " requests");
}
function connect() {
sigs.resource = signals.connect("resource", resourceCB);
sigs.navigation = signals.connect("navigation", navigationCB);
if (config.notify)
sigs.loadFinished = signals.connect("loadFinished", loadFinishedCB);
}
function disconnect() {
sigs.forEach(function (key, value) {
if (value != -1) {
signals.disconnect(value);
sigs[key] = -1;
}
});
}//}}}
return {
init : function(c) {
config = extensions.getConfig(c, defaultConfig);
if (system.fileTest(config.whiteList, FileTest.regular | FileTest.symlink)) {
var rawWhiteList = io.read(config.whiteList);
try {
persistentList = JSON.parse(rawWhiteList);
}
catch (e) {
extensions.debug(me, e, "Error parsing persistentList");
}
}
persistentList = persistentList || {};
connect();
bind(config.shortcut, showMenu, "requestpolicy");
bind(config.unblockCurrent, unblockCurrent, "requestpolicyUnblockCurrent");
bind(config.unblockAll, unblockAll, "requestpolicyUnblockAll");
return true;
},
end : function () {
disconnect();
unbind("requestpolicy");
unbind("requestpolicyUnblockCurrent");
unbind("requestpolicyUnblockAll");
}
}
// vim: set ft=javascript:
|