2014-06-23 16:42:43 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
µBlock - a Chromium browser extension to block requests.
|
|
|
|
Copyright (C) 2014 Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* jshint bitwise: false */
|
|
|
|
/* global µBlock */
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
µBlock.cosmeticFilteringEngine = (function(){
|
2014-06-23 16:42:43 -06:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var µb = µBlock;
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Could be replaced with encodeURIComponent/decodeURIComponent,
|
|
|
|
// which seems faster on Firefox.
|
|
|
|
var encode = JSON.stringify;
|
|
|
|
var decode = JSON.parse;
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/*
|
|
|
|
var histogram = function(label, buckets) {
|
|
|
|
var h = [],
|
|
|
|
bucket;
|
|
|
|
for ( var k in buckets ) {
|
|
|
|
if ( buckets.hasOwnProperty(k) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bucket = buckets[k];
|
|
|
|
h.push({
|
|
|
|
k: k,
|
|
|
|
n: bucket instanceof FilterBucket ? bucket.filters.length : 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Histogram %s', label);
|
|
|
|
|
|
|
|
var total = h.length;
|
|
|
|
h.sort(function(a, b) { return b.n - a.n; });
|
|
|
|
|
|
|
|
// Find indices of entries of interest
|
|
|
|
var target = 3;
|
|
|
|
for ( var i = 0; i < total; i++ ) {
|
|
|
|
if ( h[i].n === target ) {
|
|
|
|
console.log('\tEntries with only %d filter(s) start at index %s (key = "%s")', target, i, h[i].k);
|
|
|
|
target -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h = h.slice(0, 50);
|
|
|
|
|
|
|
|
h.forEach(function(v) {
|
|
|
|
console.log('\tkey="%s" count=%d', v.k, v.n);
|
|
|
|
});
|
|
|
|
console.log('\tTotal buckets count: %d', total);
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Pure id- and class-based filters
|
|
|
|
// Examples:
|
|
|
|
// #A9AdsMiddleBoxTop
|
|
|
|
// .AD-POST
|
|
|
|
|
|
|
|
var FilterPlain = function(s) {
|
|
|
|
this.s = s;
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterPlain.prototype.retrieve = function(s, out) {
|
|
|
|
if ( s === this.s ) {
|
|
|
|
out.push(this.s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
FilterPlain.prototype.fid = '#';
|
|
|
|
|
|
|
|
FilterPlain.prototype.toSelfie = function() {
|
|
|
|
return this.s;
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterPlain.fromSelfie = function(s) {
|
|
|
|
return new FilterPlain(s);
|
|
|
|
};
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Id- and class-based filters with extra selector stuff following.
|
|
|
|
// Examples:
|
|
|
|
// #center_col > div[style="font-size:14px;margin-right:0;min-height:5px"] ...
|
|
|
|
// #adframe:not(frameset)
|
|
|
|
// .l-container > #fishtank
|
|
|
|
|
|
|
|
var FilterPlainMore = function(s) {
|
|
|
|
this.s = s;
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterPlainMore.prototype.retrieve = function(s, out) {
|
|
|
|
if ( s === this.s.slice(0, s.length) ) {
|
|
|
|
out.push(this.s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
FilterPlainMore.prototype.fid = '#+';
|
|
|
|
|
|
|
|
FilterPlainMore.prototype.toSelfie = function() {
|
|
|
|
return this.s;
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterPlainMore.fromSelfie = function(s) {
|
|
|
|
return new FilterPlainMore(s);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var FilterBucket = function(a, b) {
|
|
|
|
this.f = null;
|
|
|
|
this.filters = [];
|
|
|
|
if ( a !== undefined ) {
|
|
|
|
this.filters[0] = a;
|
|
|
|
if ( b !== undefined ) {
|
|
|
|
this.filters[1] = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterBucket.prototype.add = function(a) {
|
|
|
|
this.filters.push(a);
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterBucket.prototype.retrieve = function(s, out) {
|
|
|
|
var i = this.filters.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
this.filters[i].retrieve(s, out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterBucket.prototype.fid = '[]';
|
|
|
|
|
|
|
|
FilterBucket.prototype.toSelfie = function() {
|
|
|
|
return this.filters.length.toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterBucket.fromSelfie = function() {
|
|
|
|
return new FilterBucket();
|
|
|
|
};
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Any selector specific to a hostname
|
|
|
|
// Examples:
|
|
|
|
// search.snapdo.com###ABottomD
|
|
|
|
// facebook.com##.-cx-PRIVATE-fbAdUnit__root
|
|
|
|
// sltrib.com###BLContainer + div[style="height:90px;"]
|
|
|
|
// myps3.com.au##.Boxer[style="height: 250px;"]
|
|
|
|
// lindaikeji.blogspot.com##a > img[height="600"]
|
|
|
|
// japantimes.co.jp##table[align="right"][width="250"]
|
|
|
|
// mobilephonetalk.com##[align="center"] > b > a[href^="http://tinyurl.com/"]
|
|
|
|
|
|
|
|
var FilterHostname = function(s, hostname) {
|
|
|
|
this.s = s;
|
|
|
|
this.hostname = hostname;
|
|
|
|
};
|
|
|
|
|
2014-07-13 00:36:38 -06:00
|
|
|
FilterHostname.prototype.retrieve = function(hostname, out) {
|
|
|
|
if ( hostname.slice(-this.hostname.length) === this.hostname ) {
|
|
|
|
out.push(this.s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
FilterHostname.prototype.fid = 'h';
|
|
|
|
|
|
|
|
FilterHostname.prototype.toSelfie = function() {
|
|
|
|
return encode(this.s) + '\t' + this.hostname;
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterHostname.fromSelfie = function(s) {
|
|
|
|
var pos = s.indexOf('\t');
|
|
|
|
return new FilterHostname(decode(s.slice(0, pos)), s.slice(pos + 1));
|
|
|
|
};
|
|
|
|
|
2014-08-14 11:59:37 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Any selector specific to an entity
|
|
|
|
// Examples:
|
|
|
|
// google.*###cnt #center_col > #res > #topstuff > .ts
|
|
|
|
|
|
|
|
var FilterEntity = function(s, entity) {
|
|
|
|
this.s = s;
|
|
|
|
this.entity = entity;
|
|
|
|
};
|
|
|
|
|
|
|
|
FilterEntity.prototype.retrieve = function(entity, out) {
|
|
|
|
if ( entity.slice(-this.entity.length) === this.entity ) {
|
|
|
|
out.push(this.s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
FilterEntity.prototype.fid = 'e';
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
FilterEntity.prototype.toSelfie = function() {
|
|
|
|
return encode(this.s) + '\t' + this.entity;
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
FilterEntity.fromSelfie = function(s) {
|
|
|
|
var pos = s.indexOf('\t');
|
|
|
|
return new FilterEntity(decode(s.slice(0, pos)), s.slice(pos + 1));
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var FilterParser = function() {
|
|
|
|
this.prefix = '';
|
|
|
|
this.suffix = '';
|
2014-08-12 10:19:54 -06:00
|
|
|
this.unhide = 0;
|
2014-06-23 16:42:43 -06:00
|
|
|
this.hostnames = [];
|
|
|
|
this.invalid = false;
|
|
|
|
this.reParser = /^\s*([^#]*)(##|#@#)(.+)\s*$/;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterParser.prototype.reset = function() {
|
|
|
|
this.prefix = '';
|
|
|
|
this.suffix = '';
|
2014-08-12 10:19:54 -06:00
|
|
|
this.unhide = 0;
|
|
|
|
this.hostnames.length = 0;
|
2014-06-23 16:42:43 -06:00
|
|
|
this.invalid = false;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterParser.prototype.parse = function(s) {
|
|
|
|
// important!
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
var matches = this.reParser.exec(s);
|
|
|
|
if ( matches === null || matches.length !== 4 ) {
|
|
|
|
this.invalid = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember original string
|
|
|
|
this.prefix = matches[1];
|
|
|
|
this.suffix = matches[3];
|
|
|
|
|
|
|
|
// 2014-05-23:
|
|
|
|
// https://github.com/gorhill/httpswitchboard/issues/260
|
|
|
|
// Any sequence of `#` longer than one means the line is not a valid
|
|
|
|
// cosmetic filter.
|
2014-08-12 10:19:54 -06:00
|
|
|
if ( this.suffix.indexOf('##') !== -1 ) {
|
2014-06-23 16:42:43 -06:00
|
|
|
this.invalid = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
// Normalize high-medium selectors: `href` is assumed to imply `a` tag. We
|
|
|
|
// need to do this here in order to correctly avoid duplicates. The test
|
|
|
|
// is designed to minimize overhead -- this is a low occurrence filter.
|
|
|
|
if ( this.suffix.charAt(1) === '[' && this.suffix.slice(2, 9) === 'href^="' ) {
|
|
|
|
this.suffix = this.suffix.slice(1);
|
|
|
|
}
|
|
|
|
|
2014-08-21 08:56:36 -06:00
|
|
|
this.unhide = matches[2].charAt(1) === '@' ? 1 : 0;
|
2014-06-23 16:42:43 -06:00
|
|
|
if ( this.prefix !== '' ) {
|
|
|
|
this.hostnames = this.prefix.split(/\s*,\s*/);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-13 18:03:55 -06:00
|
|
|
var SelectorCacheEntry = function() {
|
2014-08-14 11:59:37 -06:00
|
|
|
this.cosmetic = {};
|
|
|
|
this.net = {};
|
|
|
|
this.netCount = 0;
|
2014-08-13 18:03:55 -06:00
|
|
|
this.lastAccessTime = Date.now();
|
|
|
|
};
|
|
|
|
|
2014-08-14 11:59:37 -06:00
|
|
|
SelectorCacheEntry.prototype.netLowWaterMark = 20;
|
|
|
|
SelectorCacheEntry.prototype.netHighWaterMark = 30;
|
|
|
|
|
|
|
|
SelectorCacheEntry.prototype.addCosmetic = function(selectors) {
|
|
|
|
var dict = this.cosmetic;
|
|
|
|
var i = selectors.length || 0;
|
|
|
|
while ( i-- ) {
|
|
|
|
dict[selectors[i]] = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-15 08:34:13 -06:00
|
|
|
SelectorCacheEntry.prototype.addNet = function(selectors) {
|
|
|
|
if ( typeof selectors === 'string' ) {
|
|
|
|
this.addNetOne(selectors, Date.now());
|
|
|
|
} else {
|
|
|
|
this.addNetMany(selectors, Date.now());
|
2014-08-14 11:59:37 -06:00
|
|
|
}
|
|
|
|
// Net request-derived selectors: I limit the number of cached selectors,
|
|
|
|
// as I expect cases where the blocked net-requests are never the
|
|
|
|
// exact same URL.
|
2014-08-15 08:34:13 -06:00
|
|
|
if ( this.netCount < this.netHighWaterMark ) {
|
2014-08-14 11:59:37 -06:00
|
|
|
return;
|
|
|
|
}
|
2014-08-15 08:34:13 -06:00
|
|
|
var dict = this.net;
|
|
|
|
var keys = Object.keys(dict).sort(function(a, b) {
|
|
|
|
return dict[b] - dict[a];
|
|
|
|
}).slice(this.netLowWaterMark);
|
|
|
|
var i = keys.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
delete dict[keys[i]];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SelectorCacheEntry.prototype.addNetOne = function(selector, now) {
|
|
|
|
var dict = this.net;
|
|
|
|
if ( dict[selector] === undefined ) {
|
|
|
|
this.netCount += 1;
|
|
|
|
}
|
|
|
|
dict[selector] = now;
|
|
|
|
};
|
|
|
|
|
|
|
|
SelectorCacheEntry.prototype.addNetMany = function(selectors, now) {
|
|
|
|
var dict = this.net;
|
|
|
|
var i = selectors.length || 0;
|
|
|
|
var selector;
|
|
|
|
while ( i-- ) {
|
|
|
|
selector = selectors[i];
|
|
|
|
if ( dict[selector] === undefined ) {
|
|
|
|
this.netCount += 1;
|
2014-08-14 11:59:37 -06:00
|
|
|
}
|
2014-08-15 08:34:13 -06:00
|
|
|
dict[selector] = now;
|
2014-08-14 11:59:37 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SelectorCacheEntry.prototype.add = function(selectors, type) {
|
2014-08-13 18:03:55 -06:00
|
|
|
this.lastAccessTime = Date.now();
|
2014-08-14 11:59:37 -06:00
|
|
|
if ( type === 'cosmetic' ) {
|
|
|
|
this.addCosmetic(selectors);
|
|
|
|
} else {
|
|
|
|
this.addNet(selectors);
|
|
|
|
}
|
2014-08-13 18:03:55 -06:00
|
|
|
};
|
|
|
|
|
2014-08-14 11:59:37 -06:00
|
|
|
SelectorCacheEntry.prototype.retrieve = function(type, out) {
|
2014-08-13 18:03:55 -06:00
|
|
|
this.lastAccessTime = Date.now();
|
2014-08-14 11:59:37 -06:00
|
|
|
var dict = type === 'cosmetic' ? this.cosmetic : this.net;
|
|
|
|
for ( var selector in dict ) {
|
|
|
|
if ( dict.hasOwnProperty(selector) ) {
|
|
|
|
out.push(selector);
|
|
|
|
}
|
2014-08-13 18:03:55 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
// Two Unicode characters:
|
|
|
|
// T0HHHHHHH HHHHHHHHH
|
|
|
|
// | | |
|
|
|
|
// | | |
|
|
|
|
// | | |
|
|
|
|
// | | +-- bit 8-0 of FNV
|
|
|
|
// | |
|
|
|
|
// | +-- bit 15-9 of FNV
|
|
|
|
// |
|
|
|
|
// +-- filter type (0=hide 1=unhide)
|
|
|
|
//
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
var makeHash = function(unhide, token, mask) {
|
|
|
|
// Ref: Given a URL, returns a unique 4-character long hash string
|
|
|
|
// Based on: FNV32a
|
|
|
|
// http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-reference-source
|
|
|
|
// The rest is custom, suited for µBlock.
|
|
|
|
var i1 = token.length;
|
|
|
|
var i2 = i1 >> 1;
|
|
|
|
var i4 = i1 >> 2;
|
|
|
|
var i8 = i1 >> 3;
|
|
|
|
var hval = (0x811c9dc5 ^ token.charCodeAt(0)) >>> 0;
|
|
|
|
hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
|
|
|
|
hval >>>= 0;
|
|
|
|
hval ^= token.charCodeAt(i8);
|
|
|
|
hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
|
|
|
|
hval >>>= 0;
|
|
|
|
hval ^= token.charCodeAt(i4);
|
|
|
|
hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
|
|
|
|
hval >>>= 0;
|
|
|
|
hval ^= token.charCodeAt(i4+i8);
|
|
|
|
hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
|
|
|
|
hval >>>= 0;
|
|
|
|
hval ^= token.charCodeAt(i2);
|
|
|
|
hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
|
|
|
|
hval >>>= 0;
|
|
|
|
hval ^= token.charCodeAt(i2+i8);
|
|
|
|
hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
|
|
|
|
hval >>>= 0;
|
|
|
|
hval ^= token.charCodeAt(i2+i4);
|
|
|
|
hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
|
|
|
|
hval >>>= 0;
|
|
|
|
hval ^= token.charCodeAt(i1-1);
|
|
|
|
hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
|
|
|
|
hval >>>= 0;
|
|
|
|
hval &= mask;
|
|
|
|
if ( unhide !== 0 ) {
|
|
|
|
hval |= 0x20000;
|
|
|
|
}
|
|
|
|
return String.fromCharCode(hval >>> 9, hval & 0x1FF);
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
// Cosmetic filter family tree:
|
|
|
|
//
|
|
|
|
// Generic
|
|
|
|
// Low generic simple: class or id only
|
|
|
|
// Low generic complex: class or id + extra stuff after
|
|
|
|
// High generic:
|
|
|
|
// High-low generic: [alt="..."],[title="..."]
|
|
|
|
// High-medium generic: [href^="..."]
|
|
|
|
// High-high generic: everything else
|
|
|
|
// Specific
|
|
|
|
// Specfic hostname
|
2014-08-14 11:59:37 -06:00
|
|
|
// Specific entity
|
2014-08-12 10:19:54 -06:00
|
|
|
// Generic filters can only be enforced once the main document is loaded.
|
|
|
|
// Specific filers can be enforced before the main document is loaded.
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
var FilterContainer = function() {
|
2014-08-27 11:50:18 -06:00
|
|
|
this.domainHashMask = (1 << 10) - 1;
|
|
|
|
this.genericHashMask = (1 << 15) - 1;
|
|
|
|
this.type0NoDomainHash = 'type0NoDomain';
|
|
|
|
this.type1NoDomainHash = 'type1NoDomain';
|
2014-08-21 08:56:36 -06:00
|
|
|
this.parser = new FilterParser();
|
2014-07-15 18:32:33 -06:00
|
|
|
this.reset();
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Reset all, thus reducing to a minimum memory footprint of the context.
|
|
|
|
|
|
|
|
FilterContainer.prototype.reset = function() {
|
2014-08-21 08:56:36 -06:00
|
|
|
this.parser.reset();
|
2014-07-20 13:00:26 -06:00
|
|
|
this.frozen = false;
|
2014-06-23 16:42:43 -06:00
|
|
|
this.acceptedCount = 0;
|
2014-08-12 10:19:54 -06:00
|
|
|
this.duplicateCount = 0;
|
|
|
|
|
2014-08-13 18:03:55 -06:00
|
|
|
this.selectorCache = {};
|
|
|
|
this.selectorCacheCount = 0;
|
|
|
|
this.selectorCacheLowWaterMark = 75;
|
|
|
|
this.selectorCacheHighWaterMark = 100;
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
// temporary (at parse time)
|
|
|
|
this.lowGenericHide = {};
|
|
|
|
this.lowGenericDonthide = {};
|
|
|
|
this.highGenericHide = {};
|
|
|
|
this.highGenericDonthide = {};
|
2014-08-06 09:34:59 -06:00
|
|
|
this.hostnameHide = {};
|
|
|
|
this.hostnameDonthide = {};
|
2014-08-14 11:59:37 -06:00
|
|
|
this.entityHide = {};
|
|
|
|
this.entityDonthide = {};
|
2014-09-08 15:46:58 -06:00
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
// permanent
|
|
|
|
// [class], [id]
|
|
|
|
this.lowGenericFilters = {};
|
|
|
|
|
|
|
|
// [alt="..."], [title="..."]
|
|
|
|
this.highLowGenericHide = {};
|
|
|
|
this.highLowGenericDonthide = {};
|
|
|
|
this.highLowGenericHideCount = 0;
|
|
|
|
this.highLowGenericDonthideCount = 0;
|
|
|
|
|
|
|
|
// a[href^="http..."]
|
|
|
|
this.highMediumGenericHide = {};
|
|
|
|
this.highMediumGenericDonthide = {};
|
|
|
|
this.highMediumGenericHideCount = 0;
|
|
|
|
this.highMediumGenericDonthideCount = 0;
|
|
|
|
|
|
|
|
// everything else
|
2014-09-08 15:46:58 -06:00
|
|
|
this.highHighGenericHide = '';
|
|
|
|
this.highHighGenericDonthide = '';
|
2014-08-12 10:19:54 -06:00
|
|
|
this.highHighGenericHideCount = 0;
|
|
|
|
this.highHighGenericDonthideCount = 0;
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
// hostname, entity-based filters
|
2014-08-12 10:19:54 -06:00
|
|
|
this.hostnameFilters = {};
|
2014-08-14 11:59:37 -06:00
|
|
|
this.entityFilters = {};
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.add = function(s) {
|
2014-08-21 08:56:36 -06:00
|
|
|
var parsed = this.parser.parse(s);
|
2014-06-23 16:42:43 -06:00
|
|
|
if ( parsed.invalid ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
var hostnames = parsed.hostnames;
|
|
|
|
var i = hostnames.length;
|
|
|
|
if ( i === 0 ) {
|
|
|
|
this.addGenericSelector(parsed);
|
2014-08-12 18:25:11 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// https://github.com/gorhill/uBlock/issues/151
|
|
|
|
// Negated hostname means the filter applies to all non-negated hostnames
|
|
|
|
// of same filter OR globally if there is no non-negated hostnames.
|
|
|
|
var applyGlobally = true;
|
|
|
|
var hostname;
|
|
|
|
while ( i-- ) {
|
|
|
|
hostname = hostnames[i];
|
|
|
|
if ( hostname.charAt(0) !== '~' ) {
|
|
|
|
applyGlobally = false;
|
2014-08-12 10:19:54 -06:00
|
|
|
}
|
2014-08-14 18:00:44 -06:00
|
|
|
if ( hostname.slice(-2) === '.*' ) {
|
|
|
|
this.addEntitySelector(hostname, parsed);
|
|
|
|
} else {
|
|
|
|
this.addHostnameSelector(hostname, parsed);
|
|
|
|
}
|
2014-08-12 18:25:11 -06:00
|
|
|
}
|
|
|
|
if ( applyGlobally ) {
|
|
|
|
this.addGenericSelector(parsed);
|
2014-08-12 10:19:54 -06:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
/******************************************************************************/
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
FilterContainer.prototype.addGenericSelector = function(parsed) {
|
|
|
|
var entries;
|
|
|
|
var selectorType = parsed.suffix.charAt(0);
|
|
|
|
if ( selectorType === '#' || selectorType === '.' ) {
|
|
|
|
entries = parsed.unhide === 0 ?
|
|
|
|
this.lowGenericHide :
|
|
|
|
this.lowGenericDonthide;
|
|
|
|
} else {
|
|
|
|
entries = parsed.unhide === 0 ?
|
|
|
|
this.highGenericHide :
|
|
|
|
this.highGenericDonthide;
|
|
|
|
}
|
|
|
|
if ( entries[parsed.suffix] === undefined ) {
|
|
|
|
entries[parsed.suffix] = true;
|
|
|
|
this.acceptedCount += 1;
|
|
|
|
} else {
|
|
|
|
this.duplicateCount += 1;
|
2014-08-06 09:34:59 -06:00
|
|
|
}
|
2014-08-12 10:19:54 -06:00
|
|
|
return true;
|
|
|
|
};
|
2014-08-06 09:34:59 -06:00
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.addHostnameSelector = function(hostname, parsed) {
|
|
|
|
// https://github.com/gorhill/uBlock/issues/145
|
|
|
|
var unhide = parsed.unhide;
|
|
|
|
if ( hostname.charAt(0) === '~' ) {
|
|
|
|
hostname = hostname.slice(1);
|
|
|
|
unhide ^= 1;
|
2014-06-23 16:42:43 -06:00
|
|
|
}
|
2014-08-12 10:19:54 -06:00
|
|
|
var entries = unhide === 0 ?
|
|
|
|
this.hostnameHide :
|
|
|
|
this.hostnameDonthide;
|
|
|
|
var entry = entries[hostname];
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
entry = entries[hostname] = {};
|
|
|
|
entry[parsed.suffix] = true;
|
|
|
|
this.acceptedCount += 1;
|
|
|
|
} else if ( entry[parsed.suffix] === undefined ) {
|
|
|
|
entry[parsed.suffix] = true;
|
|
|
|
this.acceptedCount += 1;
|
|
|
|
} else {
|
|
|
|
this.duplicateCount += 1;
|
|
|
|
}
|
|
|
|
};
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-14 11:59:37 -06:00
|
|
|
FilterContainer.prototype.addEntitySelector = function(hostname, parsed) {
|
|
|
|
var entries = parsed.unhide === 0 ?
|
|
|
|
this.entityHide :
|
|
|
|
this.entityDonthide;
|
|
|
|
var entity = hostname.slice(0, -2);
|
|
|
|
var entry = entries[entity];
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
entry = entries[entity] = {};
|
|
|
|
entry[parsed.suffix] = true;
|
|
|
|
this.acceptedCount += 1;
|
|
|
|
} else if ( entry[parsed.suffix] === undefined ) {
|
|
|
|
entry[parsed.suffix] = true;
|
|
|
|
this.acceptedCount += 1;
|
|
|
|
} else {
|
|
|
|
this.duplicateCount += 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
FilterContainer.prototype.freezeLowGenerics = function(what, type) {
|
|
|
|
var selectors = this[what];
|
|
|
|
var matches, selectorPrefix, f, hash, bucket;
|
|
|
|
for ( var selector in selectors ) {
|
|
|
|
if ( selectors.hasOwnProperty(selector) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
matches = this.rePlainSelector.exec(selector);
|
|
|
|
if ( !matches ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
selectorPrefix = matches[1];
|
|
|
|
f = selectorPrefix === selector ?
|
|
|
|
new FilterPlain(selector) :
|
|
|
|
new FilterPlainMore(selector);
|
|
|
|
hash = makeHash(type, selectorPrefix, this.genericHashMask);
|
|
|
|
bucket = this.lowGenericFilters[hash];
|
|
|
|
if ( bucket === undefined ) {
|
|
|
|
this.lowGenericFilters[hash] = f;
|
|
|
|
} else if ( bucket instanceof FilterBucket ) {
|
|
|
|
bucket.add(f);
|
|
|
|
} else {
|
|
|
|
this.lowGenericFilters[hash] = new FilterBucket(bucket, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this[what] = {};
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
FilterContainer.prototype.rePlainSelector = /^([#.][\w-]+)/;
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-06 09:34:59 -06:00
|
|
|
FilterContainer.prototype.freezeHostnameSpecifics = function(what, type) {
|
|
|
|
var µburi = µb.URI;
|
|
|
|
var entries = this[what];
|
|
|
|
var filters = this.hostnameFilters;
|
2014-08-27 11:50:18 -06:00
|
|
|
var f, domain, hash, bucket;
|
2014-08-06 09:34:59 -06:00
|
|
|
for ( var hostname in entries ) {
|
|
|
|
if ( entries.hasOwnProperty(hostname) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-13 18:03:55 -06:00
|
|
|
f = new FilterHostname(Object.keys(entries[hostname]).join(',\n'), hostname);
|
2014-08-27 11:50:18 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/188
|
|
|
|
// If not a real domain as per PSL, assign a synthetic one
|
|
|
|
domain = µburi.domainFromHostname(hostname);
|
|
|
|
if ( domain === '' ) {
|
|
|
|
hash = type === 0 ? this.type0NoDomainHash : this.type1NoDomainHash;
|
|
|
|
} else {
|
|
|
|
hash = makeHash(type, domain, this.domainHashMask);
|
|
|
|
}
|
2014-08-06 09:34:59 -06:00
|
|
|
bucket = filters[hash];
|
|
|
|
if ( bucket === undefined ) {
|
|
|
|
filters[hash] = f;
|
|
|
|
} else if ( bucket instanceof FilterBucket ) {
|
|
|
|
bucket.add(f);
|
|
|
|
} else {
|
|
|
|
filters[hash] = new FilterBucket(bucket, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this[what] = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-14 11:59:37 -06:00
|
|
|
FilterContainer.prototype.freezeEntitySpecifics = function(what, type) {
|
|
|
|
var entries = this[what];
|
|
|
|
var filters = this.entityFilters;
|
|
|
|
var f, hash, bucket;
|
|
|
|
for ( var entity in entries ) {
|
|
|
|
if ( entries.hasOwnProperty(entity) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f = new FilterEntity(Object.keys(entries[entity]).join(',\n'), entity);
|
|
|
|
hash = makeHash(type, entity, this.domainHashMask);
|
|
|
|
bucket = filters[hash];
|
|
|
|
if ( bucket === undefined ) {
|
|
|
|
filters[hash] = f;
|
|
|
|
} else if ( bucket instanceof FilterBucket ) {
|
|
|
|
bucket.add(f);
|
|
|
|
} else {
|
|
|
|
filters[hash] = new FilterBucket(bucket, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this[what] = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
FilterContainer.prototype.freezeHighGenerics = function(what) {
|
|
|
|
var selectors = this['highGeneric' + what];
|
2014-07-04 14:47:34 -06:00
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
// ["title"] and ["alt"] will go in high-low generic bin.
|
|
|
|
// [href^="..."] wil go in high-mdium generic bin.
|
|
|
|
// The rest will be put in the high-high generic bin.
|
|
|
|
var highLowGeneric = {};
|
|
|
|
var highLowGenericCount = 0;
|
|
|
|
var highMediumGeneric = {};
|
|
|
|
var highMediumGenericCount = 0;
|
|
|
|
var highHighGeneric = [];
|
|
|
|
var reHighLow = /^[a-z]*(\[(?:alt|title)="[^"]+"\])$/;
|
|
|
|
var reHighMedium = /^\[href\^="https?:\/\/([^"]{8})[^"]*"\]$/;
|
|
|
|
var matches, hash;
|
|
|
|
for ( var selector in selectors ) {
|
|
|
|
if ( selectors.hasOwnProperty(selector) === false ) {
|
2014-07-04 14:47:34 -06:00
|
|
|
continue;
|
|
|
|
}
|
2014-08-12 10:19:54 -06:00
|
|
|
matches = reHighLow.exec(selector);
|
|
|
|
if ( matches && matches.length === 2 ) {
|
|
|
|
highLowGeneric[matches[1]] = true;
|
|
|
|
highLowGenericCount += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
matches = reHighMedium.exec(selector);
|
|
|
|
if ( matches && matches.length === 2 ) {
|
|
|
|
hash = matches[1];
|
|
|
|
if ( highMediumGeneric[hash] === undefined ) {
|
|
|
|
highMediumGeneric[hash] = matches[0];
|
|
|
|
} else {
|
|
|
|
highMediumGeneric[hash] += ',\n' + matches[0];
|
|
|
|
}
|
|
|
|
highMediumGenericCount += 1;
|
|
|
|
continue;
|
2014-06-23 16:42:43 -06:00
|
|
|
}
|
2014-08-12 10:19:54 -06:00
|
|
|
highHighGeneric.push(selector);
|
2014-06-23 16:42:43 -06:00
|
|
|
}
|
2014-08-12 10:19:54 -06:00
|
|
|
this['highLowGeneric' + what] = highLowGeneric;
|
|
|
|
this['highLowGeneric' + what + 'Count'] = highLowGenericCount;
|
|
|
|
this['highMediumGeneric' + what] = highMediumGeneric;
|
|
|
|
this['highMediumGeneric' + what + 'Count'] = highMediumGenericCount;
|
|
|
|
this['highHighGeneric' + what] = highHighGeneric.join(',\n');
|
|
|
|
this['highHighGeneric' + what + 'Count'] = highHighGeneric.length;
|
|
|
|
this['highGeneric' + what] = {};
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.freeze = function() {
|
2014-08-12 10:19:54 -06:00
|
|
|
this.freezeLowGenerics('lowGenericHide', 0);
|
|
|
|
this.freezeLowGenerics('lowGenericDonthide', 1);
|
|
|
|
this.freezeHighGenerics('Hide');
|
|
|
|
this.freezeHighGenerics('Donthide');
|
|
|
|
this.freezeHostnameSpecifics('hostnameHide', 0);
|
|
|
|
this.freezeHostnameSpecifics('hostnameDonthide', 1);
|
2014-08-14 11:59:37 -06:00
|
|
|
this.freezeEntitySpecifics('entityHide', 0);
|
|
|
|
this.freezeEntitySpecifics('entityDonthide', 1);
|
2014-08-21 08:56:36 -06:00
|
|
|
this.parser.reset();
|
2014-07-20 13:00:26 -06:00
|
|
|
this.frozen = true;
|
2014-07-15 18:32:33 -06:00
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
//histogram('lowGenericFilters', this.lowGenericFilters);
|
2014-08-06 09:34:59 -06:00
|
|
|
//histogram('hostnameFilters', this.hostnameFilters);
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-08 15:46:58 -06:00
|
|
|
FilterContainer.prototype.toSelfie = function() {
|
|
|
|
var selfieFromDict = function(dict) {
|
|
|
|
var selfie = [];
|
|
|
|
var bucket, ff, n, i, f;
|
|
|
|
for ( var k in dict ) {
|
|
|
|
if ( dict.hasOwnProperty(k) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// We need to encode the key because there could be a `\n`
|
|
|
|
// character in it, which would trip the code at parse time.
|
|
|
|
selfie.push('k\t' + encode(k));
|
|
|
|
bucket = dict[k];
|
|
|
|
selfie.push(bucket.fid + '\t' + bucket.toSelfie());
|
|
|
|
if ( bucket.fid !== '[]' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ff = bucket.filters;
|
|
|
|
n = ff.length;
|
|
|
|
for ( i = 0; i < n; i++ ) {
|
|
|
|
f = ff[i];
|
|
|
|
selfie.push(f.fid + '\t' + f.toSelfie());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return selfie.join('\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
acceptedCount: this.acceptedCount,
|
|
|
|
duplicateCount: this.duplicateCount,
|
|
|
|
hostnameSpecificFilters: selfieFromDict(this.hostnameFilters),
|
|
|
|
entitySpecificFilters: selfieFromDict(this.entityFilters),
|
|
|
|
lowGenericFilters: selfieFromDict(this.lowGenericFilters),
|
|
|
|
highLowGenericHide: this.highLowGenericHide,
|
|
|
|
highLowGenericDonthide: this.highLowGenericDonthide,
|
|
|
|
highLowGenericHideCount: this.highLowGenericHideCount,
|
|
|
|
highLowGenericDonthideCount: this.highLowGenericDonthideCount,
|
|
|
|
highMediumGenericHide: this.highMediumGenericHide,
|
|
|
|
highMediumGenericDonthide: this.highMediumGenericDonthide,
|
|
|
|
highMediumGenericHideCount: this.highMediumGenericHideCount,
|
|
|
|
highMediumGenericDonthideCount: this.highMediumGenericDonthideCount,
|
|
|
|
highHighGenericHide: this.highHighGenericHide,
|
|
|
|
highHighGenericDonthide: this.highHighGenericDonthide,
|
|
|
|
highHighGenericHideCount: this.highHighGenericHideCount,
|
|
|
|
highHighGenericDonthideCount: this.highHighGenericDonthideCount
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.fromSelfie = function(selfie) {
|
|
|
|
var factories = {
|
|
|
|
'[]': FilterBucket,
|
|
|
|
'#': FilterPlain,
|
|
|
|
'#+': FilterPlainMore,
|
|
|
|
'h': FilterHostname,
|
|
|
|
'e': FilterEntity
|
|
|
|
};
|
|
|
|
|
|
|
|
var dictFromSelfie = function(selfie) {
|
|
|
|
var dict = {};
|
|
|
|
var dictKey;
|
|
|
|
var bucket = null;
|
|
|
|
var rawText = selfie;
|
|
|
|
var rawEnd = rawText.length;
|
|
|
|
var lineBeg = 0, lineEnd;
|
|
|
|
var line, pos, what, factory;
|
|
|
|
while ( lineBeg < rawEnd ) {
|
|
|
|
lineEnd = rawText.indexOf('\n', lineBeg);
|
|
|
|
if ( lineEnd < 0 ) {
|
|
|
|
lineEnd = rawEnd;
|
|
|
|
}
|
|
|
|
line = rawText.slice(lineBeg, lineEnd);
|
|
|
|
lineBeg = lineEnd + 1;
|
|
|
|
pos = line.indexOf('\t');
|
|
|
|
what = line.slice(0, pos);
|
|
|
|
if ( what === 'k' ) {
|
|
|
|
dictKey = decode(line.slice(pos + 1));
|
|
|
|
bucket = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
factory = factories[what];
|
|
|
|
if ( bucket === null ) {
|
|
|
|
bucket = dict[dictKey] = factory.fromSelfie(line.slice(pos + 1));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// When token key is reused, it can't be anything
|
|
|
|
// else than FilterBucket
|
|
|
|
bucket.add(factory.fromSelfie(line.slice(pos + 1)));
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.frozen = true;
|
|
|
|
this.acceptedCount = selfie.acceptedCount;
|
|
|
|
this.duplicateCount = selfie.duplicateCount;
|
|
|
|
this.hostnameFilters = dictFromSelfie(selfie.hostnameSpecificFilters);
|
|
|
|
this.entityFilters = dictFromSelfie(selfie.entitySpecificFilters);
|
|
|
|
this.lowGenericFilters = dictFromSelfie(selfie.lowGenericFilters);
|
|
|
|
this.highLowGenericHide = selfie.highLowGenericHide;
|
|
|
|
this.highLowGenericDonthide = selfie.highLowGenericDonthide;
|
|
|
|
this.highLowGenericHideCount = selfie.highLowGenericHideCount;
|
|
|
|
this.highLowGenericDonthideCount = selfie.highLowGenericDonthideCount;
|
|
|
|
this.highMediumGenericHide = selfie.highMediumGenericHide;
|
|
|
|
this.highMediumGenericDonthide = selfie.highMediumGenericDonthide;
|
|
|
|
this.highMediumGenericHideCount = selfie.highMediumGenericHideCount;
|
|
|
|
this.highMediumGenericDonthideCount = selfie.highMediumGenericDonthideCount;
|
|
|
|
this.highHighGenericHide = selfie.highHighGenericHide;
|
|
|
|
this.highHighGenericDonthide = selfie.highHighGenericDonthide;
|
|
|
|
this.highHighGenericHideCount = selfie.highHighGenericHideCount;
|
|
|
|
this.highHighGenericDonthideCount = selfie.highHighGenericDonthideCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-14 11:59:37 -06:00
|
|
|
FilterContainer.prototype.addToSelectorCache = function(details) {
|
|
|
|
var hostname = details.hostname;
|
2014-08-13 18:03:55 -06:00
|
|
|
if ( typeof hostname !== 'string' || hostname === '' ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-14 11:59:37 -06:00
|
|
|
var selectors = details.selectors;
|
|
|
|
if ( !selectors ) {
|
2014-08-13 18:03:55 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var entry = this.selectorCache[hostname];
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
entry = this.selectorCache[hostname] = new SelectorCacheEntry();
|
|
|
|
this.selectorCacheCount += 1;
|
|
|
|
if ( this.selectorCacheCount > this.selectorCacheHighWaterMark ) {
|
|
|
|
this.pruneSelectorCache();
|
|
|
|
}
|
|
|
|
}
|
2014-08-14 11:59:37 -06:00
|
|
|
entry.add(selectors, details.type);
|
2014-08-13 18:03:55 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-14 11:59:37 -06:00
|
|
|
FilterContainer.prototype.retrieveFromSelectorCache = function(hostname, type, out) {
|
2014-08-13 18:03:55 -06:00
|
|
|
var entry = this.selectorCache[hostname];
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-14 11:59:37 -06:00
|
|
|
entry.retrieve(type, out);
|
2014-08-13 18:03:55 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.pruneSelectorCache = function() {
|
|
|
|
var cache = this.selectorCache;
|
|
|
|
var hostnames = Object.keys(cache).sort(function(a ,b) {
|
|
|
|
return cache[b].lastAccessTime - cache[a].lastAccessTime;
|
|
|
|
});
|
|
|
|
var toRemove = hostnames.slice(this.selectorCacheLowWaterMark);
|
|
|
|
var i = toRemove.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
delete cache[toRemove[i]];
|
|
|
|
}
|
|
|
|
this.selectorCacheCount -= toRemove.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-02 09:40:27 -06:00
|
|
|
FilterContainer.prototype.retrieveGenericSelectors = function(request) {
|
2014-06-23 16:42:43 -06:00
|
|
|
if ( µb.userSettings.parseAllABPHideFilters !== true ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( !request.selectors ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//quickProfiler.start('FilterContainer.retrieve()');
|
|
|
|
|
|
|
|
var r = {
|
|
|
|
hide: [],
|
2014-08-12 10:19:54 -06:00
|
|
|
donthide: []
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
2014-08-12 10:19:54 -06:00
|
|
|
if ( request.highGenerics ) {
|
|
|
|
r.highGenerics = {
|
|
|
|
hideLow: this.highLowGenericHide,
|
|
|
|
hideLowCount: this.highLowGenericHideCount,
|
|
|
|
hideMedium: this.highMediumGenericHide,
|
|
|
|
hideMediumCount: this.highMediumGenericHideCount,
|
|
|
|
hideHigh: this.highHighGenericHide,
|
|
|
|
hideHighCount: this.highHighGenericHideCount,
|
|
|
|
donthideLow: this.highLowGenericDonthide,
|
|
|
|
donthideLowCount: this.highLowGenericDonthideCount,
|
|
|
|
donthideMedium: this.highMediumGenericDonthide,
|
|
|
|
donthideMediumCount: this.highMediumGenericDonthideCount,
|
|
|
|
donthideHigh: this.highHighGenericDonthide,
|
|
|
|
donthideHighCount: this.highHighGenericDonthideCount
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
var hash, bucket;
|
2014-08-06 09:34:59 -06:00
|
|
|
var hashMask = this.genericHashMask;
|
2014-06-23 16:42:43 -06:00
|
|
|
var hideSelectors = r.hide;
|
|
|
|
var selectors = request.selectors;
|
|
|
|
var i = selectors.length;
|
|
|
|
var selector;
|
|
|
|
while ( i-- ) {
|
|
|
|
selector = selectors[i];
|
|
|
|
if ( !selector ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-12 10:19:54 -06:00
|
|
|
hash = makeHash(0, selector, hashMask);
|
|
|
|
if ( bucket = this.lowGenericFilters[hash] ) {
|
2014-06-23 16:42:43 -06:00
|
|
|
bucket.retrieve(selector, hideSelectors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//quickProfiler.stop();
|
|
|
|
|
2014-07-02 10:02:29 -06:00
|
|
|
//console.log(
|
|
|
|
// 'µBlock> abp-hide-filters.js: %d selectors in => %d selectors out',
|
|
|
|
// request.selectors.length,
|
|
|
|
// r.hide.length + r.donthide.length
|
|
|
|
//);
|
2014-06-23 16:42:43 -06:00
|
|
|
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-02 09:40:27 -06:00
|
|
|
FilterContainer.prototype.retrieveDomainSelectors = function(request) {
|
2014-06-23 16:42:43 -06:00
|
|
|
if ( µb.userSettings.parseAllABPHideFilters !== true ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( !request.locationURL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//quickProfiler.start('FilterContainer.retrieve()');
|
|
|
|
|
2014-07-13 00:36:38 -06:00
|
|
|
var hostname = µb.URI.hostnameFromURI(request.locationURL);
|
2014-08-27 11:50:18 -06:00
|
|
|
var domain = µb.URI.domainFromHostname(hostname) || hostname;
|
2014-08-14 11:59:37 -06:00
|
|
|
var pos = domain.indexOf('.');
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
var r = {
|
2014-08-14 11:59:37 -06:00
|
|
|
domain: domain,
|
|
|
|
entity: pos === -1 ? domain : domain.slice(0, pos - domain.length),
|
|
|
|
cosmeticHide: [],
|
|
|
|
cosmeticDonthide: [],
|
|
|
|
netHide: [],
|
|
|
|
netCollapse: µb.userSettings.collapseBlocked
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
2014-06-23 17:23:36 -06:00
|
|
|
|
2014-08-07 14:12:15 -06:00
|
|
|
var hash, bucket;
|
2014-08-27 11:50:18 -06:00
|
|
|
hash = makeHash(0, domain, this.domainHashMask);
|
2014-07-13 00:36:38 -06:00
|
|
|
if ( bucket = this.hostnameFilters[hash] ) {
|
2014-08-14 11:59:37 -06:00
|
|
|
bucket.retrieve(hostname, r.cosmeticHide);
|
|
|
|
}
|
2014-08-27 11:50:18 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/188
|
|
|
|
// Special bucket for those filters without a valid domain name as per PSL
|
|
|
|
if ( bucket = this.hostnameFilters[this.type0NoDomainHash] ) {
|
|
|
|
bucket.retrieve(hostname, r.cosmeticHide);
|
|
|
|
}
|
2014-08-14 11:59:37 -06:00
|
|
|
hash = makeHash(0, r.entity, this.domainHashMask);
|
|
|
|
if ( bucket = this.entityFilters[hash] ) {
|
|
|
|
bucket.retrieve(pos === -1 ? domain : hostname.slice(0, pos - domain.length), r.cosmeticHide);
|
2014-07-13 00:36:38 -06:00
|
|
|
}
|
2014-08-27 11:50:18 -06:00
|
|
|
hash = makeHash(1, domain, this.domainHashMask);
|
2014-07-13 00:36:38 -06:00
|
|
|
if ( bucket = this.hostnameFilters[hash] ) {
|
2014-08-14 11:59:37 -06:00
|
|
|
bucket.retrieve(hostname, r.cosmeticDonthide);
|
2014-06-23 16:42:43 -06:00
|
|
|
}
|
2014-08-27 11:50:18 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/188
|
|
|
|
// Special bucket for those filters without a valid domain name as per PSL
|
|
|
|
if ( bucket = this.hostnameFilters[this.type1NoDomainHash] ) {
|
|
|
|
bucket.retrieve(hostname, r.cosmeticDonthide);
|
|
|
|
}
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2014-08-14 11:59:37 -06:00
|
|
|
this.retrieveFromSelectorCache(hostname, 'cosmetic', r.cosmeticHide);
|
|
|
|
this.retrieveFromSelectorCache(hostname, 'net', r.netHide);
|
2014-08-13 18:03:55 -06:00
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
//quickProfiler.stop();
|
|
|
|
|
2014-07-02 10:02:29 -06:00
|
|
|
//console.log(
|
|
|
|
// 'µBlock> abp-hide-filters.js: "%s" => %d selectors out',
|
|
|
|
// request.locationURL,
|
2014-08-14 11:59:37 -06:00
|
|
|
// r.cosmeticHide.length + r.cosmeticDonthide.length
|
2014-07-02 10:02:29 -06:00
|
|
|
//);
|
2014-06-23 16:42:43 -06:00
|
|
|
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.getFilterCount = function() {
|
|
|
|
return this.acceptedCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
return new FilterContainer();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|