| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 /** @module abp2blocklist */ | 18 /** @module abp2blocklist */ |
| 19 | 19 |
| 20 "use strict"; | 20 "use strict"; |
| 21 | 21 |
| 22 let filterClasses = require("filterClasses"); | 22 let filterClasses = require("filterClasses"); |
| 23 let tldjs = require("tldjs"); | 23 let tldjs = require("tldjs"); |
| 24 let punycode = require("punycode"); | 24 let punycode = require("punycode"); |
| 25 | 25 |
| 26 const selectorLimit = 5000; | 26 const selectorLimit = 5000; |
| 27 const typeMap = filterClasses.RegExpFilter.typeMap; | 27 const typeMap = filterClasses.RegExpFilter.typeMap; |
| 28 const whitelistableRequestTypes = (typeMap.IMAGE | |
| 29 | typeMap.STYLESHEET | |
| 30 | typeMap.SCRIPT | |
| 31 | typeMap.FONT | |
| 32 | typeMap.MEDIA | |
| 33 | typeMap.POPUP | |
| 34 | typeMap.OBJECT | |
| 35 | typeMap.OBJECT_SUBREQUEST | |
| 36 | typeMap.XMLHTTPREQUEST | |
| 37 | typeMap.PING | |
| 38 | typeMap.SUBDOCUMENT | |
| 39 | typeMap.OTHER); | |
| 28 | 40 |
| 29 function parseDomains(domains, included, excluded) | 41 function parseDomains(domains, included, excluded) |
| 30 { | 42 { |
| 31 for (let domain in domains) | 43 for (let domain in domains) |
| 32 { | 44 { |
| 33 if (domain != "") | 45 if (domain != "") |
| 34 { | 46 { |
| 35 let enabled = domains[domain]; | 47 let enabled = domains[domain]; |
| 36 domain = punycode.toASCII(domain.toLowerCase()); | 48 domain = punycode.toASCII(domain.toLowerCase()); |
| 37 | 49 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 filter.contentType & typeMap.DOCUMENT && | 240 filter.contentType & typeMap.DOCUMENT && |
| 229 parsed.justHostname) | 241 parsed.justHostname) |
| 230 { | 242 { |
| 231 rules.push({ | 243 rules.push({ |
| 232 trigger: { | 244 trigger: { |
| 233 "url-filter": ".*", | 245 "url-filter": ".*", |
| 234 "if-domain": addDomainPrefix([parsed.hostname]) | 246 "if-domain": addDomainPrefix([parsed.hostname]) |
| 235 }, | 247 }, |
| 236 action: {type: "ignore-previous-rules"} | 248 action: {type: "ignore-previous-rules"} |
| 237 }); | 249 }); |
| 238 // If the filter contains multiple options we'll need to generate further | 250 // If the filter contains other supported options we'll need to generate |
| 239 // rules for it, but if not we can simply return now. | 251 // further rules for it, but if not we can simply return now. |
| 240 if (filter.contentType == typeMap.DOCUMENT) | 252 if (!(filter.contentType | whitelistableRequestTypes)) |
|
Sebastian Noack
2016/05/17 10:55:24
What if the filter is @@||example.com$document,ele
kzar
2016/05/17 11:23:34
Done.
| |
| 241 return rules; | 253 return; |
| 242 } | 254 } |
| 243 | 255 |
| 244 let trigger = {"url-filter": parsed.regexp}; | 256 let trigger = {"url-filter": parsed.regexp}; |
| 245 | 257 |
| 246 // Limit rules to HTTP(S) URLs | 258 // Limit rules to HTTP(S) URLs |
| 247 if (!/^(\^|http)/i.test(trigger["url-filter"])) | 259 if (!/^(\^|http)/i.test(trigger["url-filter"])) |
| 248 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"]; | 260 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"]; |
| 249 | 261 |
| 250 // For rules containing only a hostname we know that we're matching against | 262 // For rules containing only a hostname we know that we're matching against |
| 251 // a lowercase string unless the matchCase option was passed. | 263 // a lowercase string unless the matchCase option was passed. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 264 trigger["resource-type"] = getResourceTypes(filter); | 276 trigger["resource-type"] = getResourceTypes(filter); |
| 265 if (filter.thirdParty != null) | 277 if (filter.thirdParty != null) |
| 266 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; | 278 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; |
| 267 | 279 |
| 268 if (included.length > 0) | 280 if (included.length > 0) |
| 269 trigger["if-domain"] = addDomainPrefix(included); | 281 trigger["if-domain"] = addDomainPrefix(included); |
| 270 else if (excluded.length > 0) | 282 else if (excluded.length > 0) |
| 271 trigger["unless-domain"] = addDomainPrefix(excluded); | 283 trigger["unless-domain"] = addDomainPrefix(excluded); |
| 272 | 284 |
| 273 rules.push({trigger: trigger, action: {type: action}}); | 285 rules.push({trigger: trigger, action: {type: action}}); |
| 274 | |
| 275 return rules; | |
|
Sebastian Noack
2016/05/17 10:55:24
You don't have to return the rules here anymore.
kzar
2016/05/17 11:23:34
Oops, Done.
| |
| 276 } | 286 } |
| 277 | 287 |
| 278 function hasNonASCI(obj) | 288 function hasNonASCI(obj) |
| 279 { | 289 { |
| 280 if (typeof obj == "string") | 290 if (typeof obj == "string") |
| 281 { | 291 { |
| 282 if (/[^\x00-\x7F]/.test(obj)) | 292 if (/[^\x00-\x7F]/.test(obj)) |
| 283 return true; | 293 return true; |
| 284 } | 294 } |
| 285 | 295 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 return; | 386 return; |
| 377 if (filter instanceof filterClasses.RegExpFilter && | 387 if (filter instanceof filterClasses.RegExpFilter && |
| 378 filter.regexpSource == null) | 388 filter.regexpSource == null) |
| 379 return; | 389 return; |
| 380 | 390 |
| 381 if (filter instanceof filterClasses.BlockingFilter) | 391 if (filter instanceof filterClasses.BlockingFilter) |
| 382 this.requestFilters.push(filter); | 392 this.requestFilters.push(filter); |
| 383 | 393 |
| 384 if (filter instanceof filterClasses.WhitelistFilter) | 394 if (filter instanceof filterClasses.WhitelistFilter) |
| 385 { | 395 { |
| 386 if (filter.contentType & (typeMap.DOCUMENT | 396 if (filter.contentType & (typeMap.DOCUMENT | whitelistableRequestTypes)) |
| 387 | typeMap.IMAGE | |
| 388 | typeMap.STYLESHEET | |
| 389 | typeMap.SCRIPT | |
| 390 | typeMap.FONT | |
| 391 | typeMap.MEDIA | |
| 392 | typeMap.POPUP | |
| 393 | typeMap.OBJECT | |
| 394 | typeMap.OBJECT_SUBREQUEST | |
| 395 | typeMap.XMLHTTPREQUEST | |
| 396 | typeMap.PING | |
| 397 | typeMap.SUBDOCUMENT | |
| 398 | typeMap.OTHER)) | |
| 399 this.requestExceptions.push(filter); | 397 this.requestExceptions.push(filter); |
| 400 | 398 |
| 401 if (filter.contentType & typeMap.ELEMHIDE) | 399 if (filter.contentType & typeMap.ELEMHIDE) |
| 402 this.elemhideExceptions.push(filter); | 400 this.elemhideExceptions.push(filter); |
| 403 } | 401 } |
| 404 | 402 |
| 405 if (filter instanceof filterClasses.ElemHideFilter) | 403 if (filter instanceof filterClasses.ElemHideFilter) |
| 406 this.elemhideFilters.push(filter); | 404 this.elemhideFilters.push(filter); |
| 407 | 405 |
| 408 if (filter instanceof filterClasses.ElemHideException) | 406 if (filter instanceof filterClasses.ElemHideException) |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 463 | 461 |
| 464 for (let filter of this.elemhideExceptions) | 462 for (let filter of this.elemhideExceptions) |
| 465 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); | 463 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); |
| 466 for (let filter of this.requestFilters) | 464 for (let filter of this.requestFilters) |
| 467 convertFilterAddRules(rules, filter, "block", true); | 465 convertFilterAddRules(rules, filter, "block", true); |
| 468 for (let filter of this.requestExceptions) | 466 for (let filter of this.requestExceptions) |
| 469 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); | 467 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); |
| 470 | 468 |
| 471 return rules.filter(rule => !hasNonASCI(rule)); | 469 return rules.filter(rule => !hasNonASCI(rule)); |
| 472 }; | 470 }; |
| LEFT | RIGHT |