Left: | ||
Right: |
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 [included.map(matchDomain), filter.selector]; |
Sebastian Noack
2016/02/15 19:48:48
How about returning an object here? That would mak
kzar
2016/02/16 15:05:08
Done.
| |
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; | |
273 let targetDomains = result[0]; | |
274 let selector = result[1]; | |
287 | 275 |
288 //for (let filter of elemhideFilters) | 276 if (targetDomains.length == 0) |
289 // convertElemHideFilter(filter).forEach(addRule); | 277 targetDomains = ["^https?://"]; |
290 //for (let filter of elemhideExceptions) | 278 |
291 // addRule(convertFilter(filter, "ignore-previous-rules", false)); | 279 for (let domain of targetDomains) |
280 { | |
281 if (!groupedElemhideFilters.has(domain)) | |
Sebastian Noack
2016/02/15 19:48:48
The additional lookup is redundant. IMO, better:
kzar
2016/02/16 15:05:08
Done, kind of.
We're either going to have to perf
| |
282 groupedElemhideFilters.set(domain, []); | |
283 groupedElemhideFilters.get(domain).push(selector); | |
284 } | |
285 } | |
286 | |
287 groupedElemhideFilters.forEach((selectors, domain) => | |
288 { | |
289 while (selectors.length) | |
290 { | |
291 addRule({ | |
292 trigger: {"url-filter": domain}, | |
Sebastian Noack
2016/02/15 19:48:47
The variable name isn't accurate. It's not a domai
kzar
2016/02/16 15:05:08
Acknowledged.
| |
293 action: {type: "css-display-none"}, | |
294 selector: selectors.splice(0, selectorLimit).join(", ") | |
295 }); | |
296 } | |
297 }); | |
298 | |
299 for (let filter of elemhideExceptions) | |
300 addRule(convertFilter(filter, "ignore-previous-rules", false)); | |
292 | 301 |
293 for (let filter of requestFilters) | 302 for (let filter of requestFilters) |
294 addRule(convertFilter(filter, "block", true)); | 303 addRule(convertFilter(filter, "block", true)); |
295 for (let filter of requestExceptions) | 304 for (let filter of requestExceptions) |
296 addRule(convertFilter(filter, "ignore-previous-rules", true)); | 305 addRule(convertFilter(filter, "ignore-previous-rules", true)); |
297 | 306 |
298 console.log(JSON.stringify(rules, null, "\t")); | 307 console.log(JSON.stringify(rules, null, "\t")); |
299 } | 308 } |
300 | 309 |
301 let rl = readline.createInterface({input: process.stdin, terminal: false}); | 310 let rl = readline.createInterface({input: process.stdin, terminal: false}); |
302 rl.on("line", parseFilter); | 311 rl.on("line", parseFilter); |
303 rl.on("close", logRules); | 312 rl.on("close", logRules); |
OLD | NEW |