| OLD | NEW |
| 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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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"); | |
| 24 let punycode = require("punycode"); | 23 let punycode = require("punycode"); |
| 25 | 24 |
| 26 const selectorLimit = 5000; | 25 const selectorLimit = 5000; |
| 27 const typeMap = filterClasses.RegExpFilter.typeMap; | 26 const typeMap = filterClasses.RegExpFilter.typeMap; |
| 28 const whitelistableRequestTypes = (typeMap.IMAGE | 27 const whitelistableRequestTypes = (typeMap.IMAGE |
| 29 | typeMap.STYLESHEET | 28 | typeMap.STYLESHEET |
| 30 | typeMap.SCRIPT | 29 | typeMap.SCRIPT |
| 31 | typeMap.FONT | 30 | typeMap.FONT |
| 32 | typeMap.MEDIA | 31 | typeMap.MEDIA |
| 33 | typeMap.POPUP | 32 | typeMap.POPUP |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 typeMap.OBJECT_SUBREQUEST | | 207 typeMap.OBJECT_SUBREQUEST | |
| 209 typeMap.PING | | 208 typeMap.PING | |
| 210 typeMap.OTHER)) | 209 typeMap.OTHER)) |
| 211 types.push("raw"); | 210 types.push("raw"); |
| 212 if (filter.contentType & typeMap.SUBDOCUMENT) | 211 if (filter.contentType & typeMap.SUBDOCUMENT) |
| 213 types.push("document"); | 212 types.push("document"); |
| 214 | 213 |
| 215 return types; | 214 return types; |
| 216 } | 215 } |
| 217 | 216 |
| 218 function addDomainPrefix(domains) | |
| 219 { | |
| 220 let result = []; | |
| 221 | |
| 222 for (let domain of domains) | |
| 223 { | |
| 224 result.push(domain); | |
| 225 | |
| 226 if (tldjs.getDomain(domain) == domain) | |
| 227 result.push("www." + domain); | |
| 228 } | |
| 229 | |
| 230 return result; | |
| 231 } | |
| 232 | |
| 233 function convertFilterAddRules(rules, filter, action, withResourceTypes) | 217 function convertFilterAddRules(rules, filter, action, withResourceTypes) |
| 234 { | 218 { |
| 235 let parsed = parseFilterRegexpSource(filter.regexpSource); | 219 let parsed = parseFilterRegexpSource(filter.regexpSource); |
| 236 | 220 |
| 237 // For the special case of $document whitelisting filters with just a domain | 221 // For the special case of $document whitelisting filters with just a domain |
| 238 // we can generate an equivalent blocking rule exception using if-domain. | 222 // we can generate an equivalent blocking rule exception using if-domain. |
| 239 if (filter instanceof filterClasses.WhitelistFilter && | 223 if (filter instanceof filterClasses.WhitelistFilter && |
| 240 filter.contentType & typeMap.DOCUMENT && | 224 filter.contentType & typeMap.DOCUMENT && |
| 241 parsed.justHostname) | 225 parsed.justHostname) |
| 242 { | 226 { |
| 243 rules.push({ | 227 rules.push({ |
| 244 trigger: { | 228 trigger: { |
| 245 "url-filter": ".*", | 229 "url-filter": ".*", |
| 246 "if-domain": addDomainPrefix([parsed.hostname]) | 230 "if-domain": ["*" + parsed.hostname] |
| 247 }, | 231 }, |
| 248 action: {type: "ignore-previous-rules"} | 232 action: {type: "ignore-previous-rules"} |
| 249 }); | 233 }); |
| 250 // If the filter contains other supported options we'll need to generate | 234 // If the filter contains other supported options we'll need to generate |
| 251 // further rules for it, but if not we can simply return now. | 235 // further rules for it, but if not we can simply return now. |
| 252 if (!(filter.contentType & whitelistableRequestTypes)) | 236 if (!(filter.contentType & whitelistableRequestTypes)) |
| 253 return; | 237 return; |
| 254 } | 238 } |
| 255 | 239 |
| 256 let trigger = {"url-filter": parsed.regexp}; | 240 let trigger = {"url-filter": parsed.regexp}; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 277 trigger["resource-type"] = getResourceTypes(filter); | 261 trigger["resource-type"] = getResourceTypes(filter); |
| 278 | 262 |
| 279 if (trigger["resource-type"].length == 0) | 263 if (trigger["resource-type"].length == 0) |
| 280 return; | 264 return; |
| 281 } | 265 } |
| 282 | 266 |
| 283 if (filter.thirdParty != null) | 267 if (filter.thirdParty != null) |
| 284 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; | 268 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; |
| 285 | 269 |
| 286 if (included.length > 0) | 270 if (included.length > 0) |
| 287 trigger["if-domain"] = addDomainPrefix(included); | 271 trigger["if-domain"] = included.map(name => "*" + name); |
| 288 else if (excluded.length > 0) | 272 else if (excluded.length > 0) |
| 289 trigger["unless-domain"] = addDomainPrefix(excluded); | 273 trigger["unless-domain"] = excluded.map(name => "*" + name); |
| 290 | 274 |
| 291 rules.push({trigger: trigger, action: {type: action}}); | 275 rules.push({trigger: trigger, action: {type: action}}); |
| 292 } | 276 } |
| 293 | 277 |
| 294 function hasNonASCI(obj) | 278 function hasNonASCI(obj) |
| 295 { | 279 { |
| 296 if (typeof obj == "string") | 280 if (typeof obj == "string") |
| 297 { | 281 { |
| 298 if (/[^\x00-\x7F]/.test(obj)) | 282 if (/[^\x00-\x7F]/.test(obj)) |
| 299 return true; | 283 return true; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 | 451 |
| 468 for (let filter of this.elemhideExceptions) | 452 for (let filter of this.elemhideExceptions) |
| 469 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); | 453 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); |
| 470 for (let filter of this.requestFilters) | 454 for (let filter of this.requestFilters) |
| 471 convertFilterAddRules(rules, filter, "block", true); | 455 convertFilterAddRules(rules, filter, "block", true); |
| 472 for (let filter of this.requestExceptions) | 456 for (let filter of this.requestExceptions) |
| 473 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); | 457 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); |
| 474 | 458 |
| 475 return rules.filter(rule => !hasNonASCI(rule)); | 459 return rules.filter(rule => !hasNonASCI(rule)); |
| 476 }; | 460 }; |
| OLD | NEW |