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

Commit b4a286e

Browse files
committed
this allows to discriminate between plain selector and plain selector with stuff after
1 parent f85d038 commit b4a286e

File tree

2 files changed

+115
-11
lines changed

2 files changed

+115
-11
lines changed

js/abp-hide-filters.js

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ var histogram = function(label, buckets) {
7272
*/
7373
/******************************************************************************/
7474

75-
// Good for id- and class-based filters
75+
// Pure id- and class-based filters
76+
// Examples:
77+
// #A9AdsMiddleBoxTop
78+
// .AD-POST
7679

7780
var FilterPlain = function(s) {
7881
this.s = s;
@@ -86,6 +89,29 @@ FilterPlain.prototype.retrieve = function(s, out) {
8689

8790
/******************************************************************************/
8891

92+
// Id- and class-based filters with extra selector stuff following.
93+
// Examples:
94+
// #center_col > div[style="font-size:14px;margin-right:0;min-height:5px"] ...
95+
// #adframe:not(frameset)
96+
// .l-container > #fishtank
97+
98+
var FilterPlainMore = function(s) {
99+
this.s = s;
100+
};
101+
102+
FilterPlainMore.prototype.retrieve = function(s, out) {
103+
if ( s === this.s.slice(0, s.length) ) {
104+
out.push(this.s);
105+
}
106+
};
107+
108+
/******************************************************************************/
109+
110+
// Pure id- and class-based filters sepcific to a hostname
111+
// Examples:
112+
// search.snapdo.com###ABottomD
113+
// facebook.com##.-cx-PRIVATE-fbAdUnit__root
114+
89115
var FilterPlainHostname = function(s, hostname) {
90116
this.s = s;
91117
this.hostname = hostname;
@@ -97,6 +123,24 @@ FilterPlainHostname.prototype.retrieve = function(s, out) {
97123
}
98124
};
99125

126+
/******************************************************************************/
127+
128+
// Pure id- and class-based filters sepcific to a hostname
129+
// Examples:
130+
// sltrib.com###BLContainer + div[style="height:90px;"]
131+
// myps3.com.au##.Boxer[style="height: 250px;"]
132+
133+
var FilterPlainMoreHostname = function(s, hostname) {
134+
this.s = s;
135+
this.hostname = hostname;
136+
};
137+
138+
FilterPlainMoreHostname.prototype.retrieve = function(s, out) {
139+
if ( s === this.s.slice(0, s.length) && pageHostname.slice(-this.hostname.length) === this.hostname ) {
140+
out.push(this.s);
141+
}
142+
};
143+
100144
/******************************************************************************/
101145
/******************************************************************************/
102146

@@ -125,6 +169,7 @@ var FilterParser = function() {
125169
this.hostnames = [];
126170
this.invalid = false;
127171
this.unsupported = false;
172+
this.rePlain = /#[#@]([#.][\w-]+)$/;
128173
};
129174

130175
/******************************************************************************/
@@ -176,11 +221,23 @@ FilterParser.prototype.parse = function(s) {
176221

177222
/******************************************************************************/
178223

224+
FilterParser.prototype.extractPlain = function() {
225+
var matches = this.rePlain.exec(this.f);
226+
if ( matches && matches.length === 2 ) {
227+
return matches[1];
228+
}
229+
return '';
230+
};
231+
232+
/******************************************************************************/
233+
179234
var FilterContainer = function() {
180235
this.filterParser = new FilterParser();
181236
this.acceptedCount = 0;
182237
this.rejectedCount = 0;
183238
this.filters = {};
239+
this.rePlain = /^##[#.][\w-]+$/;
240+
this.rePlainMore = /^##[#.][\w-]+[^\w-]/;
184241
};
185242

186243
/******************************************************************************/
@@ -215,6 +272,10 @@ FilterContainer.prototype.add = function(s) {
215272
/******************************************************************************/
216273

217274
FilterContainer.prototype.freeze = function() {
275+
console.log('HTTPSB> adp-hide-filters.js: %d filters accepted', this.acceptedCount);
276+
console.log('HTTPSB> adp-hide-filters.js: %d filters rejected', this.rejectedCount);
277+
console.log('HTTPSB> adp-hide-filters.js: coverage is %s%', (this.acceptedCount * 100 / (this.acceptedCount+this.rejectedCount)).toFixed(1));
278+
218279
// histogram('allFilters', this.filters);
219280
};
220281

@@ -224,8 +285,7 @@ FilterContainer.prototype.makeHash = function(filterType, selector, domain) {
224285
var i = (selector.length - 1) >> 2;
225286
var hash = String.fromCharCode(
226287
filterType.charCodeAt(0) << 8 |
227-
selector.charCodeAt(0)
228-
,
288+
selector.charCodeAt(0),
229289
(selector.charCodeAt(1) & 0xF) << 12 |
230290
(selector.charCodeAt(1+i) & 0xF) << 8 |
231291
(selector.charCodeAt(1+i+i) & 0xF) << 4 |
@@ -246,18 +306,21 @@ FilterContainer.prototype.makeHash = function(filterType, selector, domain) {
246306
/******************************************************************************/
247307

248308
FilterContainer.prototype.addPlainFilter = function(parsed) {
309+
// Verify whether the plain selector is followed by extra selector stuff
310+
if ( this.rePlainMore.test(parsed.f) ) {
311+
return this.addPlainMoreFilter(parsed);
312+
}
249313
if ( parsed.hostnames.length ) {
250-
return this.addPlainFilterHostname(parsed);
314+
return this.addPlainHostnameFilter(parsed);
251315
}
252316
var f = new FilterPlain(parsed.f);
253317
var hash = this.makeHash(parsed.filterType, parsed.f);
254318
this.addFilterEntry(hash, f);
255-
return true;
256319
};
257320

258321
/******************************************************************************/
259322

260-
FilterContainer.prototype.addPlainFilterHostname = function(parsed) {
323+
FilterContainer.prototype.addPlainHostnameFilter = function(parsed) {
261324
var httpsburi = HTTPSB.URI;
262325
var f, hash;
263326
var hostnames = parsed.hostnames;
@@ -276,6 +339,44 @@ FilterContainer.prototype.addPlainFilterHostname = function(parsed) {
276339

277340
/******************************************************************************/
278341

342+
FilterContainer.prototype.addPlainMoreFilter = function(parsed) {
343+
if ( parsed.hostnames.length ) {
344+
return this.addPlainMoreHostnameFilter(parsed);
345+
}
346+
var plainSelector = parsed.extractPlain();
347+
if ( plainSelector === '' ) {
348+
return;
349+
}
350+
var f = new FilterPlainMore(parsed.f);
351+
var hash = this.makeHash(parsed.filterType, plainSelector);
352+
this.addFilterEntry(hash, f);
353+
};
354+
355+
/******************************************************************************/
356+
357+
FilterContainer.prototype.addPlainMoreHostnameFilter = function(parsed) {
358+
var plainSelector = parsed.extractPlain();
359+
if ( plainSelector === '' ) {
360+
return;
361+
}
362+
var httpsburi = HTTPSB.URI;
363+
var f, hash;
364+
var hostnames = parsed.hostnames;
365+
var i = hostnames.length;
366+
var hostname;
367+
while ( i-- ) {
368+
hostname = hostnames[i];
369+
if ( !hostname ) {
370+
continue;
371+
}
372+
f = new FilterPlainMoreHostname(parsed.f, hostname);
373+
hash = this.makeHash(parsed.filterType, parsed.f, httpsburi.domainFromHostname(hostname));
374+
this.addFilterEntry(hash, plainSelector);
375+
}
376+
};
377+
378+
/******************************************************************************/
379+
279380
FilterContainer.prototype.addFilterEntry = function(hash, f) {
280381
var bucket = this.filters[hash];
281382
if ( bucket === undefined ) {
@@ -320,7 +421,10 @@ FilterContainer.prototype.retrieve = function(url, inSelectors) {
320421
bucket.retrieve(selector, donthideSelectors);
321422
}
322423
}
323-
return { hide: hideSelectors, donthide: donthideSelectors };
424+
return {
425+
hide: hideSelectors.join(','),
426+
donthide: donthideSelectors.join(',')
427+
};
324428
};
325429

326430
/******************************************************************************/

js/contentscript-elemhide.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ var retrieveHandler = function(selectors) {
2828
var styleText = [];
2929
if ( selectors.hide.length > 0 ) {
3030
var hideStyleText = '{{hideSelectors}} {display:none;}'
31-
.replace('{{hideSelectors}}', selectors.hide.join(','));
31+
.replace('{{hideSelectors}}', selectors.hide);
3232
styleText.push(hideStyleText);
33-
console.log('ABP cosmetic filters: injecting CSS rule:', hideStyleText);
33+
console.log('HTTPSB> ABP cosmetic filters: injecting CSS rule:', hideStyleText);
3434
}
3535
if ( selectors.donthide.length > 0 ) {
3636
var dontHideStyleText = '{{donthideSelectors}} {display:initial;}'
37-
.replace('{{donthideSelectors}}', selectors.donthide.join(','));
37+
.replace('{{donthideSelectors}}', selectors.donthide);
3838
styleText.push(donthideStyleText);
39-
console.log('ABP cosmetic filters: injecting CSS rule:', donthideStyleText);
39+
console.log('HTTPSB> ABP cosmetic filters: injecting CSS rule:', donthideStyleText);
4040
}
4141
if ( styleText.length > 0 ) {
4242
var style = document.createElement('style');

0 commit comments

Comments
 (0)