-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadblock_filter.js
423 lines (363 loc) · 14.3 KB
/
adblock_filter.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
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
//Production code
var ss = require("sdk/simple-storage");
const {Cc, Ci} = require("chrome");
const self = require("sdk/self");
const urlModule = require("sdk/url");
const tabsModule = require("sdk/tabs");
const Request = require("sdk/request").Request;
var whitelist = [];
var blacklist = [];
// loads specified list of uris and returns as array of strings
// Usage:
// load_list(String filename)
function load_list() {
// Use this for loading the Easylist from online
var easylist;
Request({
url: "https://easylist-downloads.adblockplus.org/easylist.txt",
onComplete: function (response) {
categorize(response.text.split("\n"));
}
}).get();
return easylist;
// Use this for loading Easylist from file
// return self.data.load("easylist/easylist.txt").split("\n");
}
// Categorizes the easylist into a whitelist and a blacklist. It also divides
// the blacklist into general domains, exact domains, and just by parts. All
// arrays are stored as global variables because they will need to get used
// repeatedly during the life of program
// Usage:
// categorize(String[] list)
function categorize(easylist) {
var rules = [];
var exceptions = [];
// divides easy list into blacklist and exceptions in blacklist (whitelist)
for (i in easylist) {
var line = easylist[i];
//DOESN'T add any lines with two hashes in them, they are not requests
if (line.indexOf("##") == -1 && line.indexOf("#@#" != -1)) {
if (line.match(/^@@.*/)) {
exceptions.push(line);
} else {
if (line.charAt(0) != "!") {
rules.push(line);
}
}
}
}
for (i in rules) {
var line = rules[i];
if (line.match(/^\|\|.*/)) {
line = line.substring(2, line.length)
}
blacklist.push(line);
}
for (i in exceptions) {
var line = exceptions[i];
if (line.match(/^@@.*/)) {
if (line.match(/^\|\|.*/)) {
line = line.substring(4, line.length)
}
else {
line = line.substring(2, line.length)
}
}
else if (line.match(/^\|\|.*/)) {
line = line.substring(2, line.length)
}
whitelist.push(line);
}
}
// This function returns if the following is image or not, "image" if image
// and "~image" if not an image
// Usage:
// is_image(String url)
function is_image(url) {
var arr = url.split(".");
var ext = arr[arr.length - 1];
if (ext == "png" || ext == "rif" || ext == "tif" || ext == "tiff" || ext == "jpeg" ||
ext == "jpg" || ext == "pcd" || ext == "jif" || ext == "gif" || ext == "jfif" ||
ext == "jp2" || ext == "jpx" || ext == "pcd") {
return "image";
}
else {
return "~image";
}
}
// This function returns if the following is script or not, "script" if script
// and "~script" if not a script
//Usage:
// is_script(String url)
function is_script(url) {
var arr = url.split(".");
var ext = arr[arr.length - 1];
if (ext == "js") {
return "script";
}
else {
return "~script";
}
}
// This function returns if the following is object or not, "object" if object
// and "~object" if not an object
// Usage:
// is_object(String url)
function is_object(url) {
var arr = url.split(".");
var ext = arr[arr.length - 1];
if (ext == "swf" || ext == "class") {
return "object";
}
else {
return "~object";
}
}
// This function returns if the following is stylesheet or not, "stylesheet" if stylesheet
// and "~stylesheet" if not a stylesheet
// Usage:
// is_stylesheet(String url)
function is_stylesheet(url) {
var arr = url.split(".");
var ext = arr[arr.length - 1];
if (ext == "css") {
return "stylesheet";
}
else {
return "~stylesheet";
}
}
// This function returns if the following is thirdparty or not, "thirdparty" if thirdparty
// and "~thirdparty" if not a thirdparty
// Usage:
// is_thirdparty(String url)
function is_thirdparty(url) {
//url = url request. window.location.hostname = host of current page
//var hostname = window.location.hostname;
var hostname = urlModule.URL(tabsModule.activeTab.url).host;
if (hostname.indexOf("www") == 0) {
hostname = hostname.replace("www.", "");
}
if (url.match(escape_reg_exp(hostname))) {
return "~third-party";
}
else return "third-party";
}
//returns a JSON object representing the URL passed as a parameter.
// Usage:
// JSONObject parseUri(String sourceUri)
function parseUri(sourceUri) {
var uriPartNames = ["source", "protocol", "authority", "domain", "port", "path", "directoryPath", "fileName", "query", "anchor"],
uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
uri = {};
for (var i = 0; i < 10; i++) {
uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
}
/* Always end directoryPath with a trailing backslash if a path was present in the source URI
Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
if (uri.directoryPath.length > 0) {
uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
}
if (uri.domain.indexOf("www.") == 0) {
uri.domain = uri.domain.substring(4, uri.domain.length);
}
return uri;
}
// Changes string rule into regular expression equivalent for comparison. This
// is so the request domain can be matched against the AdBlock rule.
// Usage:
// escape_reg_exp(String rule)
function escape_reg_exp(str) {
var newStr = str.replace(/[\-\[\]\/\{\}\(\)\+\?\.\\\$\|]/g, "\\$&");
newStr = newStr.replace("^", "\($|\/|\:\)");
newStr = newStr.replace("*", "\(.*\)");
newStr = newStr + "\(.*\)"
return new RegExp(newStr);
}
// Determines whether or not a request URL is an ad or not. This is the
// function that will be called by the other classes. The method first checks
// the request against the user blacklist. Then it checks the default AdBlock
// blacklist for a match. If there is a match, it checks the request against
// the whitelist. If a request matches the blaclist and the whitelist, it is
// not an ad, but otherwise, it is. If there is no match at all, the request
// is, by default, not an ad.
// Usage:
// is_Ad(String url_request)
function is_Ad(url) {
for (var i = 0; i < ss.storage.blacklist.length; i++) {
if (parseUri(url).domain.match(escape_reg_exp(ss.storage.blacklist[i]))) {
return true;
}
}
if (is_blacklisted(url)) {
if (is_whitelisted(url))
return false;
else return true;
}
else return false;
}
function is_whitelisted(url) {
return matching_urls(url, whitelist);
}
function is_blacklisted(url) {
return matching_urls(url, blacklist);
}
// This function is the main functionality of adblock_filter.js. This function
// checks a single request against a given list (whitelist/blacklist in our
// case) and then checks for additional options at the end of the the rule
// (separated by a "$" delimeter). These options are then parsed and each rule
// is assigned file types it should and should not be applied to. If the
// file type matches the rule's file types to block, it is blocked. Otherwise,
// it is not.
// Usage:
// matching_urls(String url, String[] rule_list)
function matching_urls(url, rule_list) {
for (var i = 0; i < rule_list.length; i++) {
var domain = rule_list[i].split("$");
if (url.match(escape_reg_exp(domain[0]))) {
// if there are no additional options and the url matches the rule,
// return true.
if (domain.length == 1) {
return true;
}
//else, if additional options are present, check them all
else {
var options = domain[1].split(",");
var domain_present = false;
var domain_sublist = "";
var blocked_rule;
var negated = false;
if (options[0].indexOf("~") == 0) {
negated = true;
}
for (var p = 0; p < options.length; p++) {
if (options[p].indexOf("domain=") != -1) {
domain_present = true;
domain_sublist = options[p].replace("domain=", "").split("|");
}
// since our rule consisted of several possible types of file types
// and different origins we decided creating an "object-like" structure
// would make the most sense. our object is basically a pool of all the
// possible type the rule can be, and whenever one of hte options is true
// it gets set to true in the object
// the later code inside this loop simply modifies this object as
// the url gets further parsing
// if ~ exists, we know all the rules will be negated
if (negated == true) {
blocked_rule = {
"stylesheet": true,
"script": true,
"obj": true,
"image": true,
"obj_sub": true,
"subrequest": true
};
if (options[p] == "~stylesheet") {
blocked_rule.stylesheet = false;
}
else if (options[p] == "~script") {
blocked_rule.script = false;
}
else if (options[p] == "~object") {
blocked_rule.obj = false;
}
else if (options[p] == "~image") {
blocked_rule.image = false;
}
else if (options[p] == "~object-subrequest") {
blocked_rule.obj_sub = false;
}
else if (options[p] == "~subdocument") {
blocked_rule.subrequest = false;
}
}
else {
blocked_rule = {
"stylesheet": false,
"script": false,
"obj": false,
"image": false,
"obj_sub": false,
"subrequest": false
};
if (options[p] == "stylesheet") {
blocked_rule.image = true;
}
else if (options[p] == "script") {
blocked_rule.script = true;
}
else if (options[p] == "object") {
blocked_rule.obj = true;
}
else if (options[p] == "image") {
blocked_rule.image = true;
}
else if (options[p] == "obj_sub") {
blocked_rule.obj_sub = true;
}
else if (options[p] == "subdocument") {
blocked_rule.subrequest = true;
}
}
}
if (blocked_rule.stylesheet == false && blocked_rule.script == false &&
blocked_rule.obj == false && blocked_rule.image == false && blocked_rule.obj_sub == false
&& blocked_rule.subrequest == false) {
blocked_rule = {"stylesheet": true, "script": true, "obj": true, "image": true, "obj_sub": true}
}
if (is_image(url) == "image") {
if (!blocked_rule.image) {
return false;
}
}
else if (is_stylesheet(url) == "stylesheet") {
if (!blocked_rule.stylesheet) {
return false;
}
}
else if (is_script(url) == "script") {
if (!blocked_rule.script) {
return false;
}
}
else if (is_object(url) == "object") {
if (!blocked_rule.obj) {
return false;
}
}
if (options.indexOf("third-party") != -1 && is_thirdparty(url) == "~third-party") {
return false;
}
else if (options.indexOf("~third-party") != -1 && is_thirdparty(url) == "third-party") {
return false;
}
// in this portion we iterate over the domains if any are present, these domains can
// either be accepted or negated
if (domain_present) {
if (domain_sublist[0].indexOf("~") != -1) {
for (n = 0; n < domain_sublist.length; n++) {
if (urlModule.URL(tabsModule.activeTab.url).host.match(escape_reg_exp(domain_sublist[n].replace("~", "")))) {
return false;
}
}
}
else {
var matched = false;
for (n = 0; n < domain_sublist.length; n++) {
if (urlModule.URL(tabsModule.activeTab.url).host.match(escape_reg_exp(domain_sublist[n]))) {
matched = true;
}
}
if (!matched) return false;
}
}
}
return true;
}
}
;
return false;
}
exports.categorize = categorize;
exports.load_list = load_list;
exports.is_Ad = is_Ad;