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 */
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.abpHideFilters = (function(){
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var µb = µBlock;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// 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 ) {
|
2014-06-23 16:42:43 -06:00
|
|
|
out.push(this.s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// TODO: evaluate the gain (if any) from avoiding the use of an array for when
|
|
|
|
// there are only two filters (or three, etc.). I suppose there is a specific
|
|
|
|
// number of filters below which using an array is more of an overhead than
|
|
|
|
// using a couple of property members.
|
|
|
|
// i.e. FilterBucket2, FilterBucket3, FilterBucketN.
|
|
|
|
|
|
|
|
var FilterBucket = function(a, b) {
|
|
|
|
this.filters = [a, 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var FilterParser = function() {
|
|
|
|
this.s = '';
|
|
|
|
this.prefix = '';
|
|
|
|
this.suffix = '';
|
|
|
|
this.anchor = 0;
|
|
|
|
this.filterType = '#';
|
|
|
|
this.hostnames = [];
|
|
|
|
this.invalid = false;
|
|
|
|
this.unsupported = false;
|
|
|
|
this.reParser = /^\s*([^#]*)(##|#@#)(.+)\s*$/;
|
|
|
|
this.rePlain = /^([#.][\w-]+)/;
|
|
|
|
this.rePlainMore = /^[#.][\w-]+[^\w-]/;
|
|
|
|
this.reElement = /^[a-z]/i;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterParser.prototype.reset = function() {
|
|
|
|
this.s = '';
|
|
|
|
this.prefix = '';
|
|
|
|
this.suffix = '';
|
|
|
|
this.anchor = '';
|
|
|
|
this.filterType = '#';
|
|
|
|
this.hostnames = [];
|
|
|
|
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.s = s;
|
|
|
|
this.prefix = matches[1];
|
|
|
|
this.anchor = matches[2];
|
|
|
|
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.
|
|
|
|
if ( this.suffix.indexOf('##') >= 0 ) {
|
|
|
|
this.invalid = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.filterType = this.anchor.charAt(1);
|
|
|
|
if ( this.prefix !== '' ) {
|
|
|
|
this.hostnames = this.prefix.split(/\s*,\s*/);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterParser.prototype.isPlainMore = function() {
|
|
|
|
return this.rePlainMore.test(this.suffix);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterParser.prototype.isElement = function() {
|
|
|
|
return this.reElement.test(this.suffix);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterParser.prototype.extractPlain = function() {
|
|
|
|
var matches = this.rePlain.exec(this.suffix);
|
|
|
|
if ( matches && matches.length === 2 ) {
|
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var FilterContainer = function() {
|
|
|
|
this.filterParser = 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() {
|
|
|
|
this.filterParser.reset();
|
2014-07-20 13:00:26 -06:00
|
|
|
this.frozen = false;
|
2014-06-23 16:42:43 -06:00
|
|
|
this.acceptedCount = 0;
|
|
|
|
this.processedCount = 0;
|
2014-08-06 09:34:59 -06:00
|
|
|
this.domainHashMask = (1 << 10) - 1;
|
|
|
|
this.genericHashMask = (1 << 15) - 1;
|
2014-07-13 00:36:38 -06:00
|
|
|
this.genericFilters = {};
|
2014-08-06 09:34:59 -06:00
|
|
|
this.hostnameHide = {};
|
|
|
|
this.hostnameDonthide = {};
|
2014-07-13 00:36:38 -06:00
|
|
|
this.hostnameFilters = {};
|
2014-08-06 09:34:59 -06:00
|
|
|
this.entityHide = {};
|
|
|
|
this.entityDonthide = {};
|
2014-07-13 00:36:38 -06:00
|
|
|
this.entityFilters = {};
|
2014-06-23 16:42:43 -06:00
|
|
|
this.hideUnfiltered = [];
|
2014-07-04 14:47:34 -06:00
|
|
|
this.hideLowGenerics = {};
|
|
|
|
this.hideHighGenerics = [];
|
2014-06-23 16:42:43 -06:00
|
|
|
this.donthideUnfiltered = [];
|
2014-07-04 14:47:34 -06:00
|
|
|
this.donthideLowGenerics = {};
|
|
|
|
this.donthideHighGenerics = [];
|
2014-06-23 16:42:43 -06:00
|
|
|
this.rejected = [];
|
2014-07-15 18:32:33 -06:00
|
|
|
this.duplicates = {};
|
|
|
|
this.duplicateCount = 0;
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.add = function(s) {
|
2014-07-15 18:32:33 -06:00
|
|
|
s = s.trim();
|
2014-06-23 16:42:43 -06:00
|
|
|
var parsed = this.filterParser.parse(s);
|
|
|
|
if ( parsed.invalid ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.processedCount += 1;
|
|
|
|
|
|
|
|
//if ( s === 'mail.google.com##.nH.adC > .nH > .nH > .u5 > .azN' ) {
|
|
|
|
// debugger;
|
|
|
|
//}
|
|
|
|
|
|
|
|
// hostname-based filters: with a hostname, narrowing is good enough, no
|
|
|
|
// need to further narrow.
|
|
|
|
if ( parsed.hostnames.length ) {
|
2014-08-06 09:34:59 -06:00
|
|
|
return this.addSpecificFilter(parsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.duplicates[s] ) {
|
|
|
|
this.duplicateCount++;
|
|
|
|
return false;
|
2014-06-23 16:42:43 -06:00
|
|
|
}
|
2014-08-06 09:34:59 -06:00
|
|
|
this.duplicates[s] = true;
|
2014-06-23 16:42:43 -06:00
|
|
|
|
|
|
|
// no specific hostname, narrow using class or id.
|
|
|
|
var selectorType = parsed.suffix.charAt(0);
|
|
|
|
if ( selectorType === '#' || selectorType === '.' ) {
|
|
|
|
return this.addPlainFilter(parsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// no specific hostname, no class, no id.
|
|
|
|
if ( parsed.filterType === '#' ) {
|
|
|
|
this.hideUnfiltered.push(parsed.suffix);
|
|
|
|
} else {
|
|
|
|
this.donthideUnfiltered.push(parsed.suffix);
|
|
|
|
}
|
|
|
|
this.acceptedCount += 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
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;
|
|
|
|
var f, hash, bucket;
|
|
|
|
for ( var hostname in entries ) {
|
|
|
|
if ( entries.hasOwnProperty(hostname) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f = new FilterHostname(Object.keys(entries[hostname]).join(','), hostname);
|
|
|
|
hash = makeHash(type, µburi.domainFromHostname(hostname), 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] = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
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(','), 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-07-04 14:47:34 -06:00
|
|
|
FilterContainer.prototype.freezeGenerics = function(what) {
|
|
|
|
var selectors = this[what + 'Unfiltered'];
|
|
|
|
//console.log('%d highly generic selectors:\n', selectors.length, selectors.sort().join('\n'));
|
|
|
|
|
|
|
|
// ["title"] and ["alt"] will be sorted out manually, these are the most
|
|
|
|
// common generic selectors, aka "low generics". The rest will be put in
|
|
|
|
// the high genericity bin.
|
|
|
|
var lowGenerics = {};
|
|
|
|
var lowGenericCount = 0;
|
|
|
|
var re = /^(([a-z]*)\[(alt|title)="([^"]+)"\])$/;
|
|
|
|
var i = selectors.length;
|
|
|
|
var selector, matches;
|
|
|
|
while ( i-- ) {
|
|
|
|
selector = selectors[i];
|
|
|
|
matches = re.exec(selector);
|
|
|
|
if ( !matches ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lowGenerics[matches[1]] = true;
|
|
|
|
lowGenericCount++;
|
|
|
|
selectors.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
2014-07-02 21:33:26 -06:00
|
|
|
// Chunksize is a compromise between number of selectors per chunk (the
|
|
|
|
// number of selectors querySelector() will have to deal with), and the
|
|
|
|
// number of chunks (the number of times querySelector() will have to
|
|
|
|
// be called.)
|
|
|
|
// Benchmarking shows this is a hot spot performance-wise for "heavy"
|
|
|
|
// sites (like say, Sports Illustrated, good test case). Not clear what
|
|
|
|
// better can be done at this point, I doubt javascript-side code can beat
|
|
|
|
// querySelector().
|
|
|
|
var chunkSize = Math.max(selectors.length >>> 3, 8);
|
2014-06-23 16:42:43 -06:00
|
|
|
var chunkified = [], chunk;
|
|
|
|
for (;;) {
|
2014-07-02 21:33:26 -06:00
|
|
|
chunk = selectors.splice(0, chunkSize);
|
2014-06-23 16:42:43 -06:00
|
|
|
if ( chunk.length === 0 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
chunkified.push(chunk.join(','));
|
|
|
|
}
|
2014-07-04 14:47:34 -06:00
|
|
|
|
|
|
|
this[what + 'LowGenerics'] = lowGenerics;
|
|
|
|
this[what + 'LowGenericCount'] = lowGenericCount;
|
|
|
|
this[what + 'HighGenerics'] = chunkified;
|
|
|
|
this[what + 'Unfiltered'] = [];
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.freeze = function() {
|
2014-08-06 09:34:59 -06:00
|
|
|
this.freezeHostnameSpecifics('hostnameHide', '#');
|
|
|
|
this.freezeHostnameSpecifics('hostnameDonthide', '@');
|
|
|
|
this.freezeEntitySpecifics('entityHide', '#');
|
|
|
|
this.freezeEntitySpecifics('entityDonthide', '@');
|
2014-07-04 14:47:34 -06:00
|
|
|
this.freezeGenerics('hide');
|
|
|
|
this.freezeGenerics('donthide');
|
2014-06-23 16:42:43 -06:00
|
|
|
|
|
|
|
this.filterParser.reset();
|
|
|
|
|
2014-07-16 10:42:29 -06:00
|
|
|
// console.debug('Number of duplicate cosmetic filters skipped:', this.duplicateCount);
|
2014-07-15 18:32:33 -06:00
|
|
|
this.duplicates = {};
|
2014-07-20 13:00:26 -06:00
|
|
|
this.frozen = true;
|
2014-07-15 18:32:33 -06:00
|
|
|
|
2014-08-06 09:34:59 -06:00
|
|
|
//histogram('genericFilters', this.genericFilters);
|
|
|
|
//histogram('hostnameFilters', this.hostnameFilters);
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-06 09:34:59 -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-06 09:34:59 -06:00
|
|
|
var makeHash = function(type, token, mask) {
|
2014-06-23 16:42:43 -06:00
|
|
|
// 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.
|
2014-08-06 09:34:59 -06:00
|
|
|
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 ( type === '@' ) {
|
|
|
|
hval |= 0x20000;
|
|
|
|
}
|
|
|
|
return String.fromCharCode(hval >>> 9, hval & 0x1FF);
|
|
|
|
};
|
2014-06-23 16:42:43 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.addPlainFilter = function(parsed) {
|
|
|
|
// Verify whether the plain selector is followed by extra selector stuff
|
|
|
|
if ( parsed.isPlainMore() ) {
|
|
|
|
return this.addPlainMoreFilter(parsed);
|
|
|
|
}
|
|
|
|
var f = new FilterPlain(parsed.suffix);
|
2014-08-06 09:34:59 -06:00
|
|
|
var hash = makeHash(parsed.filterType, parsed.suffix, this.genericHashMask);
|
2014-07-13 00:36:38 -06:00
|
|
|
this.addFilterEntry(this.genericFilters, hash, f);
|
2014-06-23 16:42:43 -06:00
|
|
|
this.acceptedCount += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.addPlainMoreFilter = function(parsed) {
|
|
|
|
var selectorSuffix = parsed.extractPlain();
|
|
|
|
if ( selectorSuffix === '' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var f = new FilterPlainMore(parsed.suffix);
|
2014-08-06 09:34:59 -06:00
|
|
|
var hash = makeHash(parsed.filterType, selectorSuffix, this.genericHashMask);
|
2014-07-13 00:36:38 -06:00
|
|
|
this.addFilterEntry(this.genericFilters, hash, f);
|
2014-06-23 16:42:43 -06:00
|
|
|
this.acceptedCount += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-13 00:36:38 -06:00
|
|
|
FilterContainer.prototype.addHostnameFilter = function(hostname, parsed) {
|
2014-08-06 09:34:59 -06:00
|
|
|
var entries = parsed.filterType === '#' ?
|
|
|
|
this.hostnameHide :
|
|
|
|
this.hostnameDonthide;
|
|
|
|
var entry = entries[hostname];
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
entry = entries[hostname] = {};
|
|
|
|
}
|
|
|
|
entry[parsed.suffix] = true;
|
2014-07-13 00:36:38 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.addEntityFilter = function(hostname, parsed) {
|
2014-08-06 09:34:59 -06:00
|
|
|
var entries = parsed.filterType === '#' ?
|
|
|
|
this.entityHide :
|
|
|
|
this.entityDonthide;
|
2014-07-13 00:36:38 -06:00
|
|
|
var entity = hostname.slice(0, -2);
|
2014-08-06 09:34:59 -06:00
|
|
|
var entry = entries[entity];
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
entry = entries[entity] = {};
|
2014-07-13 00:36:38 -06:00
|
|
|
}
|
2014-08-06 09:34:59 -06:00
|
|
|
entry[parsed.suffix] = true;
|
2014-07-13 00:36:38 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-06 09:34:59 -06:00
|
|
|
FilterContainer.prototype.addSpecificFilter = function(parsed) {
|
2014-06-23 16:42:43 -06:00
|
|
|
var hostnames = parsed.hostnames;
|
|
|
|
var i = hostnames.length, hostname;
|
|
|
|
while ( i-- ) {
|
|
|
|
hostname = hostnames[i];
|
|
|
|
if ( !hostname ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-13 00:36:38 -06:00
|
|
|
// rhill 2014-07-13: new filter class: entity.
|
|
|
|
if ( hostname.slice(-2) === '.*' ) {
|
|
|
|
this.addEntityFilter(hostname, parsed);
|
|
|
|
} else {
|
|
|
|
this.addHostnameFilter(hostname, parsed);
|
|
|
|
}
|
2014-06-23 16:42:43 -06:00
|
|
|
}
|
|
|
|
this.acceptedCount += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-13 00:36:38 -06:00
|
|
|
FilterContainer.prototype.addFilterEntry = function(filterDict, hash, f) {
|
|
|
|
var bucket = filterDict[hash];
|
2014-06-23 16:42:43 -06:00
|
|
|
if ( bucket === undefined ) {
|
2014-07-13 00:36:38 -06:00
|
|
|
filterDict[hash] = f;
|
2014-06-23 16:42:43 -06:00
|
|
|
} else if ( bucket instanceof FilterBucket ) {
|
|
|
|
bucket.add(f);
|
|
|
|
} else {
|
2014-07-13 00:36:38 -06:00
|
|
|
filterDict[hash] = new FilterBucket(bucket, f);
|
2014-06-23 16:42:43 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
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: [],
|
|
|
|
donthide: [],
|
2014-07-04 14:47:34 -06:00
|
|
|
hideLowGenerics: this.hideLowGenerics,
|
|
|
|
hideLowGenericCount: this.hideLowGenericCount,
|
|
|
|
hideHighGenerics: this.hideHighGenerics,
|
|
|
|
donthideLowGenerics: this.donthideLowGenerics,
|
|
|
|
donthideLowGenericCount: this.donthideLowGenericCount,
|
|
|
|
donthideHighGenerics: this.donthideHighGenerics
|
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-06 09:34:59 -06:00
|
|
|
hash = makeHash('#', selector, hashMask);
|
2014-07-13 00:36:38 -06:00
|
|
|
if ( bucket = this.genericFilters[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);
|
|
|
|
var domain = µb.URI.domainFromHostname(hostname);
|
|
|
|
var pos = domain.indexOf('.');
|
2014-06-23 16:42:43 -06:00
|
|
|
var r = {
|
2014-07-13 00:36:38 -06:00
|
|
|
domain: domain,
|
|
|
|
entity: pos === -1 ? domain : domain.slice(0, pos - domain.length),
|
2014-06-23 16:42:43 -06:00
|
|
|
hide: [],
|
|
|
|
donthide: []
|
|
|
|
};
|
2014-06-23 17:23:36 -06:00
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
var bucket;
|
2014-08-06 09:34:59 -06:00
|
|
|
var hash = makeHash('#', r.domain, this.domainHashMask);
|
2014-07-13 00:36:38 -06:00
|
|
|
if ( bucket = this.hostnameFilters[hash] ) {
|
|
|
|
bucket.retrieve(hostname, r.hide);
|
|
|
|
}
|
2014-08-06 09:34:59 -06:00
|
|
|
hash = makeHash('#', r.entity, this.domainHashMask);
|
2014-07-13 00:36:38 -06:00
|
|
|
if ( bucket = this.entityFilters[hash] ) {
|
|
|
|
bucket.retrieve(pos === -1 ? domain : hostname.slice(0, pos - domain.length), r.hide);
|
2014-06-23 16:42:43 -06:00
|
|
|
}
|
2014-08-06 09:34:59 -06:00
|
|
|
hash = makeHash('@', r.domain, this.domainHashMask);
|
2014-07-13 00:36:38 -06:00
|
|
|
if ( bucket = this.hostnameFilters[hash] ) {
|
|
|
|
bucket.retrieve(hostname, r.donthide);
|
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,
|
|
|
|
// r.hide.length + r.donthide.length
|
|
|
|
//);
|
2014-06-23 16:42:43 -06:00
|
|
|
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.getFilterCount = function() {
|
|
|
|
return this.acceptedCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
return new FilterContainer();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|