OLD | NEW |
1 "use strict"; | 1 "use strict"; |
2 | 2 |
3 let readline = require("readline"); | 3 let readline = require("readline"); |
4 let punycode = require("punycode"); | 4 let punycode = require("punycode"); |
5 let tldjs = require("tldjs"); | 5 let tldjs = require("tldjs"); |
6 let filterClasses = require("./adblockplus.js"); | 6 let filterClasses = require("./adblockplus.js"); |
7 | 7 |
8 let typeMap = filterClasses.RegExpFilter.typeMap; | 8 let typeMap = filterClasses.RegExpFilter.typeMap; |
9 | 9 |
| 10 const selectorLimit = 5000; |
| 11 |
10 let requestFilters = []; | 12 let requestFilters = []; |
11 let requestExceptions = []; | 13 let requestExceptions = []; |
12 let elemhideFilters = []; | 14 let elemhideFilters = []; |
13 let elemhideExceptions = []; | 15 let elemhideExceptions = []; |
14 let elemhideSelectorExceptions = new Map(); | 16 let elemhideSelectorExceptions = new Map(); |
15 | 17 |
16 function recordException(filter) | 18 function recordException(filter) |
17 { | 19 { |
18 if (filter.contentType & (typeMap.IMAGE | 20 if (filter.contentType & (typeMap.IMAGE |
19 | typeMap.STYLESHEET | 21 | typeMap.STYLESHEET |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 95 |
94 function convertElemHideFilter(filter) | 96 function convertElemHideFilter(filter) |
95 { | 97 { |
96 let included = []; | 98 let included = []; |
97 let excluded = []; | 99 let excluded = []; |
98 let rules = []; | 100 let rules = []; |
99 | 101 |
100 parseDomains(filter.domains, included, excluded); | 102 parseDomains(filter.domains, included, excluded); |
101 | 103 |
102 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions)) | 104 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions)) |
103 { | 105 return {matchDomains: included.map(matchDomain), selector: filter.selector}; |
104 let action = { | |
105 type: "css-display-none", | |
106 selector: filter.selector | |
107 }; | |
108 | |
109 for (let domain of included) | |
110 rules.push({ | |
111 trigger: {"url-filter": matchDomain(domain)}, | |
112 action: action | |
113 }); | |
114 | |
115 if (included.length == 0) | |
116 rules.push({ | |
117 trigger: {"url-filter": "^https?://"}, | |
118 action: action | |
119 }); | |
120 } | |
121 | |
122 return rules; | |
123 } | 106 } |
124 | 107 |
125 function toRegExp(text) | 108 function toRegExp(text) |
126 { | 109 { |
127 let result = ""; | 110 let result = ""; |
128 let lastIndex = text.length - 1; | 111 let lastIndex = text.length - 1; |
129 | 112 |
130 for (let i = 0; i < text.length; i++) | 113 for (let i = 0; i < text.length; i++) |
131 { | 114 { |
132 let c = text[i]; | 115 let c = text[i]; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 function logRules() | 257 function logRules() |
275 { | 258 { |
276 let rules = []; | 259 let rules = []; |
277 | 260 |
278 function addRule(rule) | 261 function addRule(rule) |
279 { | 262 { |
280 if (!hasNonASCI(rule)) | 263 if (!hasNonASCI(rule)) |
281 rules.push(rule); | 264 rules.push(rule); |
282 } | 265 } |
283 | 266 |
284 // HACK: We ignore element hiding filter for now to get the list of | 267 let groupedElemhideFilters = new Map(); |
285 // rules down below 50K. This limit is enforced by iOS and Safari. | 268 for (let filter of elemhideFilters) |
286 // To be undone with https://issues.adblockplus.org/ticket/3585 | 269 { |
| 270 let result = convertElemHideFilter(filter); |
| 271 if (!result) |
| 272 continue; |
287 | 273 |
288 //for (let filter of elemhideFilters) | 274 if (result.matchDomains.length == 0) |
289 // convertElemHideFilter(filter).forEach(addRule); | 275 result.matchDomains = ["^https?://"]; |
290 //for (let filter of elemhideExceptions) | 276 |
291 // addRule(convertFilter(filter, "ignore-previous-rules", false)); | 277 for (let matchDomain of result.matchDomains) |
| 278 { |
| 279 let group = groupedElemhideFilters.get(matchDomain) || []; |
| 280 group.push(result.selector); |
| 281 groupedElemhideFilters.set(matchDomain, group); |
| 282 } |
| 283 } |
| 284 |
| 285 groupedElemhideFilters.forEach((selectors, domain) => |
| 286 { |
| 287 while (selectors.length) |
| 288 { |
| 289 addRule({ |
| 290 trigger: {"url-filter": domain}, |
| 291 action: {type: "css-display-none"}, |
| 292 selector: selectors.splice(0, selectorLimit).join(", ") |
| 293 }); |
| 294 } |
| 295 }); |
| 296 |
| 297 for (let filter of elemhideExceptions) |
| 298 addRule(convertFilter(filter, "ignore-previous-rules", false)); |
292 | 299 |
293 for (let filter of requestFilters) | 300 for (let filter of requestFilters) |
294 addRule(convertFilter(filter, "block", true)); | 301 addRule(convertFilter(filter, "block", true)); |
295 for (let filter of requestExceptions) | 302 for (let filter of requestExceptions) |
296 addRule(convertFilter(filter, "ignore-previous-rules", true)); | 303 addRule(convertFilter(filter, "ignore-previous-rules", true)); |
297 | 304 |
298 console.log(JSON.stringify(rules, null, "\t")); | 305 console.log(JSON.stringify(rules, null, "\t")); |
299 } | 306 } |
300 | 307 |
301 let rl = readline.createInterface({input: process.stdin, terminal: false}); | 308 let rl = readline.createInterface({input: process.stdin, terminal: false}); |
302 rl.on("line", parseFilter); | 309 rl.on("line", parseFilter); |
303 rl.on("close", logRules); | 310 rl.on("close", logRules); |
OLD | NEW |