Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Commit d734e42

Browse files
committed
fine-tuning: getting closer to code freeze for cosmetic filters
1 parent 5612cd7 commit d734e42

File tree

1 file changed

+70
-108
lines changed

1 file changed

+70
-108
lines changed

js/abp-hide-filters.js

Lines changed: 70 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var histogram = function(label, buckets) {
5656
h.sort(function(a, b) { return b.n - a.n; });
5757
5858
// Find indices of entries of interest
59-
var target = 2;
59+
var target = 3;
6060
for ( var i = 0; i < total; i++ ) {
6161
if ( h[i].n === target ) {
6262
console.log('\tEntries with only %d filter(s) start at index %s (key = "%s")', target, i, h[i].k);
@@ -111,52 +111,20 @@ FilterPlainMore.prototype.retrieve = function(s, out) {
111111

112112
// HTML tag specific to a hostname
113113
// Examples:
114-
// lindaikeji.blogspot.com##a > img[height="600"]
115-
// japantimes.co.jp##table[align="right"][width="250"]
116-
117-
var FilterElementHostname = function(s, hostname) {
118-
this.s = s;
119-
this.hostname = hostname;
120-
};
121-
122-
FilterElementHostname.prototype.retrieve = function(s, out) {
123-
if ( pageHostname.slice(-this.hostname.length) === this.hostname ) {
124-
out.push(this.s);
125-
}
126-
};
127-
128-
/******************************************************************************/
129-
130-
// Pure id- and class-based filters specific to a hostname
131-
// Examples:
132114
// search.snapdo.com###ABottomD
133115
// facebook.com##.-cx-PRIVATE-fbAdUnit__root
134-
135-
var FilterPlainHostname = function(s, hostname) {
136-
this.s = s;
137-
this.hostname = hostname;
138-
};
139-
140-
FilterPlainHostname.prototype.retrieve = function(s, out) {
141-
if ( pageHostname.slice(-this.hostname.length) === this.hostname ) {
142-
out.push(this.s);
143-
}
144-
};
145-
146-
/******************************************************************************/
147-
148-
// Pure id- and class-based filters with extra selector stuff following and
149-
// specific to a hostname
150-
// Examples:
151116
// sltrib.com###BLContainer + div[style="height:90px;"]
152117
// myps3.com.au##.Boxer[style="height: 250px;"]
118+
// lindaikeji.blogspot.com##a > img[height="600"]
119+
// japantimes.co.jp##table[align="right"][width="250"]
120+
// mobilephonetalk.com##[align="center"] > b > a[href^="http://tinyurl.com/"]
153121

154-
var FilterPlainMoreHostname = function(s, hostname) {
122+
var FilterHostname = function(s, hostname) {
155123
this.s = s;
156124
this.hostname = hostname;
157125
};
158126

159-
FilterPlainMoreHostname.prototype.retrieve = function(s, out) {
127+
FilterHostname.prototype.retrieve = function(s, out) {
160128
if ( pageHostname.slice(-this.hostname.length) === this.hostname ) {
161129
out.push(this.s);
162130
}
@@ -165,6 +133,12 @@ FilterPlainMoreHostname.prototype.retrieve = function(s, out) {
165133
/******************************************************************************/
166134
/******************************************************************************/
167135

136+
// TODO: evaluate the gain (if any) from avoiding the use of an array for when
137+
// there are only two filters (or three, etc.). I suppose there is a specific
138+
// number of filters below which using an array is more of an overhead than
139+
// using a couple of property members.
140+
// i.e. FilterBucket2, FilterBucket3, FilterBucketN.
141+
168142
var FilterBucket = function(a, b) {
169143
this.filters = [a, b];
170144
};
@@ -259,6 +233,7 @@ FilterParser.prototype.extractPlain = function() {
259233
return '';
260234
};
261235

236+
/******************************************************************************/
262237
/******************************************************************************/
263238

264239
var FilterContainer = function() {
@@ -273,6 +248,7 @@ var FilterContainer = function() {
273248
// Reset all, thus reducing to a minimum memory footprint of the context.
274249

275250
FilterContainer.prototype.reset = function() {
251+
this.filterParser.reset();
276252
this.acceptedCount = 0;
277253
this.processedCount = 0;
278254
this.filters = {};
@@ -292,11 +268,27 @@ FilterContainer.prototype.add = function(s) {
292268
// debugger;
293269
//}
294270

271+
// hostname-based filters: with a hostname, narrowing is good enough, no
272+
// need to further narrow.
273+
if ( parsed.hostnames.length ) {
274+
return this.addHostnameFilter(parsed);
275+
}
276+
277+
// no specific hostname, narrow using class or id.
295278
var selectorType = parsed.suffix.charAt(0);
296279
if ( selectorType === '#' || selectorType === '.' ) {
297280
return this.addPlainFilter(parsed);
298281
}
299282

283+
// no specific hostname, no class, no id.
284+
// TO IMPLEMENT
285+
// My idea of implementation so far is to return a pre-built container
286+
// of these very generic filter, and let the content script sort out
287+
// what it needs from it. Filters in that category are mostly
288+
// `a[href^="..."]` kind of filters.
289+
// Content script side, the unsorted container of selectors could be used
290+
// in a querySelectorAll() to figure which rules apply (if any), or they
291+
// could just all be injected undiscriminately (not good).
300292
if ( parsed.isElement() ) {
301293
return this.addElementFilter(parsed);
302294
}
@@ -307,11 +299,13 @@ FilterContainer.prototype.add = function(s) {
307299
/******************************************************************************/
308300

309301
FilterContainer.prototype.freeze = function() {
302+
this.filterParser.reset();
303+
310304
console.log('HTTPSB> adp-hide-filters.js: %d filters accepted', this.acceptedCount);
311305
console.log('HTTPSB> adp-hide-filters.js: %d filters processed', this.processedCount);
312306
console.log('HTTPSB> adp-hide-filters.js: coverage is %s%', (this.acceptedCount * 100 / this.processedCount).toFixed(1));
313307

314-
// histogram('allFilters', this.filters);
308+
//histogram('allFilters', this.filters);
315309
};
316310

317311
/******************************************************************************/
@@ -331,8 +325,8 @@ FilterContainer.prototype.freeze = function() {
331325
// |
332326
// +-- filter type ('#'=hide '@'=unhide)
333327
//
334-
// TODO: Keep trying to find a better hash (FNV32a?). Ideally, all buckets have
335-
// the same (low) number of filters.
328+
// TODO: Keep trying to find a better hash (ls 12 bits of FNV32a?).
329+
// Ideally, all buckets have the same (low) number of filters.
336330

337331
var makePrefixHash = function(type, prefix) {
338332
var len = prefix.length;
@@ -370,39 +364,60 @@ var makeSuffixHash = function(type, suffix) {
370364
Histogram for above hash generator:
371365
372366
Histogram allFilters
373-
Entries with only 2 filter(s) start at index 3107 (key = "@៩")
374-
Entries with only 1 filter(s) start at index 5887 (key = "#ꎜ")
367+
Entries with only 3 filter(s) start at index 1869 (key = "#镉")
368+
Entries with only 2 filter(s) start at index 3117 (key = "@ᶹ")
369+
Entries with only 1 filter(s) start at index 5902 (key = "#蹐")
375370
key=#叭 count=141
376371
key=#徭 count=101
377-
key=#雽 count=57
372+
key=#雽 count=59
373+
key=#ュ count=49
378374
key=#֭ count=47
379375
key=#節 count=42
380-
key=#ュ count=39
381376
key=#㼉 count=36
377+
key=#傭 count=36
382378
key=# count=35
383-
key=#傭 count=34
384379
key=#ᆳ count=32
385-
key=#教 count=31
386-
key=#홹 count=29
380+
key=#教 count=32
387381
key=#ꗴ count=29
388-
key=# count=27
382+
key=# count=29
389383
key=#敨 count=27
390384
key=#䓕 count=27
385+
key=#媭 count=27
391386
key=#㪉 count=26
392387
key=#ꪭ count=25
393388
key=#釭 count=24
394-
key=#嵩 count=24
395-
key=#ꕔ count=24
396389
key=#� count=24
390+
key=#ꕔ count=24
391+
key=#嵩 count=24
397392
key=#錀 count=23
398393
key=#ꗰ count=22
399-
key=#ꖭ count=22
400394
key=#酹 count=22
395+
key=#ꖭ count=22
401396
key=#৙ count=22
402-
key=#ㅩ count=21
403397
key=#꿴 count=21
398+
key=#ㅩ count=21
404399
key=#龭 count=21
405400
key=#施 count=21
401+
key=#ꂭ count=20
402+
key=#ꔄ count=20
403+
key=#� count=19
404+
key=#ય count=19
405+
key=#꽔 count=19
406+
key=#ꗼ count=19
407+
key=#鎀 count=19
408+
key=#ꕘ count=19
409+
key=#隹 count=19
410+
key=# count=19
411+
key=#璙 count=18
412+
key=#᤭ count=18
413+
key=#֯ count=18
414+
key=# count=18
415+
key=#䉏 count=17
416+
key=#ꔌ count=17
417+
key=# count=17
418+
key=#嫜 count=17
419+
key=#ᱭ count=17
420+
Total buckets count: 14348
406421
*/
407422

408423
/******************************************************************************/
@@ -412,9 +427,6 @@ FilterContainer.prototype.addPlainFilter = function(parsed) {
412427
if ( parsed.isPlainMore() ) {
413428
return this.addPlainMoreFilter(parsed);
414429
}
415-
if ( parsed.hostnames.length ) {
416-
return this.addPlainHostnameFilter(parsed);
417-
}
418430
var f = new FilterPlain(parsed.suffix);
419431
var hash = makeSuffixHash(parsed.filterType, parsed.suffix);
420432
this.addFilterEntry(hash, f);
@@ -423,31 +435,7 @@ FilterContainer.prototype.addPlainFilter = function(parsed) {
423435

424436
/******************************************************************************/
425437

426-
// rhill 2014-05-20: When a domain exists, just specify a generic selector.
427-
428-
FilterContainer.prototype.addPlainHostnameFilter = function(parsed) {
429-
var httpsburi = HTTPSB.URI;
430-
var f, hash;
431-
var hostnames = parsed.hostnames;
432-
var i = hostnames.length, hostname;
433-
while ( i-- ) {
434-
hostname = hostnames[i];
435-
if ( !hostname ) {
436-
continue;
437-
}
438-
f = new FilterPlainHostname(parsed.suffix, hostname);
439-
hash = makePrefixHash(parsed.filterType, httpsburi.domainFromHostname(hostname));
440-
this.addFilterEntry(hash, f);
441-
}
442-
this.acceptedCount += 1;
443-
};
444-
445-
/******************************************************************************/
446-
447438
FilterContainer.prototype.addPlainMoreFilter = function(parsed) {
448-
if ( parsed.hostnames.length ) {
449-
return this.addPlainMoreHostnameFilter(parsed);
450-
}
451439
var plainSelector = parsed.extractPlain();
452440
if ( plainSelector === '' ) {
453441
return;
@@ -462,11 +450,7 @@ FilterContainer.prototype.addPlainMoreFilter = function(parsed) {
462450

463451
// rhill 2014-05-20: When a domain exists, just specify a generic selector.
464452

465-
FilterContainer.prototype.addPlainMoreHostnameFilter = function(parsed) {
466-
var plainSelector = parsed.extractPlain();
467-
if ( plainSelector === '' ) {
468-
return;
469-
}
453+
FilterContainer.prototype.addHostnameFilter = function(parsed) {
470454
var httpsburi = HTTPSB.URI;
471455
var f, hash;
472456
var hostnames = parsed.hostnames;
@@ -476,7 +460,7 @@ FilterContainer.prototype.addPlainMoreHostnameFilter = function(parsed) {
476460
if ( !hostname ) {
477461
continue;
478462
}
479-
f = new FilterPlainMoreHostname(parsed.suffix, hostname);
463+
f = new FilterHostname(parsed.suffix, hostname);
480464
hash = makePrefixHash(parsed.filterType, httpsburi.domainFromHostname(hostname));
481465
this.addFilterEntry(hash, f);
482466
}
@@ -486,28 +470,6 @@ FilterContainer.prototype.addPlainMoreHostnameFilter = function(parsed) {
486470
/******************************************************************************/
487471

488472
FilterContainer.prototype.addElementFilter = function(parsed) {
489-
if ( parsed.hostnames.length ) {
490-
return this.addElementHostnameFilter(parsed);
491-
}
492-
};
493-
494-
/******************************************************************************/
495-
496-
FilterContainer.prototype.addElementHostnameFilter = function(parsed) {
497-
var httpsburi = HTTPSB.URI;
498-
var f, hash;
499-
var hostnames = parsed.hostnames;
500-
var i = hostnames.length, hostname;
501-
while ( i-- ) {
502-
hostname = hostnames[i];
503-
if ( !hostname ) {
504-
continue;
505-
}
506-
f = new FilterElementHostname(parsed.suffix, hostname);
507-
hash = makePrefixHash(parsed.filterType, httpsburi.domainFromHostname(hostname));
508-
this.addFilterEntry(hash, f);
509-
}
510-
this.acceptedCount += 1;
511473
};
512474

513475
/******************************************************************************/

0 commit comments

Comments
 (0)