2014-06-24 00:42:43 +02:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2017-03-13 18:03:51 +01:00
|
|
|
|
Copyright (C) 2014-2017 Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
|
|
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 */
|
2016-06-28 01:09:04 +02:00
|
|
|
|
/* global punycode */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
|
µBlock.cosmeticFilteringEngine = (function(){
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
var µb = µBlock;
|
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
var isValidCSSSelector = (function() {
|
|
|
|
|
var div = document.createElement('div'),
|
|
|
|
|
matchesFn;
|
|
|
|
|
// Keep in mind:
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/693
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1955
|
|
|
|
|
if ( div.matches instanceof Function ) {
|
|
|
|
|
matchesFn = div.matches.bind(div);
|
|
|
|
|
} else if ( div.mozMatchesSelector instanceof Function ) {
|
|
|
|
|
matchesFn = div.mozMatchesSelector.bind(div);
|
|
|
|
|
} else if ( div.webkitMatchesSelector instanceof Function ) {
|
|
|
|
|
matchesFn = div.webkitMatchesSelector.bind(div);
|
|
|
|
|
} else if ( div.msMatchesSelector instanceof Function ) {
|
|
|
|
|
matchesFn = div.msMatchesSelector.bind(div);
|
|
|
|
|
} else {
|
|
|
|
|
matchesFn = div.querySelector.bind(div);
|
|
|
|
|
}
|
2017-10-09 05:47:23 +02:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/3111
|
|
|
|
|
// Workaround until https://bugzilla.mozilla.org/show_bug.cgi?id=1406817
|
|
|
|
|
// is fixed.
|
|
|
|
|
try {
|
|
|
|
|
matchesFn(':scope');
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
matchesFn = div.querySelector.bind(div);
|
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
return function(s) {
|
|
|
|
|
try {
|
|
|
|
|
matchesFn(s + ', ' + s + ':not(#foo)');
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
var reIsRegexLiteral = /^\/.+\/$/;
|
|
|
|
|
|
2015-10-26 16:23:56 +01:00
|
|
|
|
var isBadRegex = function(s) {
|
|
|
|
|
try {
|
|
|
|
|
void new RegExp(s);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
isBadRegex.message = ex.toString();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-12 19:29:30 +02:00
|
|
|
|
var cosmeticSurveyingMissCountMax = parseInt(vAPI.localStorage.getItem('cosmeticSurveyingMissCountMax'), 10) || 15;
|
2016-07-10 01:21:46 +02:00
|
|
|
|
|
2014-06-24 00:42:43 +02: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);
|
|
|
|
|
};
|
|
|
|
|
*/
|
2017-05-25 23:46:59 +02:00
|
|
|
|
/*******************************************************************************
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
Each filter class will register itself in the map.
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
IMPORTANT: any change which modifies the mapping will have to be
|
|
|
|
|
reflected with µBlock.systemSettings.compiledMagic.
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
**/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
var filterClasses = [];
|
2014-09-08 23:46:58 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
var registerFilterClass = function(ctor) {
|
|
|
|
|
filterClasses[ctor.prototype.fid] = ctor;
|
2014-09-08 23:46:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
var filterFromCompiledData = function(args) {
|
|
|
|
|
return filterClasses[args[0]].load(args);
|
2014-09-08 23:46:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-06-24 00:42:43 +02: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;
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterHostname.prototype.fid = 8;
|
|
|
|
|
|
2014-07-13 08:36:38 +02:00
|
|
|
|
FilterHostname.prototype.retrieve = function(hostname, out) {
|
2015-12-15 16:40:40 +01:00
|
|
|
|
if ( hostname.endsWith(this.hostname) ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
out.add(this.s);
|
2014-07-13 08:36:38 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterHostname.prototype.compile = function() {
|
|
|
|
|
return [ this.fid, this.s, this.hostname ];
|
2014-09-08 23:46:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterHostname.load = function(data) {
|
|
|
|
|
return new FilterHostname(data[1], data[2]);
|
2014-09-08 23:46:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
registerFilterClass(FilterHostname);
|
|
|
|
|
|
2014-08-14 19:59:37 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
|
var FilterBucket = function(a, b) {
|
|
|
|
|
this.f = null;
|
|
|
|
|
this.filters = [];
|
|
|
|
|
if ( a !== undefined ) {
|
|
|
|
|
this.filters[0] = a;
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.filters[1] = b;
|
2016-06-28 01:09:04 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterBucket.prototype.fid = 10;
|
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterBucket.prototype.compile = function() {
|
|
|
|
|
var out = [],
|
|
|
|
|
filters = this.filters;
|
|
|
|
|
for ( var i = 0, n = filters.length; i < n; i++ ) {
|
|
|
|
|
out[i] = filters[i].compile();
|
|
|
|
|
}
|
|
|
|
|
return [ this.fid, out ];
|
2016-06-28 01:09:04 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterBucket.load = function(data) {
|
|
|
|
|
var bucket = new FilterBucket(),
|
|
|
|
|
entries = data[1];
|
|
|
|
|
for ( var i = 0, n = entries.length; i < n; i++ ) {
|
|
|
|
|
bucket.filters[i] = filterFromCompiledData(entries[i]);
|
|
|
|
|
}
|
|
|
|
|
return bucket;
|
2016-06-28 01:09:04 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
registerFilterClass(FilterBucket);
|
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
var FilterParser = function() {
|
2016-12-30 16:32:17 +01:00
|
|
|
|
this.prefix = this.suffix = '';
|
2014-08-12 18:19:54 +02:00
|
|
|
|
this.unhide = 0;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
this.hostnames = [];
|
|
|
|
|
this.invalid = false;
|
2015-03-16 19:58:35 +01:00
|
|
|
|
this.cosmetic = true;
|
2017-09-28 18:53:05 +02:00
|
|
|
|
this.reNeedHostname = /^(?:script:contains|script:inject|.+?:-abp-contains|.+?:-abp-has|.+?:contains|.+?:has|.+?:has-text|.+?:if|.+?:if-not|.+?:matches-css(?:-before|-after)?|.*?:xpath)\(.+\)$/;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
FilterParser.prototype.reset = function() {
|
2016-06-28 01:09:04 +02:00
|
|
|
|
this.raw = '';
|
2016-12-30 16:32:17 +01:00
|
|
|
|
this.prefix = this.suffix = '';
|
2014-08-12 18:19:54 +02:00
|
|
|
|
this.unhide = 0;
|
|
|
|
|
this.hostnames.length = 0;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
this.invalid = false;
|
2015-03-16 19:58:35 +01:00
|
|
|
|
this.cosmetic = true;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2015-10-26 16:23:56 +01:00
|
|
|
|
FilterParser.prototype.parse = function(raw) {
|
2014-06-24 00:42:43 +02:00
|
|
|
|
// important!
|
|
|
|
|
this.reset();
|
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
|
this.raw = raw;
|
|
|
|
|
|
2015-11-30 20:47:56 +01:00
|
|
|
|
// Find the bounds of the anchor.
|
|
|
|
|
var lpos = raw.indexOf('#');
|
|
|
|
|
if ( lpos === -1 ) {
|
|
|
|
|
this.cosmetic = false;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
var rpos = raw.indexOf('#', lpos + 1);
|
|
|
|
|
if ( rpos === -1 ) {
|
|
|
|
|
this.cosmetic = false;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Coarse-check that the anchor is valid.
|
|
|
|
|
// `##`: l = 1
|
2017-09-28 18:53:05 +02:00
|
|
|
|
// `#@#`, `#$#`, `#%#`, `#?#`: l = 2
|
|
|
|
|
// `#@$#`, `#@%#`, `#@?#`: l = 3
|
2015-11-30 20:47:56 +01:00
|
|
|
|
if ( (rpos - lpos) > 3 ) {
|
|
|
|
|
this.cosmetic = false;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find out type of cosmetic filter.
|
|
|
|
|
// Exception filter?
|
|
|
|
|
if ( raw.charCodeAt(lpos + 1) === 0x40 /* '@' */ ) {
|
|
|
|
|
this.unhide = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/952
|
|
|
|
|
// Find out whether we are dealing with an Adguard-specific cosmetic
|
2016-06-29 23:07:33 +02:00
|
|
|
|
// filter, and if so, translate it if supported, or discard it if not
|
|
|
|
|
// supported.
|
2015-11-30 20:47:56 +01:00
|
|
|
|
var cCode = raw.charCodeAt(rpos - 1);
|
|
|
|
|
if ( cCode !== 0x23 /* '#' */ && cCode !== 0x40 /* '@' */ ) {
|
2017-09-28 18:53:05 +02:00
|
|
|
|
// We have an Adguard/ABP cosmetic filter if and only if the character
|
|
|
|
|
// is `$`, `%` or `?`, otherwise it's not a cosmetic filter.
|
|
|
|
|
if (
|
|
|
|
|
cCode !== 0x24 /* '$' */ &&
|
|
|
|
|
cCode !== 0x25 /* '%' */ &&
|
|
|
|
|
cCode !== 0x3F /* '?' */
|
|
|
|
|
) {
|
2015-11-30 20:47:56 +01:00
|
|
|
|
this.cosmetic = false;
|
2016-06-29 23:07:33 +02:00
|
|
|
|
return this;
|
2015-11-30 20:47:56 +01:00
|
|
|
|
}
|
2017-09-28 18:53:05 +02:00
|
|
|
|
// Adguard's scriptlet injection: not supported.
|
|
|
|
|
if ( cCode === 0x25 /* '%' */ ) {
|
2016-06-29 23:07:33 +02:00
|
|
|
|
this.invalid = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2017-09-28 18:53:05 +02:00
|
|
|
|
// Adguard's style injection: supported, but translate to uBO's format.
|
|
|
|
|
if ( cCode === 0x24 /* '$' */ ) {
|
|
|
|
|
raw = this.translateAdguardCSSInjectionFilter(raw);
|
|
|
|
|
if ( raw === '' ) {
|
|
|
|
|
this.invalid = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2016-06-29 23:07:33 +02:00
|
|
|
|
}
|
|
|
|
|
rpos = raw.indexOf('#', lpos + 1);
|
2015-11-30 20:47:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract the hostname(s).
|
|
|
|
|
if ( lpos !== 0 ) {
|
|
|
|
|
this.prefix = raw.slice(0, lpos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract the selector.
|
2016-09-01 21:54:01 +02:00
|
|
|
|
this.suffix = raw.slice(rpos + 1).trim();
|
2015-11-30 20:47:56 +01:00
|
|
|
|
if ( this.suffix.length === 0 ) {
|
2015-03-16 19:58:35 +01:00
|
|
|
|
this.cosmetic = false;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2015-10-04 19:27:05 +02:00
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
|
// 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 18:19:54 +02:00
|
|
|
|
if ( this.suffix.indexOf('##') !== -1 ) {
|
2015-03-16 19:58:35 +01:00
|
|
|
|
this.cosmetic = false;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-12 18:19:54 +02: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.
|
2015-12-15 16:40:40 +01:00
|
|
|
|
if ( this.suffix.startsWith('[href^="', 1) ) {
|
2014-08-12 18:19:54 +02:00
|
|
|
|
this.suffix = this.suffix.slice(1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-04 19:27:05 +02:00
|
|
|
|
if ( this.prefix !== '' ) {
|
|
|
|
|
this.hostnames = this.prefix.split(/\s*,\s*/);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// For some selectors, it is mandatory to have a hostname or entity:
|
|
|
|
|
// ##script:contains(...)
|
|
|
|
|
// ##script:inject(...)
|
2017-09-28 18:53:05 +02:00
|
|
|
|
// ##.foo:-abp-contains(...)
|
|
|
|
|
// ##.foo:-abp-has(...)
|
|
|
|
|
// ##.foo:contains(...)
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// ##.foo:has(...)
|
2016-12-25 22:56:39 +01:00
|
|
|
|
// ##.foo:has-text(...)
|
|
|
|
|
// ##.foo:if(...)
|
|
|
|
|
// ##.foo:if-not(...)
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// ##.foo:matches-css(...)
|
2016-12-25 22:56:39 +01:00
|
|
|
|
// ##.foo:matches-css-after(...)
|
|
|
|
|
// ##.foo:matches-css-before(...)
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// ##:xpath(...)
|
2016-06-28 01:09:04 +02:00
|
|
|
|
if (
|
|
|
|
|
this.hostnames.length === 0 &&
|
|
|
|
|
this.unhide === 0 &&
|
|
|
|
|
this.reNeedHostname.test(this.suffix)
|
|
|
|
|
) {
|
|
|
|
|
this.invalid = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-06-29 23:07:33 +02:00
|
|
|
|
// Reference: https://adguard.com/en/filterrules.html#cssInjection
|
|
|
|
|
|
|
|
|
|
FilterParser.prototype.translateAdguardCSSInjectionFilter = function(raw) {
|
|
|
|
|
var matches = /^([^#]*)#(@?)\$#([^{]+)\{([^}]+)\}$/.exec(raw);
|
|
|
|
|
if ( matches === null ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2016-06-30 00:12:21 +02:00
|
|
|
|
// For now we do not allow generic CSS injections (prolly never).
|
|
|
|
|
if ( matches[1] === '' && matches[2] !== '@' ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2016-06-29 23:07:33 +02:00
|
|
|
|
return matches[1] +
|
|
|
|
|
'#' + matches[2] + '#' +
|
|
|
|
|
matches[3].trim() +
|
|
|
|
|
':style(' + matches[4].trim() + ')';
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-08-14 02:03:55 +02:00
|
|
|
|
var SelectorCacheEntry = function() {
|
2014-12-26 21:26:44 +01:00
|
|
|
|
this.reset();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
SelectorCacheEntry.junkyard = [];
|
|
|
|
|
|
|
|
|
|
SelectorCacheEntry.factory = function() {
|
|
|
|
|
var entry = SelectorCacheEntry.junkyard.pop();
|
|
|
|
|
if ( entry ) {
|
|
|
|
|
return entry.reset();
|
|
|
|
|
}
|
|
|
|
|
return new SelectorCacheEntry();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-10-22 14:59:29 +02:00
|
|
|
|
var netSelectorCacheLowWaterMark = 20;
|
2016-08-08 15:53:35 +02:00
|
|
|
|
var netSelectorCacheHighWaterMark = 30;
|
2014-12-26 21:26:44 +01:00
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
SelectorCacheEntry.prototype.reset = function() {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.cosmetic = new Set();
|
2016-07-10 01:21:46 +02:00
|
|
|
|
this.cosmeticSurveyingMissCount = 0;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.net = new Map();
|
2014-08-14 02:03:55 +02:00
|
|
|
|
this.lastAccessTime = Date.now();
|
2014-12-26 21:26:44 +01:00
|
|
|
|
return this;
|
2014-08-14 02:03:55 +02:00
|
|
|
|
};
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
SelectorCacheEntry.prototype.dispose = function() {
|
|
|
|
|
this.cosmetic = this.net = null;
|
|
|
|
|
if ( SelectorCacheEntry.junkyard.length < 25 ) {
|
|
|
|
|
SelectorCacheEntry.junkyard.push(this);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-08-14 19:59:37 +02:00
|
|
|
|
|
2016-10-06 16:49:46 +02:00
|
|
|
|
SelectorCacheEntry.prototype.addCosmetic = function(details) {
|
|
|
|
|
var selectors = details.selectors,
|
|
|
|
|
i = selectors.length || 0;
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2011
|
|
|
|
|
// Avoiding seemingly pointless surveys only if they appear costly.
|
|
|
|
|
if ( details.first && i === 0 ) {
|
|
|
|
|
if ( (details.cost || 0) >= 80 ) {
|
|
|
|
|
this.cosmeticSurveyingMissCount += 1;
|
|
|
|
|
}
|
2016-07-10 01:21:46 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.cosmeticSurveyingMissCount = 0;
|
2014-08-14 19:59:37 +02:00
|
|
|
|
while ( i-- ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.cosmetic.add(selectors[i]);
|
2014-08-14 19:59:37 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-10-22 14:59:29 +02:00
|
|
|
|
SelectorCacheEntry.prototype.addNet = function(selectors) {
|
2014-08-15 16:34:13 +02:00
|
|
|
|
if ( typeof selectors === 'string' ) {
|
|
|
|
|
this.addNetOne(selectors, Date.now());
|
|
|
|
|
} else {
|
|
|
|
|
this.addNetMany(selectors, Date.now());
|
2014-08-14 19:59:37 +02: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.
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( this.net.size < netSelectorCacheHighWaterMark ) { return; }
|
2014-08-15 16:34:13 +02:00
|
|
|
|
var dict = this.net;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
var keys = µb.arrayFrom(dict.keys()).sort(function(a, b) {
|
|
|
|
|
return dict.get(b) - dict.get(a);
|
2016-08-08 15:53:35 +02:00
|
|
|
|
}).slice(netSelectorCacheLowWaterMark);
|
2014-08-15 16:34:13 +02:00
|
|
|
|
var i = keys.length;
|
|
|
|
|
while ( i-- ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
dict.delete(keys[i]);
|
2014-08-15 16:34:13 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-08-15 16:34:13 +02:00
|
|
|
|
SelectorCacheEntry.prototype.addNetOne = function(selector, now) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.net.set(selector, now);
|
2014-08-15 16:34:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-08-15 16:34:13 +02:00
|
|
|
|
SelectorCacheEntry.prototype.addNetMany = function(selectors, now) {
|
|
|
|
|
var i = selectors.length || 0;
|
|
|
|
|
while ( i-- ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.net.set(selectors[i], now);
|
2014-08-14 19:59:37 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-10-06 16:49:46 +02:00
|
|
|
|
SelectorCacheEntry.prototype.add = function(details) {
|
2014-08-14 02:03:55 +02:00
|
|
|
|
this.lastAccessTime = Date.now();
|
2016-10-06 16:49:46 +02:00
|
|
|
|
if ( details.type === 'cosmetic' ) {
|
|
|
|
|
this.addCosmetic(details);
|
2014-08-14 19:59:37 +02:00
|
|
|
|
} else {
|
2016-10-06 16:49:46 +02:00
|
|
|
|
this.addNet(details.selectors);
|
2014-08-14 19:59:37 +02:00
|
|
|
|
}
|
2014-08-14 02:03:55 +02:00
|
|
|
|
};
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/420
|
2014-12-17 16:32:50 +01:00
|
|
|
|
SelectorCacheEntry.prototype.remove = function(type) {
|
|
|
|
|
this.lastAccessTime = Date.now();
|
2015-02-11 23:28:19 +01:00
|
|
|
|
if ( type === undefined || type === 'cosmetic' ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.cosmetic.clear();
|
2016-07-10 01:21:46 +02:00
|
|
|
|
this.cosmeticSurveyingMissCount = 0;
|
2015-02-11 23:28:19 +01:00
|
|
|
|
}
|
|
|
|
|
if ( type === undefined || type === 'net' ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.net.clear();
|
2014-12-17 16:32:50 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-10-22 14:59:29 +02:00
|
|
|
|
SelectorCacheEntry.prototype.retrieveToArray = function(iterator, out) {
|
|
|
|
|
for ( var selector of iterator ) {
|
|
|
|
|
out.push(selector);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SelectorCacheEntry.prototype.retrieveToSet = function(iterator, out) {
|
|
|
|
|
for ( var selector of iterator ) {
|
|
|
|
|
out.add(selector);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-14 19:59:37 +02:00
|
|
|
|
SelectorCacheEntry.prototype.retrieve = function(type, out) {
|
2014-08-14 02:03:55 +02:00
|
|
|
|
this.lastAccessTime = Date.now();
|
2017-10-22 14:59:29 +02:00
|
|
|
|
var iterator = type === 'cosmetic' ? this.cosmetic : this.net.keys();
|
|
|
|
|
if ( Array.isArray(out) ) {
|
|
|
|
|
this.retrieveToArray(iterator, out);
|
|
|
|
|
} else {
|
|
|
|
|
this.retrieveToSet(iterator, out);
|
2014-08-14 02:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-08-12 18:19:54 +02:00
|
|
|
|
// Two Unicode characters:
|
|
|
|
|
// T0HHHHHHH HHHHHHHHH
|
2014-10-17 21:44:19 +02:00
|
|
|
|
// | | |
|
|
|
|
|
// | | |
|
|
|
|
|
// | | |
|
2014-08-12 18:19:54 +02:00
|
|
|
|
// | | +-- bit 8-0 of FNV
|
|
|
|
|
// | |
|
|
|
|
|
// | +-- bit 15-9 of FNV
|
|
|
|
|
// |
|
|
|
|
|
// +-- filter type (0=hide 1=unhide)
|
|
|
|
|
//
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
var makeHash = function(token) {
|
2014-08-12 18:19:54 +02: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
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// The rest is custom, suited for uBlock.
|
2014-08-12 18:19:54 +02: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;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
hval &= 0x0FFF; // 12 bits
|
2015-02-24 00:31:29 +01:00
|
|
|
|
return hval.toString(36);
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-08-12 18:19:54 +02: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 19:59:37 +02:00
|
|
|
|
// Specific entity
|
2014-08-12 18:19:54 +02: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-24 00:42:43 +02:00
|
|
|
|
var FilterContainer = function() {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.noDomainHash = '-';
|
2014-08-21 16:56:36 +02:00
|
|
|
|
this.parser = new FilterParser();
|
2015-03-23 15:40:03 +01:00
|
|
|
|
this.reHasUnicode = /[^\x00-\x7F]/;
|
2016-10-30 20:19:58 +01:00
|
|
|
|
this.rePlainSelector = /^[#.][\w\\-]+/;
|
|
|
|
|
this.rePlainSelectorEscaped = /^[#.](?:\\[0-9A-Fa-f]+ |\\.|\w|-)+/;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.rePlainSelectorEx = /^[^#.\[(]+([#.][\w-]+)|([#.][\w-]+)$/;
|
2016-10-30 20:19:58 +01:00
|
|
|
|
this.reEscapeSequence = /\\([0-9A-Fa-f]+ |.)/g;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.reSimpleHighGeneric1 = /^[a-z]*\[[^[]+]$/;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.reHighMedium = /^\[href\^="https?:\/\/([^"]{8})[^"]*"\]$/;
|
|
|
|
|
this.reScriptSelector = /^script:(contains|inject)\((.+)\)$/;
|
2015-03-23 15:40:03 +01:00
|
|
|
|
this.punycode = punycode;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
|
|
|
|
this.selectorCache = new Map();
|
|
|
|
|
this.selectorCachePruneDelay = 10 * 60 * 1000; // 10 minutes
|
|
|
|
|
this.selectorCacheAgeMax = 120 * 60 * 1000; // 120 minutes
|
|
|
|
|
this.selectorCacheCountMin = 25;
|
|
|
|
|
this.netSelectorCacheCountMax = netSelectorCacheHighWaterMark;
|
|
|
|
|
this.selectorCacheTimer = null;
|
|
|
|
|
|
2017-10-23 15:01:00 +02:00
|
|
|
|
this.supportsUserStylesheets = vAPI.supportsUserStylesheets;
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// generic exception filters
|
|
|
|
|
this.genericDonthideSet = new Set();
|
|
|
|
|
|
|
|
|
|
// hostname, entity-based filters
|
|
|
|
|
this.specificFilters = new Map();
|
|
|
|
|
this.proceduralFilters = new Map();
|
|
|
|
|
|
|
|
|
|
// low generic cosmetic filters, organized by id/class then simple/complex.
|
|
|
|
|
this.lowlyGeneric = Object.create(null);
|
|
|
|
|
this.lowlyGeneric.id = {
|
|
|
|
|
canonical: 'ids',
|
|
|
|
|
prefix: '#',
|
|
|
|
|
simple: new Set(),
|
|
|
|
|
complex: new Map()
|
|
|
|
|
};
|
|
|
|
|
this.lowlyGeneric.cl = {
|
|
|
|
|
canonical: 'classes',
|
|
|
|
|
prefix: '.',
|
|
|
|
|
simple: new Set(),
|
|
|
|
|
complex: new Map()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// highly generic selectors sets
|
2017-10-29 18:58:46 +01:00
|
|
|
|
this.highlyGeneric = Object.create(null);
|
|
|
|
|
this.highlyGeneric.simple = {
|
|
|
|
|
canonical: 'highGenericHideSimple',
|
|
|
|
|
dict: new Set(),
|
|
|
|
|
str: '',
|
|
|
|
|
mru: new µb.MRUCache(16)
|
|
|
|
|
};
|
|
|
|
|
this.highlyGeneric.complex = {
|
|
|
|
|
canonical: 'highGenericHideComplex',
|
|
|
|
|
dict: new Set(),
|
|
|
|
|
str: '',
|
|
|
|
|
mru: new µb.MRUCache(16)
|
|
|
|
|
};
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
|
|
|
|
this.userScripts = new Map();
|
2017-12-21 23:05:25 +01:00
|
|
|
|
this.userScriptCache = new µb.MRUCache(32);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
|
|
|
|
// Short-lived: content is valid only during one function call. These
|
|
|
|
|
// is to prevent repeated allocation/deallocation overheads -- the
|
|
|
|
|
// constructors/destructors of javascript Set/Map is assumed to be costlier
|
|
|
|
|
// than just calling clear() on these.
|
|
|
|
|
this.setRegister0 = new Set();
|
|
|
|
|
this.setRegister1 = new Set();
|
|
|
|
|
this.setRegister2 = new Set();
|
2017-12-17 14:09:47 +01:00
|
|
|
|
this.mapRegister0 = new Map();
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
2014-07-16 02:32:33 +02:00
|
|
|
|
this.reset();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
// Reset all, thus reducing to a minimum memory footprint of the context.
|
|
|
|
|
|
|
|
|
|
FilterContainer.prototype.reset = function() {
|
2014-08-21 16:56:36 +02:00
|
|
|
|
this.parser.reset();
|
2015-02-24 00:31:29 +01:00
|
|
|
|
this.µburi = µb.URI;
|
2014-07-20 21:00:26 +02:00
|
|
|
|
this.frozen = false;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
this.acceptedCount = 0;
|
2016-03-17 18:56:21 +01:00
|
|
|
|
this.discardedCount = 0;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.duplicateBuster = new Set();
|
2014-08-12 18:19:54 +02:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.selectorCache.clear();
|
2016-03-05 02:25:35 +01:00
|
|
|
|
if ( this.selectorCacheTimer !== null ) {
|
|
|
|
|
clearTimeout(this.selectorCacheTimer);
|
|
|
|
|
this.selectorCacheTimer = null;
|
|
|
|
|
}
|
2014-08-14 02:03:55 +02:00
|
|
|
|
|
2016-08-13 22:42:58 +02:00
|
|
|
|
// generic filters
|
|
|
|
|
this.hasGenericHide = false;
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// generic exception filters
|
|
|
|
|
this.genericDonthideSet.clear();
|
2014-08-12 18:19:54 +02:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// hostname, entity-based filters
|
|
|
|
|
this.specificFilters.clear();
|
|
|
|
|
this.proceduralFilters.clear();
|
2016-06-29 04:01:15 +02:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// low generic cosmetic filters, organized by id/class then simple/complex.
|
|
|
|
|
this.lowlyGeneric.id.simple.clear();
|
|
|
|
|
this.lowlyGeneric.id.complex.clear();
|
|
|
|
|
this.lowlyGeneric.cl.simple.clear();
|
|
|
|
|
this.lowlyGeneric.cl.complex.clear();
|
2014-08-12 18:19:54 +02:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// highly generic selectors sets
|
2017-10-29 18:58:46 +01:00
|
|
|
|
this.highlyGeneric.simple.dict.clear();
|
|
|
|
|
this.highlyGeneric.simple.str = '';
|
|
|
|
|
this.highlyGeneric.simple.mru.reset();
|
|
|
|
|
this.highlyGeneric.complex.dict.clear();
|
|
|
|
|
this.highlyGeneric.complex.str = '';
|
|
|
|
|
this.highlyGeneric.complex.mru.reset();
|
2015-03-13 18:01:46 +01:00
|
|
|
|
|
2015-09-27 16:13:31 +02:00
|
|
|
|
this.scriptTagFilters = {};
|
|
|
|
|
this.scriptTagFilterCount = 0;
|
2017-12-21 23:05:25 +01:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.userScripts.clear();
|
2017-12-21 23:05:25 +01:00
|
|
|
|
this.userScriptCache.reset();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-08-13 22:42:58 +02:00
|
|
|
|
FilterContainer.prototype.freeze = function() {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.duplicateBuster = new Set();
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.hasGenericHide =
|
|
|
|
|
this.lowlyGeneric.id.simple.size !== 0 ||
|
|
|
|
|
this.lowlyGeneric.id.complex.size !== 0 ||
|
|
|
|
|
this.lowlyGeneric.cl.simple.size !== 0 ||
|
|
|
|
|
this.lowlyGeneric.cl.complex.size !== 0 ||
|
2017-10-29 18:58:46 +01:00
|
|
|
|
this.highlyGeneric.simple.dict.size !== 0 ||
|
|
|
|
|
this.highlyGeneric.complex.dict.size !== 0;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
|
|
|
|
if ( this.genericDonthideSet.size !== 0 ) {
|
|
|
|
|
for ( var selector of this.genericDonthideSet ) {
|
|
|
|
|
var type = selector.charCodeAt(0);
|
|
|
|
|
if ( type === 0x23 /* '#' */ ) {
|
|
|
|
|
this.lowlyGeneric.id.simple.delete(selector.slice(1));
|
|
|
|
|
} else if ( type === 0x2E /* '.' */ ) {
|
|
|
|
|
this.lowlyGeneric.cl.simple.delete(selector.slice(1));
|
|
|
|
|
}
|
|
|
|
|
// TODO:
|
|
|
|
|
// this.lowlyGeneric.id.complex.delete(selector);
|
|
|
|
|
// this.lowlyGeneric.cl.complex.delete(selector);
|
2017-10-29 18:58:46 +01:00
|
|
|
|
this.highlyGeneric.simple.dict.delete(selector);
|
|
|
|
|
this.highlyGeneric.complex.dict.delete(selector);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
}
|
2016-08-13 22:42:58 +02:00
|
|
|
|
}
|
2017-10-29 18:58:46 +01:00
|
|
|
|
this.highlyGeneric.simple.str = µb.arrayFrom(this.highlyGeneric.simple.dict).join(',\n');
|
|
|
|
|
this.highlyGeneric.complex.str = µb.arrayFrom(this.highlyGeneric.complex.dict).join(',\n');
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
|
|
|
|
this.parser.reset();
|
2017-05-12 16:35:11 +02:00
|
|
|
|
this.compileSelector.reset();
|
|
|
|
|
this.compileProceduralSelector.reset();
|
2016-08-13 22:42:58 +02:00
|
|
|
|
this.frozen = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1004
|
2015-03-17 13:26:35 +01:00
|
|
|
|
// Detect and report invalid CSS selectors.
|
|
|
|
|
|
2016-06-29 23:07:33 +02:00
|
|
|
|
// Discard new ABP's `-abp-properties` directive until it is
|
|
|
|
|
// implemented (if ever). Unlikely, see:
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1752
|
|
|
|
|
|
2017-05-20 21:35:19 +02:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2624
|
|
|
|
|
// Convert Adguard's `-ext-has='...'` into uBO's `:has(...)`.
|
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
FilterContainer.prototype.compileSelector = (function() {
|
2017-03-13 18:03:51 +01:00
|
|
|
|
var reAfterBeforeSelector = /^(.+?)(::?after|::?before)$/,
|
|
|
|
|
reStyleSelector = /^(.+?):style\((.+?)\)$/,
|
2016-12-25 22:56:39 +01:00
|
|
|
|
reStyleBad = /url\([^)]+\)/,
|
2017-05-20 21:35:19 +02:00
|
|
|
|
reExtendedSyntax = /\[-(?:abp|ext)-[a-z-]+=(['"])(?:.+?)(?:\1)\]/,
|
2017-09-30 16:18:41 +02:00
|
|
|
|
reExtendedSyntaxParser = /\[-(?:abp|ext)-([a-z-]+)=(['"])(.+?)\2\]/,
|
2016-12-30 16:32:17 +01:00
|
|
|
|
div = document.createElement('div');
|
|
|
|
|
|
2017-09-30 16:18:41 +02:00
|
|
|
|
var normalizedExtendedSyntaxOperators = new Map([
|
|
|
|
|
[ 'contains', ':has-text' ],
|
|
|
|
|
[ 'has', ':if' ],
|
|
|
|
|
[ 'matches-css', ':matches-css' ],
|
|
|
|
|
[ 'matches-css-after', ':matches-css-after' ],
|
|
|
|
|
[ 'matches-css-before', ':matches-css-before' ],
|
|
|
|
|
]);
|
|
|
|
|
|
2016-12-30 16:32:17 +01:00
|
|
|
|
var isValidStyleProperty = function(cssText) {
|
|
|
|
|
if ( reStyleBad.test(cssText) ) { return false; }
|
|
|
|
|
div.style.cssText = cssText;
|
|
|
|
|
if ( div.style.cssText === '' ) { return false; }
|
|
|
|
|
div.style.cssText = '';
|
|
|
|
|
return true;
|
|
|
|
|
};
|
2016-12-25 22:56:39 +01:00
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
|
var entryPoint = function(raw) {
|
2017-09-30 16:18:41 +02:00
|
|
|
|
var extendedSyntax = reExtendedSyntax.test(raw);
|
|
|
|
|
if ( isValidCSSSelector(raw) && extendedSyntax === false ) {
|
2016-12-25 22:56:39 +01:00
|
|
|
|
return raw;
|
2016-06-28 01:09:04 +02:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
|
2017-05-20 21:35:19 +02:00
|
|
|
|
// We rarely reach this point -- majority of selectors are plain
|
|
|
|
|
// CSS selectors.
|
|
|
|
|
|
2017-09-30 16:18:41 +02:00
|
|
|
|
var matches, operator;
|
2017-05-20 21:35:19 +02:00
|
|
|
|
|
2017-09-30 16:18:41 +02:00
|
|
|
|
// Supported Adguard/ABP advanced selector syntax: will translate into
|
2017-05-20 21:35:19 +02:00
|
|
|
|
// uBO's syntax before further processing.
|
2017-09-30 16:18:41 +02:00
|
|
|
|
// Mind unsupported advanced selector syntax, such as ABP's
|
|
|
|
|
// `-abp-properties`.
|
|
|
|
|
// Note: extended selector syntax has been deprecated in ABP, in favor
|
|
|
|
|
// of the procedural one (i.e. `:operator(...)`). See
|
|
|
|
|
// https://issues.adblockplus.org/ticket/5287
|
|
|
|
|
if ( extendedSyntax ) {
|
|
|
|
|
while ( (matches = reExtendedSyntaxParser.exec(raw)) !== null ) {
|
|
|
|
|
operator = normalizedExtendedSyntaxOperators.get(matches[1]);
|
|
|
|
|
if ( operator === undefined ) { return; }
|
|
|
|
|
raw = raw.slice(0, matches.index) +
|
|
|
|
|
operator + '(' + matches[3] + ')' +
|
|
|
|
|
raw.slice(matches.index + matches[0].length);
|
|
|
|
|
}
|
|
|
|
|
return this.compileSelector(raw);
|
2017-05-20 21:35:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var selector = raw,
|
|
|
|
|
pseudoclass, style;
|
2016-12-25 22:56:39 +01:00
|
|
|
|
|
|
|
|
|
// `:style` selector?
|
2017-03-13 18:03:51 +01:00
|
|
|
|
if ( (matches = reStyleSelector.exec(selector)) !== null ) {
|
|
|
|
|
selector = matches[1];
|
|
|
|
|
style = matches[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2448
|
|
|
|
|
// :after- or :before-based selector?
|
|
|
|
|
if ( (matches = reAfterBeforeSelector.exec(selector)) ) {
|
|
|
|
|
selector = matches[1];
|
|
|
|
|
pseudoclass = matches[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( style !== undefined || pseudoclass !== undefined ) {
|
|
|
|
|
if ( isValidCSSSelector(selector) === false ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( pseudoclass !== undefined ) {
|
|
|
|
|
selector += pseudoclass;
|
|
|
|
|
}
|
|
|
|
|
if ( style !== undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( isValidStyleProperty(style) === false ) { return; }
|
2017-03-13 18:03:51 +01:00
|
|
|
|
return JSON.stringify({
|
|
|
|
|
raw: raw,
|
2017-10-21 19:43:46 +02:00
|
|
|
|
style: [ selector, style ]
|
2017-03-13 18:03:51 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
2016-12-30 16:41:16 +01:00
|
|
|
|
return JSON.stringify({
|
|
|
|
|
raw: raw,
|
2017-03-13 18:03:51 +01:00
|
|
|
|
pseudoclass: true
|
2016-12-30 16:41:16 +01:00
|
|
|
|
});
|
2016-06-28 01:09:04 +02:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
|
|
|
|
|
// `script:` filter?
|
2017-12-17 16:28:12 +01:00
|
|
|
|
if ( (matches = this.reScriptSelector.exec(raw)) !== null ) {
|
2016-12-25 22:56:39 +01:00
|
|
|
|
// :inject
|
|
|
|
|
if ( matches[1] === 'inject' ) {
|
|
|
|
|
return raw;
|
|
|
|
|
}
|
|
|
|
|
// :contains
|
2017-09-28 18:53:05 +02:00
|
|
|
|
if (
|
|
|
|
|
reIsRegexLiteral.test(matches[2]) === false ||
|
|
|
|
|
isBadRegex(matches[2].slice(1, -1)) === false
|
|
|
|
|
) {
|
2016-12-25 22:56:39 +01:00
|
|
|
|
return raw;
|
|
|
|
|
}
|
2016-06-28 01:09:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
// Procedural selector?
|
|
|
|
|
var compiled;
|
|
|
|
|
if ( (compiled = this.compileProceduralSelector(raw)) ) {
|
|
|
|
|
return compiled;
|
2016-06-29 23:07:33 +02:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
|
|
|
|
|
µb.logger.writeOne('', 'error', 'Cosmetic filtering – invalid filter: ' + raw);
|
2016-06-29 23:07:33 +02:00
|
|
|
|
};
|
2017-05-12 16:35:11 +02:00
|
|
|
|
|
|
|
|
|
entryPoint.reset = function() {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return entryPoint;
|
2016-12-25 22:56:39 +01:00
|
|
|
|
})();
|
2016-06-29 23:07:33 +02:00
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
FilterContainer.prototype.compileProceduralSelector = (function() {
|
2017-09-28 18:53:05 +02:00
|
|
|
|
var reOperatorParser = /(:(?:-abp-contains|-abp-has|contains|has|has-text|if|if-not|matches-css|matches-css-after|matches-css-before|xpath))\(.+\)$/,
|
2016-12-25 22:56:39 +01:00
|
|
|
|
reFirstParentheses = /^\(*/,
|
|
|
|
|
reLastParentheses = /\)*$/,
|
2017-05-20 21:35:19 +02:00
|
|
|
|
reEscapeRegex = /[.*+?^${}()|[\]\\]/g,
|
2017-10-04 19:20:43 +02:00
|
|
|
|
reNeedScope = /^\s*[+>~]/;
|
2016-12-25 22:56:39 +01:00
|
|
|
|
|
|
|
|
|
var lastProceduralSelector = '',
|
2017-10-04 19:20:43 +02:00
|
|
|
|
lastProceduralSelectorCompiled,
|
|
|
|
|
regexToRawValue = new Map();
|
2016-12-25 22:56:39 +01:00
|
|
|
|
|
|
|
|
|
var compileCSSSelector = function(s) {
|
2017-05-20 21:35:19 +02:00
|
|
|
|
// https://github.com/AdguardTeam/ExtendedCss/issues/31#issuecomment-302391277
|
|
|
|
|
// Prepend `:scope ` if needed.
|
|
|
|
|
if ( reNeedScope.test(s) ) {
|
|
|
|
|
s = ':scope ' + s;
|
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
if ( isValidCSSSelector(s) ) {
|
|
|
|
|
return s;
|
2015-03-17 13:26:35 +01:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var compileText = function(s) {
|
2017-10-04 19:20:43 +02:00
|
|
|
|
var reText;
|
2016-12-25 22:56:39 +01:00
|
|
|
|
if ( reIsRegexLiteral.test(s) ) {
|
2017-10-04 19:20:43 +02:00
|
|
|
|
reText = s.slice(1, -1);
|
|
|
|
|
if ( isBadRegex(reText) ) { return; }
|
2016-12-25 22:56:39 +01:00
|
|
|
|
} else {
|
2017-10-04 19:20:43 +02:00
|
|
|
|
reText = s.replace(reEscapeRegex, '\\$&');
|
|
|
|
|
regexToRawValue.set(reText, s);
|
2016-12-25 22:56:39 +01:00
|
|
|
|
}
|
2017-10-04 19:20:43 +02:00
|
|
|
|
return reText;
|
2016-12-25 22:56:39 +01:00
|
|
|
|
};
|
2016-06-28 01:09:04 +02:00
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
var compileCSSDeclaration = function(s) {
|
2017-10-04 19:20:43 +02:00
|
|
|
|
var name, value, reText,
|
2016-12-25 22:56:39 +01:00
|
|
|
|
pos = s.indexOf(':');
|
|
|
|
|
if ( pos === -1 ) { return; }
|
|
|
|
|
name = s.slice(0, pos).trim();
|
|
|
|
|
value = s.slice(pos + 1).trim();
|
|
|
|
|
if ( reIsRegexLiteral.test(value) ) {
|
2017-10-04 19:20:43 +02:00
|
|
|
|
reText = value.slice(1, -1);
|
|
|
|
|
if ( isBadRegex(reText) ) { return; }
|
2016-12-25 22:56:39 +01:00
|
|
|
|
} else {
|
2017-10-04 19:20:43 +02:00
|
|
|
|
reText = '^' + value.replace(reEscapeRegex, '\\$&') + '$';
|
|
|
|
|
regexToRawValue.set(reText, value);
|
2016-06-28 01:09:04 +02:00
|
|
|
|
}
|
2017-10-04 19:20:43 +02:00
|
|
|
|
return { name: name, value: reText };
|
2016-12-25 22:56:39 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var compileConditionalSelector = function(s) {
|
2017-05-20 21:35:19 +02:00
|
|
|
|
// https://github.com/AdguardTeam/ExtendedCss/issues/31#issuecomment-302391277
|
|
|
|
|
// Prepend `:scope ` if needed.
|
|
|
|
|
if ( reNeedScope.test(s) ) {
|
|
|
|
|
s = ':scope ' + s;
|
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
return compile(s);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var compileXpathExpression = function(s) {
|
|
|
|
|
var dummy;
|
|
|
|
|
try {
|
|
|
|
|
dummy = document.createExpression(s, null) instanceof XPathExpression;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return;
|
2016-08-03 15:20:55 +02:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
return s;
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-28 18:53:05 +02:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2793
|
|
|
|
|
var normalizedOperators = new Map([
|
|
|
|
|
[ ':-abp-contains', ':has-text' ],
|
|
|
|
|
[ ':-abp-has', ':if' ],
|
|
|
|
|
[ ':contains', ':has-text' ]
|
|
|
|
|
]);
|
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
var compileArgument = new Map([
|
|
|
|
|
[ ':has', compileCSSSelector ],
|
|
|
|
|
[ ':has-text', compileText ],
|
|
|
|
|
[ ':if', compileConditionalSelector ],
|
|
|
|
|
[ ':if-not', compileConditionalSelector ],
|
|
|
|
|
[ ':matches-css', compileCSSDeclaration ],
|
|
|
|
|
[ ':matches-css-after', compileCSSDeclaration ],
|
|
|
|
|
[ ':matches-css-before', compileCSSDeclaration ],
|
|
|
|
|
[ ':xpath', compileXpathExpression ]
|
|
|
|
|
]);
|
|
|
|
|
|
2017-09-30 16:18:41 +02:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2793#issuecomment-333269387
|
|
|
|
|
// - Normalize (somewhat) the stringified version of procedural cosmetic
|
|
|
|
|
// filters -- this increase the likelihood of detecting duplicates given
|
|
|
|
|
// that uBO is able to understand syntax specific to other blockers.
|
|
|
|
|
// The normalized string version is what is reported in the logger, by
|
|
|
|
|
// design.
|
|
|
|
|
var decompile = function(compiled) {
|
|
|
|
|
var raw = [ compiled.selector ],
|
2017-10-04 19:20:43 +02:00
|
|
|
|
tasks = compiled.tasks,
|
|
|
|
|
value;
|
2017-09-30 16:18:41 +02:00
|
|
|
|
if ( Array.isArray(tasks) ) {
|
|
|
|
|
for ( var i = 0, n = tasks.length, task; i < n; i++ ) {
|
|
|
|
|
task = tasks[i];
|
|
|
|
|
switch ( task[0] ) {
|
|
|
|
|
case ':has':
|
|
|
|
|
case ':xpath':
|
|
|
|
|
raw.push(task[0], '(', task[1], ')');
|
|
|
|
|
break;
|
|
|
|
|
case ':has-text':
|
2017-10-04 19:20:43 +02:00
|
|
|
|
value = regexToRawValue.get(task[1]);
|
2017-10-05 14:38:34 +02:00
|
|
|
|
if ( value === undefined ) {
|
|
|
|
|
value = '/' + task[1] + '/';
|
|
|
|
|
}
|
|
|
|
|
raw.push(task[0], '(', value, ')');
|
2017-09-30 16:18:41 +02:00
|
|
|
|
break;
|
|
|
|
|
case ':matches-css':
|
|
|
|
|
case ':matches-css-after':
|
|
|
|
|
case ':matches-css-before':
|
2017-10-04 19:20:43 +02:00
|
|
|
|
value = regexToRawValue.get(task[1].value);
|
2017-10-05 14:38:34 +02:00
|
|
|
|
if ( value === undefined ) {
|
|
|
|
|
value = '/' + task[1].value + '/';
|
|
|
|
|
}
|
|
|
|
|
raw.push(task[0], '(', task[1].name, ': ', value, ')');
|
2017-09-30 16:18:41 +02:00
|
|
|
|
break;
|
|
|
|
|
case ':if':
|
|
|
|
|
case ':if-not':
|
|
|
|
|
raw.push(task[0], '(', decompile(task[1]), ')');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return raw.join('');
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
var compile = function(raw) {
|
2016-12-30 16:32:17 +01:00
|
|
|
|
var matches = reOperatorParser.exec(raw);
|
2016-12-27 18:32:52 +01:00
|
|
|
|
if ( matches === null ) {
|
2016-12-30 16:32:17 +01:00
|
|
|
|
if ( isValidCSSSelector(raw) ) { return { selector: raw }; }
|
2016-12-27 18:32:52 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
var tasks = [],
|
|
|
|
|
firstOperand = raw.slice(0, matches.index),
|
|
|
|
|
currentOperator = matches[1],
|
|
|
|
|
selector = raw.slice(matches.index + currentOperator.length),
|
|
|
|
|
currentArgument = '', nextOperand, nextOperator,
|
|
|
|
|
depth = 0, opening, closing;
|
|
|
|
|
if ( firstOperand !== '' && isValidCSSSelector(firstOperand) === false ) { return; }
|
|
|
|
|
for (;;) {
|
2016-12-30 16:32:17 +01:00
|
|
|
|
matches = reOperatorParser.exec(selector);
|
2016-12-25 22:56:39 +01:00
|
|
|
|
if ( matches !== null ) {
|
|
|
|
|
nextOperand = selector.slice(0, matches.index);
|
|
|
|
|
nextOperator = matches[1];
|
|
|
|
|
} else {
|
|
|
|
|
nextOperand = selector;
|
|
|
|
|
nextOperator = '';
|
2016-06-28 01:09:04 +02:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
opening = reFirstParentheses.exec(nextOperand)[0].length;
|
|
|
|
|
closing = reLastParentheses.exec(nextOperand)[0].length;
|
|
|
|
|
if ( opening > closing ) {
|
|
|
|
|
if ( depth === 0 ) { currentArgument = ''; }
|
|
|
|
|
depth += 1;
|
|
|
|
|
} else if ( closing > opening && depth > 0 ) {
|
|
|
|
|
depth -= 1;
|
|
|
|
|
if ( depth === 0 ) { nextOperand = currentArgument + nextOperand; }
|
|
|
|
|
}
|
|
|
|
|
if ( depth !== 0 ) {
|
|
|
|
|
currentArgument += nextOperand + nextOperator;
|
|
|
|
|
} else {
|
2017-09-28 18:53:05 +02:00
|
|
|
|
currentOperator = normalizedOperators.get(currentOperator) || currentOperator;
|
2016-12-25 22:56:39 +01:00
|
|
|
|
currentArgument = compileArgument.get(currentOperator)(nextOperand.slice(1, -1));
|
|
|
|
|
if ( currentArgument === undefined ) { return; }
|
|
|
|
|
tasks.push([ currentOperator, currentArgument ]);
|
|
|
|
|
currentOperator = nextOperator;
|
|
|
|
|
}
|
|
|
|
|
if ( nextOperator === '' ) { break; }
|
|
|
|
|
selector = selector.slice(matches.index + nextOperator.length);
|
2016-06-28 01:09:04 +02:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
if ( tasks.length === 0 || depth !== 0 ) { return; }
|
|
|
|
|
return { selector: firstOperand, tasks: tasks };
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
|
var entryPoint = function(raw) {
|
2016-12-25 22:56:39 +01:00
|
|
|
|
if ( raw === lastProceduralSelector ) {
|
|
|
|
|
return lastProceduralSelectorCompiled;
|
2016-06-29 23:07:33 +02:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
lastProceduralSelector = raw;
|
|
|
|
|
var compiled = compile(raw);
|
|
|
|
|
if ( compiled !== undefined ) {
|
2017-09-30 16:18:41 +02:00
|
|
|
|
compiled.raw = decompile(compiled);
|
2016-12-25 22:56:39 +01:00
|
|
|
|
compiled = JSON.stringify(compiled);
|
2015-09-27 01:07:23 +02:00
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
lastProceduralSelectorCompiled = compiled;
|
|
|
|
|
return compiled;
|
2015-03-17 13:26:35 +01:00
|
|
|
|
};
|
2017-05-12 16:35:11 +02:00
|
|
|
|
|
|
|
|
|
entryPoint.reset = function() {
|
2017-10-04 19:20:43 +02:00
|
|
|
|
regexToRawValue = new Map();
|
2017-05-12 16:35:11 +02:00
|
|
|
|
lastProceduralSelector = '';
|
|
|
|
|
lastProceduralSelectorCompiled = undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return entryPoint;
|
2015-09-08 14:45:22 +02:00
|
|
|
|
})();
|
2015-03-17 13:26:35 +01:00
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-10-30 20:19:58 +01:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1668
|
|
|
|
|
// The key must be literal: unescape escaped CSS before extracting key.
|
|
|
|
|
// It's an uncommon case, so it's best to unescape only when needed.
|
|
|
|
|
|
|
|
|
|
FilterContainer.prototype.keyFromSelector = function(selector) {
|
|
|
|
|
var matches = this.rePlainSelector.exec(selector);
|
|
|
|
|
if ( matches === null ) { return; }
|
|
|
|
|
var key = matches[0];
|
|
|
|
|
if ( key.indexOf('\\') === -1 ) {
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
key = '';
|
|
|
|
|
matches = this.rePlainSelectorEscaped.exec(selector);
|
|
|
|
|
if ( matches === null ) { return; }
|
|
|
|
|
var escaped = matches[0],
|
|
|
|
|
beg = 0;
|
|
|
|
|
this.reEscapeSequence.lastIndex = 0;
|
|
|
|
|
for (;;) {
|
|
|
|
|
matches = this.reEscapeSequence.exec(escaped);
|
|
|
|
|
if ( matches === null ) {
|
|
|
|
|
return key + escaped.slice(beg);
|
|
|
|
|
}
|
|
|
|
|
key += escaped.slice(beg, matches.index);
|
|
|
|
|
beg = this.reEscapeSequence.lastIndex;
|
|
|
|
|
if ( matches[1].length === 1 ) {
|
|
|
|
|
key += matches[1];
|
|
|
|
|
} else {
|
|
|
|
|
key += String.fromCharCode(parseInt(matches[1], 16));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterContainer.prototype.compile = function(s, writer) {
|
2014-08-21 16:56:36 +02:00
|
|
|
|
var parsed = this.parser.parse(s);
|
2015-03-16 19:58:35 +01:00
|
|
|
|
if ( parsed.cosmetic === false ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-03-16 19:58:35 +01:00
|
|
|
|
if ( parsed.invalid ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2014-08-12 18:19:54 +02:00
|
|
|
|
var hostnames = parsed.hostnames;
|
|
|
|
|
var i = hostnames.length;
|
|
|
|
|
if ( i === 0 ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.compileGenericSelector(parsed, writer);
|
2015-02-24 05:25:14 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/151
|
2015-02-24 05:25:14 +01:00
|
|
|
|
// 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];
|
2015-12-15 16:40:40 +01:00
|
|
|
|
if ( hostname.startsWith('~') === false ) {
|
2015-02-24 05:25:14 +01:00
|
|
|
|
applyGlobally = false;
|
2014-08-12 18:19:54 +02:00
|
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.compileHostnameSelector(hostname, parsed, writer);
|
2014-08-13 02:25:11 +02:00
|
|
|
|
}
|
2015-02-24 05:25:14 +01:00
|
|
|
|
if ( applyGlobally ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.compileGenericSelector(parsed, writer);
|
2015-02-24 05:25:14 +01:00
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
2014-08-12 18:19:54 +02:00
|
|
|
|
return true;
|
|
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2014-08-12 18:19:54 +02:00
|
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterContainer.prototype.compileGenericSelector = function(parsed, writer) {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
if ( parsed.unhide === 0 ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.compileGenericHideSelector(parsed, writer);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
} else {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.compileGenericUnhideSelector(parsed, writer);
|
2015-03-13 17:26:54 +01:00
|
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-03-13 17:26:54 +01:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterContainer.prototype.compileGenericHideSelector = function(parsed, writer) {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
var selector = parsed.suffix,
|
2017-10-21 19:43:46 +02:00
|
|
|
|
type = selector.charCodeAt(0),
|
|
|
|
|
key;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( type === 0x23 /* '#' */ ) {
|
2016-10-30 20:19:58 +01:00
|
|
|
|
key = this.keyFromSelector(selector);
|
|
|
|
|
if ( key === undefined ) { return; }
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Simple selector-based CSS rule: no need to test for whether the
|
|
|
|
|
// selector is valid, the regex took care of this. Most generic
|
|
|
|
|
// selector falls into that category.
|
2016-10-30 20:19:58 +01:00
|
|
|
|
if ( key === selector ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
writer.push([ 0 /* lg */, key.slice(1) ]);
|
2015-03-17 13:26:35 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Complex selector-based CSS rule.
|
2017-05-20 21:35:19 +02:00
|
|
|
|
if ( this.compileSelector(selector) !== undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
writer.push([ 1 /* lg+ */, key.slice(1), selector ]);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( type === 0x2E /* '.' */ ) {
|
|
|
|
|
key = this.keyFromSelector(selector);
|
|
|
|
|
if ( key === undefined ) { return; }
|
|
|
|
|
// Simple selector-based CSS rule: no need to test for whether the
|
|
|
|
|
// selector is valid, the regex took care of this. Most generic
|
|
|
|
|
// selector falls into that category.
|
|
|
|
|
if ( key === selector ) {
|
|
|
|
|
writer.push([ 2 /* lg */, key.slice(1) ]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Complex selector-based CSS rule.
|
|
|
|
|
if ( this.compileSelector(selector) !== undefined ) {
|
|
|
|
|
writer.push([ 3 /* lg+ */, key.slice(1), selector ]);
|
2015-03-17 13:26:35 +01:00
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
|
return;
|
2014-08-12 18:19:54 +02:00
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
var compiled = this.compileSelector(selector);
|
|
|
|
|
if ( compiled === undefined ) { return; }
|
|
|
|
|
// TODO: Detect and error on procedural cosmetic filters.
|
2015-11-19 15:36:15 +01:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/909
|
|
|
|
|
// Anything which contains a plain id/class selector can be classified
|
|
|
|
|
// as a low generic cosmetic filter.
|
|
|
|
|
var matches = this.rePlainSelectorEx.exec(selector);
|
|
|
|
|
if ( matches !== null ) {
|
|
|
|
|
key = matches[1] || matches[2];
|
|
|
|
|
type = key.charCodeAt(0);
|
|
|
|
|
writer.push([
|
|
|
|
|
type === 0x23 ? 1 : 3 /* lg+ */,
|
|
|
|
|
key.slice(1),
|
|
|
|
|
selector
|
|
|
|
|
]);
|
2015-02-24 00:31:29 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Pass this point, we are dealing with highly-generic cosmetic filters.
|
|
|
|
|
//
|
|
|
|
|
// For efficiency purpose, we will distinguish between simple and complex
|
|
|
|
|
// selectors.
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( this.reSimpleHighGeneric1.test(selector) ) {
|
|
|
|
|
writer.push([ 4 /* simple */, selector ]);
|
2015-11-19 15:36:15 +01:00
|
|
|
|
return;
|
2015-03-17 13:26:35 +01:00
|
|
|
|
}
|
2015-11-19 15:36:15 +01:00
|
|
|
|
|
2016-06-29 04:01:15 +02:00
|
|
|
|
if ( selector.indexOf(' ') === -1 ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
writer.push([ 4 /* simple */, selector ]);
|
2016-06-29 04:01:15 +02:00
|
|
|
|
} else {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
writer.push([ 5 /* complex */, selector ]);
|
2016-06-29 04:01:15 +02:00
|
|
|
|
}
|
2014-08-12 18:19:54 +02:00
|
|
|
|
};
|
2014-08-06 17:34:59 +02:00
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterContainer.prototype.compileGenericUnhideSelector = function(parsed, writer) {
|
2017-12-22 17:56:27 +01:00
|
|
|
|
var selector = parsed.suffix;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
|
|
|
|
// script:contains(...)
|
|
|
|
|
// script:inject(...)
|
|
|
|
|
if ( this.reScriptSelector.test(selector) ) {
|
2017-12-22 17:56:27 +01:00
|
|
|
|
writer.push([ 6 /* js */, '!', '', selector ]);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
// Procedural cosmetic filters are acceptable as generic exception filters.
|
2017-12-22 17:56:27 +01:00
|
|
|
|
var compiled = this.compileSelector(selector);
|
2016-12-25 22:56:39 +01:00
|
|
|
|
if ( compiled === undefined ) { return; }
|
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/497
|
2017-10-23 15:01:00 +02:00
|
|
|
|
// All generic exception filters are put in the same bucket: they are
|
|
|
|
|
// expected to be very rare.
|
2017-05-25 23:46:59 +02:00
|
|
|
|
writer.push([ 7 /* g1 */, compiled ]);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
};
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
2014-08-12 18:19:54 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterContainer.prototype.compileHostnameSelector = function(hostname, parsed, writer) {
|
2015-04-07 03:26:05 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/145
|
2014-08-12 18:19:54 +02:00
|
|
|
|
var unhide = parsed.unhide;
|
2015-12-15 16:40:40 +01:00
|
|
|
|
if ( hostname.startsWith('~') ) {
|
2014-08-12 18:19:54 +02:00
|
|
|
|
hostname = hostname.slice(1);
|
|
|
|
|
unhide ^= 1;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
}
|
2015-03-23 15:40:03 +01:00
|
|
|
|
|
|
|
|
|
// punycode if needed
|
|
|
|
|
if ( this.reHasUnicode.test(hostname) ) {
|
|
|
|
|
hostname = this.punycode.toASCII(hostname);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-25 22:56:39 +01:00
|
|
|
|
var selector = parsed.suffix,
|
|
|
|
|
domain = this.µburi.domainFromHostname(hostname),
|
2017-12-22 17:56:27 +01:00
|
|
|
|
hash;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
|
|
|
|
// script:contains(...)
|
|
|
|
|
// script:inject(...)
|
2016-12-25 22:56:39 +01:00
|
|
|
|
if ( this.reScriptSelector.test(selector) ) {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
hash = domain !== '' ? domain : this.noDomainHash;
|
|
|
|
|
if ( unhide ) {
|
|
|
|
|
hash = '!' + hash;
|
|
|
|
|
}
|
2017-12-22 17:56:27 +01:00
|
|
|
|
writer.push([ 6 /* js */, hash, hostname, selector ]);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 17:56:27 +01:00
|
|
|
|
var compiled = this.compileSelector(selector);
|
2016-12-25 22:56:39 +01:00
|
|
|
|
if ( compiled === undefined ) { return; }
|
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/188
|
2015-02-24 00:31:29 +01:00
|
|
|
|
// If not a real domain as per PSL, assign a synthetic one
|
2016-09-12 16:22:25 +02:00
|
|
|
|
if ( hostname.endsWith('.*') === false ) {
|
|
|
|
|
hash = domain !== '' ? makeHash(domain) : this.noDomainHash;
|
2014-08-12 18:19:54 +02:00
|
|
|
|
} else {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
hash = makeHash(hostname);
|
|
|
|
|
}
|
|
|
|
|
if ( unhide ) {
|
|
|
|
|
hash = '!' + hash;
|
|
|
|
|
}
|
2014-08-12 18:19:54 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
// h, hash, example.com, .promoted-tweet
|
|
|
|
|
// h, hash, example.*, .promoted-tweet
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// 8 = declarative, 9 = procedural
|
|
|
|
|
writer.push([
|
|
|
|
|
compiled.charCodeAt(0) !== 0x7B /* '{' */ ? 8 : 9,
|
|
|
|
|
hash,
|
|
|
|
|
hostname,
|
|
|
|
|
compiled
|
|
|
|
|
]);
|
2014-08-14 19:59:37 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
|
FilterContainer.prototype.fromCompiledContent = function(
|
2017-05-25 23:46:59 +02:00
|
|
|
|
reader,
|
2017-05-12 16:35:11 +02:00
|
|
|
|
skipGenericCosmetic,
|
|
|
|
|
skipCosmetic
|
|
|
|
|
) {
|
2016-08-13 22:42:58 +02:00
|
|
|
|
if ( skipCosmetic ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.skipCompiledContent(reader);
|
2016-08-13 22:42:58 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( skipGenericCosmetic ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.skipGenericCompiledContent(reader);
|
2016-08-13 22:42:58 +02:00
|
|
|
|
return;
|
2014-08-12 18:19:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
var fingerprint, args, db, filter, bucket;
|
2014-08-06 17:34:59 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
while ( reader.next() === true ) {
|
2015-02-24 00:31:29 +01:00
|
|
|
|
this.acceptedCount += 1;
|
2017-05-25 23:46:59 +02:00
|
|
|
|
fingerprint = reader.fingerprint();
|
|
|
|
|
if ( this.duplicateBuster.has(fingerprint) ) {
|
2016-03-17 18:56:21 +01:00
|
|
|
|
this.discardedCount += 1;
|
2014-08-14 19:59:37 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.duplicateBuster.add(fingerprint);
|
2014-07-04 22:47:34 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
args = reader.args();
|
|
|
|
|
|
|
|
|
|
switch ( args[0] ) {
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// low generic, simple
|
|
|
|
|
case 0: // #AdBanner
|
|
|
|
|
case 2: // .largeAd
|
|
|
|
|
db = args[0] === 0 ? this.lowlyGeneric.id : this.lowlyGeneric.cl;
|
|
|
|
|
bucket = db.complex.get(args[1]);
|
2015-02-24 00:31:29 +01:00
|
|
|
|
if ( bucket === undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
db.simple.add(args[1]);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
} else if ( Array.isArray(bucket) ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
bucket.push(args[1]);
|
2015-02-24 00:31:29 +01:00
|
|
|
|
} else {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
db.complex.set(args[1], [ bucket, args[1] ]);
|
2015-02-24 00:31:29 +01:00
|
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
2014-09-25 21:44:58 +02:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// low generic, complex
|
|
|
|
|
case 1: // #tads + div + .c
|
|
|
|
|
case 3: // .Mpopup + #Mad > #MadZone
|
2017-11-05 04:51:44 +01:00
|
|
|
|
db = args[0] === 1 ? this.lowlyGeneric.id : this.lowlyGeneric.cl;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
bucket = db.complex.get(args[1]);
|
2015-02-24 00:31:29 +01:00
|
|
|
|
if ( bucket === undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( db.simple.has(args[1]) ) {
|
|
|
|
|
db.complex.set(args[1], [ args[1], args[2] ]);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
} else {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
db.complex.set(args[1], args[2]);
|
|
|
|
|
db.simple.add(args[1]);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
}
|
|
|
|
|
} else if ( Array.isArray(bucket) ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
bucket.push(args[2]);
|
2015-02-24 00:31:29 +01:00
|
|
|
|
} else {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
db.complex.set(args[1], [ bucket, args[2] ]);
|
2015-02-24 00:31:29 +01:00
|
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
// High-high generic hide/simple selectors
|
|
|
|
|
// div[id^="allo"]
|
|
|
|
|
case 4:
|
2017-10-29 18:58:46 +01:00
|
|
|
|
this.highlyGeneric.simple.dict.add(args[1]);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
2016-06-29 04:01:15 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
// High-high generic hide/complex selectors
|
|
|
|
|
// div[id^="allo"] > span
|
|
|
|
|
case 5:
|
2017-10-29 18:58:46 +01:00
|
|
|
|
this.highlyGeneric.complex.dict.add(args[1]);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
// js, hash, example.com, script:contains(...)
|
|
|
|
|
// js, hash, example.com, script:inject(...)
|
|
|
|
|
case 6:
|
2017-12-17 16:28:12 +01:00
|
|
|
|
this.createScriptFilter(args);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/497
|
2015-03-13 17:26:54 +01:00
|
|
|
|
// Generic exception filters: expected to be a rare occurrence.
|
2017-05-25 23:46:59 +02:00
|
|
|
|
// #@#.tweet
|
|
|
|
|
case 7:
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.genericDonthideSet.add(args[1]);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
// h, hash, example.com, .promoted-tweet
|
|
|
|
|
// h, hash, example.*, .promoted-tweet
|
|
|
|
|
case 8:
|
2017-10-21 19:43:46 +02:00
|
|
|
|
case 9:
|
|
|
|
|
db = args[0] === 8 ? this.specificFilters : this.proceduralFilters;
|
2017-05-25 23:46:59 +02:00
|
|
|
|
filter = new FilterHostname(args[3], args[2]);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
bucket = db.get(args[1]);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
if ( bucket === undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
db.set(args[1], filter);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
} else if ( bucket instanceof FilterBucket ) {
|
|
|
|
|
bucket.add(filter);
|
|
|
|
|
} else {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
db.set(args[1], new FilterBucket(bucket, filter));
|
2017-05-25 23:46:59 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
this.discardedCount += 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-09-25 21:44:58 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterContainer.prototype.skipGenericCompiledContent = function(reader) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
var fingerprint, args, db, filter, bucket;
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
while ( reader.next() === true ) {
|
2016-08-13 22:42:58 +02:00
|
|
|
|
this.acceptedCount += 1;
|
2017-05-25 23:46:59 +02:00
|
|
|
|
fingerprint = reader.fingerprint();
|
|
|
|
|
if ( this.duplicateBuster.has(fingerprint) ) {
|
2016-08-13 22:42:58 +02:00
|
|
|
|
this.discardedCount += 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
args = reader.args();
|
|
|
|
|
|
|
|
|
|
switch ( args[0] ) {
|
|
|
|
|
|
|
|
|
|
// js, hash, example.com, script:contains(...)
|
|
|
|
|
// js, hash, example.com, script:inject(...)
|
|
|
|
|
case 6:
|
|
|
|
|
this.duplicateBuster.add(fingerprint);
|
2017-12-17 16:28:12 +01:00
|
|
|
|
this.createScriptFilter(args);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/497
|
|
|
|
|
// Generic exception filters: expected to be a rare occurrence.
|
|
|
|
|
case 7:
|
|
|
|
|
this.duplicateBuster.add(fingerprint);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.genericDonthideSet.add(args[1]);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// h, hash, example.com, .promoted-tweet
|
|
|
|
|
// h, hash, example.*, .promoted-tweet
|
|
|
|
|
case 8:
|
2017-10-21 19:43:46 +02:00
|
|
|
|
case 9:
|
|
|
|
|
db = args[0] === 8 ? this.specificFilters : this.proceduralFilters;
|
2017-05-25 23:46:59 +02:00
|
|
|
|
this.duplicateBuster.add(fingerprint);
|
|
|
|
|
filter = new FilterHostname(args[3], args[2]);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
bucket = db.get(args[1]);
|
2016-08-13 22:42:58 +02:00
|
|
|
|
if ( bucket === undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
db.set(args[1], filter);
|
2016-08-13 22:42:58 +02:00
|
|
|
|
} else if ( bucket instanceof FilterBucket ) {
|
|
|
|
|
bucket.add(filter);
|
|
|
|
|
} else {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
db.set(args[1], new FilterBucket(bucket, filter));
|
2016-08-13 22:42:58 +02:00
|
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
|
break;
|
2014-09-25 21:44:58 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
default:
|
|
|
|
|
this.discardedCount += 1;
|
|
|
|
|
break;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
}
|
2016-08-13 22:42:58 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
FilterContainer.prototype.skipCompiledContent = function(reader) {
|
|
|
|
|
var fingerprint, args;
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
while ( reader.next() === true ) {
|
2016-03-17 18:56:21 +01:00
|
|
|
|
this.acceptedCount += 1;
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
args = reader.args();
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
|
// js, hash, example.com, script:contains(...)
|
|
|
|
|
// js, hash, example.com, script:inject(...)
|
|
|
|
|
if ( args[0] === 6 ) {
|
|
|
|
|
fingerprint = reader.fingerprint();
|
|
|
|
|
if ( this.duplicateBuster.has(fingerprint) === false ) {
|
|
|
|
|
this.duplicateBuster.add(fingerprint);
|
2017-12-17 16:28:12 +01:00
|
|
|
|
this.createScriptFilter(args);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
}
|
2016-08-13 22:42:58 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 18:56:21 +01:00
|
|
|
|
this.discardedCount += 1;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-12-17 16:28:12 +01:00
|
|
|
|
FilterContainer.prototype.createScriptFilter = function(args) {
|
2017-12-22 17:45:07 +01:00
|
|
|
|
if ( args[3].startsWith('script:inject') ) {
|
2017-12-17 16:28:12 +01:00
|
|
|
|
return this.createUserScriptRule(args);
|
2015-12-22 22:32:09 +01:00
|
|
|
|
}
|
2017-12-22 17:45:07 +01:00
|
|
|
|
if ( args[3].startsWith('script:contains') ) {
|
2017-12-17 16:28:12 +01:00
|
|
|
|
return this.createScriptTagFilter(args);
|
2015-12-22 22:32:09 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// 0123456789012345678901
|
|
|
|
|
// script:contains(token)
|
|
|
|
|
// ^ ^
|
|
|
|
|
// 16 -1
|
|
|
|
|
|
2017-12-17 16:28:12 +01:00
|
|
|
|
FilterContainer.prototype.createScriptTagFilter = function(args) {
|
2017-12-22 17:45:07 +01:00
|
|
|
|
var hostname = args[2],
|
|
|
|
|
token = args[3].slice(16, -1);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
token = token.startsWith('/') && token.endsWith('/')
|
|
|
|
|
? token.slice(1, -1)
|
|
|
|
|
: token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
|
|
2015-09-27 16:13:31 +02:00
|
|
|
|
if ( this.scriptTagFilters.hasOwnProperty(hostname) ) {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.scriptTagFilters[hostname] += '|' + token;
|
2015-09-27 16:13:31 +02:00
|
|
|
|
} else {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.scriptTagFilters[hostname] = token;
|
2015-09-27 16:13:31 +02:00
|
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2015-09-27 16:13:31 +02:00
|
|
|
|
this.scriptTagFilterCount += 1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-09-24 20:36:08 +02:00
|
|
|
|
FilterContainer.prototype.retrieveScriptTagHostnames = function() {
|
|
|
|
|
return Object.keys(this.scriptTagFilters);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2015-09-27 16:13:31 +02:00
|
|
|
|
FilterContainer.prototype.retrieveScriptTagRegex = function(domain, hostname) {
|
|
|
|
|
if ( this.scriptTagFilterCount === 0 ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var out = [], hn = hostname, pos;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
|
|
|
|
// Hostname-based
|
2015-09-27 16:13:31 +02:00
|
|
|
|
for (;;) {
|
|
|
|
|
if ( this.scriptTagFilters.hasOwnProperty(hn) ) {
|
|
|
|
|
out.push(this.scriptTagFilters[hn]);
|
|
|
|
|
}
|
|
|
|
|
if ( hn === domain ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pos = hn.indexOf('.');
|
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
hn = hn.slice(pos + 1);
|
|
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
|
|
|
|
// Entity-based
|
2015-09-27 16:13:31 +02:00
|
|
|
|
pos = domain.indexOf('.');
|
|
|
|
|
if ( pos !== -1 ) {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
hn = domain.slice(0, pos) + '.*';
|
2015-09-27 16:13:31 +02:00
|
|
|
|
if ( this.scriptTagFilters.hasOwnProperty(hn) ) {
|
|
|
|
|
out.push(this.scriptTagFilters[hn]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( out.length !== 0 ) {
|
|
|
|
|
return out.join('|');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// userScripts{hash} => FilterHostname | FilterBucket
|
|
|
|
|
|
2017-12-17 16:28:12 +01:00
|
|
|
|
FilterContainer.prototype.createUserScriptRule = function(args) {
|
2017-12-22 17:45:07 +01:00
|
|
|
|
var hash = args[1],
|
|
|
|
|
filter = new FilterHostname(args[3].slice(14, -1), args[2]);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
var bucket = this.userScripts.get(hash);
|
|
|
|
|
if ( bucket === undefined ) {
|
|
|
|
|
this.userScripts.set(hash, filter);
|
|
|
|
|
} else if ( bucket instanceof FilterBucket ) {
|
|
|
|
|
bucket.add(filter);
|
2015-12-22 22:32:09 +01:00
|
|
|
|
} else {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.userScripts.set(hash, new FilterBucket(bucket, filter));
|
2015-12-22 22:32:09 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1954
|
|
|
|
|
|
|
|
|
|
// 01234567890123456789
|
2016-11-08 21:53:08 +01:00
|
|
|
|
// script:inject(token[, arg[, ...]])
|
|
|
|
|
// ^ ^
|
|
|
|
|
// 14 -1
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2017-12-17 14:09:47 +01:00
|
|
|
|
FilterContainer.prototype.retrieveUserScripts = function(
|
|
|
|
|
domain,
|
|
|
|
|
hostname,
|
|
|
|
|
details
|
|
|
|
|
) {
|
|
|
|
|
if ( this.userScripts.size === 0 ) { return; }
|
2016-11-03 16:20:47 +01:00
|
|
|
|
if ( µb.hiddenSettings.ignoreScriptInjectFilters === true ) { return; }
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2015-12-22 22:32:09 +01:00
|
|
|
|
var reng = µb.redirectEngine;
|
2016-09-26 19:45:55 +02:00
|
|
|
|
if ( !reng ) { return; }
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2017-12-17 14:09:47 +01:00
|
|
|
|
this.mapRegister0.clear();
|
|
|
|
|
|
|
|
|
|
var toInject = this.mapRegister0,
|
2016-09-12 16:22:25 +02:00
|
|
|
|
pos = domain.indexOf('.'),
|
2016-09-26 19:45:55 +02:00
|
|
|
|
entity = pos !== -1 ? domain.slice(0, pos) + '.*' : '';
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
|
|
|
|
// Implicit
|
|
|
|
|
var hn = hostname;
|
2015-12-22 22:32:09 +01:00
|
|
|
|
for (;;) {
|
2017-12-17 14:09:47 +01:00
|
|
|
|
this._lookupUserScript(hn + '.js', reng, toInject);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
if ( hn === domain ) { break; }
|
2015-12-22 22:32:09 +01:00
|
|
|
|
pos = hn.indexOf('.');
|
2016-09-12 16:22:25 +02:00
|
|
|
|
if ( pos === -1 ) { break; }
|
2015-12-22 22:32:09 +01:00
|
|
|
|
hn = hn.slice(pos + 1);
|
|
|
|
|
}
|
2016-09-26 19:45:55 +02:00
|
|
|
|
if ( entity !== '' ) {
|
2017-12-17 14:09:47 +01:00
|
|
|
|
this._lookupUserScript(entity + '.js', reng, toInject);
|
2016-09-26 19:45:55 +02:00
|
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
|
|
|
|
// Explicit (hash is domain).
|
2017-10-21 19:43:46 +02:00
|
|
|
|
var selectors = new Set(),
|
|
|
|
|
bucket;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
if ( (bucket = this.userScripts.get(domain)) ) {
|
|
|
|
|
bucket.retrieve(hostname, selectors);
|
|
|
|
|
}
|
|
|
|
|
if ( entity !== '' && (bucket = this.userScripts.get(entity)) ) {
|
2016-09-13 21:25:22 +02:00
|
|
|
|
bucket.retrieve(entity, selectors);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
for ( var selector of selectors ) {
|
2017-12-17 14:09:47 +01:00
|
|
|
|
this._lookupUserScript(selector, reng, toInject);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-17 14:09:47 +01:00
|
|
|
|
if ( toInject.size === 0 ) { return; }
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2017-07-31 23:03:09 +02:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2835
|
|
|
|
|
// Do not inject scriptlets if the site is under an `allow` rule.
|
|
|
|
|
if (
|
|
|
|
|
µb.userSettings.advancedUserEnabled === true &&
|
|
|
|
|
µb.sessionFirewall.evaluateCellZY(hostname, hostname, '*') === 2
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// Exceptions should be rare, so we check for exception only if there are
|
|
|
|
|
// scriptlets returned.
|
2017-12-17 14:09:47 +01:00
|
|
|
|
var exceptions = new Set();
|
2016-09-12 16:22:25 +02:00
|
|
|
|
if ( (bucket = this.userScripts.get('!' + domain)) ) {
|
|
|
|
|
bucket.retrieve(hostname, exceptions);
|
|
|
|
|
}
|
|
|
|
|
if ( entity !== '' && (bucket = this.userScripts.get('!' + entity)) ) {
|
|
|
|
|
bucket.retrieve(hostname, exceptions);
|
|
|
|
|
}
|
2017-12-17 14:09:47 +01:00
|
|
|
|
|
|
|
|
|
// Return an array of scriptlets, and log results if needed.
|
|
|
|
|
var out = [],
|
|
|
|
|
logger = µb.logger.isEnabled() ? µb.logger : null,
|
|
|
|
|
isException;
|
|
|
|
|
|
|
|
|
|
for ( var entry of toInject ) {
|
|
|
|
|
if ( (isException = exceptions.has(entry[0])) === false ) {
|
|
|
|
|
out.push(entry[1]);
|
|
|
|
|
}
|
|
|
|
|
if ( logger === null ) { continue; }
|
|
|
|
|
logger.writeOne(
|
|
|
|
|
details.tabId,
|
|
|
|
|
'cosmetic',
|
|
|
|
|
{
|
|
|
|
|
source: 'cosmetic',
|
|
|
|
|
raw: (isException ? '#@#' : '##') + 'script:inject(' + entry[0] + ')'
|
|
|
|
|
},
|
|
|
|
|
'dom',
|
|
|
|
|
details.locationURL,
|
|
|
|
|
null,
|
|
|
|
|
hostname
|
|
|
|
|
);
|
2015-12-22 22:32:09 +01:00
|
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2016-01-21 15:04:04 +01:00
|
|
|
|
return out.join('\n');
|
2015-12-22 22:32:09 +01:00
|
|
|
|
};
|
|
|
|
|
|
2017-12-17 14:09:47 +01:00
|
|
|
|
FilterContainer.prototype._lookupUserScript = function(raw, reng, toInject) {
|
|
|
|
|
if ( toInject.has(raw) ) { return; }
|
2017-12-21 23:05:25 +01:00
|
|
|
|
if ( this.userScriptCache.resetTime < reng.modifyTime ) {
|
|
|
|
|
this.userScriptCache.reset();
|
2016-11-08 21:53:08 +01:00
|
|
|
|
}
|
2017-12-21 23:05:25 +01:00
|
|
|
|
var content = this.userScriptCache.lookup(raw);
|
|
|
|
|
if ( content === undefined ) {
|
|
|
|
|
var token, args,
|
|
|
|
|
pos = raw.indexOf(',');
|
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
|
token = raw;
|
|
|
|
|
} else {
|
|
|
|
|
token = raw.slice(0, pos).trim();
|
|
|
|
|
args = raw.slice(pos + 1).trim();
|
|
|
|
|
}
|
|
|
|
|
content = reng.resourceContentFromName(token, 'application/javascript');
|
2016-11-09 15:47:44 +01:00
|
|
|
|
if ( !content ) { return; }
|
2017-12-21 23:05:25 +01:00
|
|
|
|
if ( args ) {
|
|
|
|
|
content = this._fillupUserScript(content, args);
|
|
|
|
|
if ( !content ) { return; }
|
|
|
|
|
}
|
|
|
|
|
this.userScriptCache.add(raw, content);
|
2016-09-26 19:45:55 +02:00
|
|
|
|
}
|
2017-12-17 14:09:47 +01:00
|
|
|
|
toInject.set(raw, content);
|
2016-09-26 19:45:55 +02:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-09 15:47:44 +01:00
|
|
|
|
// Fill template placeholders. Return falsy if:
|
|
|
|
|
// - At least one argument contains anything else than /\w/ and `.`
|
|
|
|
|
|
|
|
|
|
FilterContainer.prototype._fillupUserScript = function(content, args) {
|
|
|
|
|
var i = 1,
|
|
|
|
|
pos, arg;
|
|
|
|
|
while ( args !== '' ) {
|
|
|
|
|
pos = args.indexOf(',');
|
|
|
|
|
if ( pos === -1 ) { pos = args.length; }
|
2016-11-17 15:25:37 +01:00
|
|
|
|
arg = args.slice(0, pos).trim().replace(this._reEscapeScriptArg, '\\$&');
|
2016-11-09 15:47:44 +01:00
|
|
|
|
content = content.replace('{{' + i + '}}', arg);
|
|
|
|
|
args = args.slice(pos + 1).trim();
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return content;
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-17 15:25:37 +01:00
|
|
|
|
FilterContainer.prototype._reEscapeScriptArg = /[\\'"]/g;
|
2016-11-08 22:31:04 +01:00
|
|
|
|
|
2015-12-22 22:32:09 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
|
FilterContainer.prototype.toSelfie = function() {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
var selfieFromMap = function(map) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
var selfie = [];
|
2017-05-19 16:12:55 +02:00
|
|
|
|
// Note: destructuring assignment not supported before Chromium 49.
|
|
|
|
|
for ( var entry of map ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
selfie.push([ entry[0], entry[1].compile() ]);
|
2014-09-08 23:46:58 +02:00
|
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
|
return JSON.stringify(selfie);
|
2014-09-08 23:46:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
acceptedCount: this.acceptedCount,
|
2016-03-17 18:56:21 +01:00
|
|
|
|
discardedCount: this.discardedCount,
|
2016-09-12 16:22:25 +02:00
|
|
|
|
specificFilters: selfieFromMap(this.specificFilters),
|
2017-10-21 19:43:46 +02:00
|
|
|
|
proceduralFilters: selfieFromMap(this.proceduralFilters),
|
2016-08-13 22:42:58 +02:00
|
|
|
|
hasGenericHide: this.hasGenericHide,
|
2017-10-21 19:43:46 +02:00
|
|
|
|
lowlyGenericSID: µb.arrayFrom(this.lowlyGeneric.id.simple),
|
|
|
|
|
lowlyGenericCID: µb.arrayFrom(this.lowlyGeneric.id.complex),
|
|
|
|
|
lowlyGenericSCL: µb.arrayFrom(this.lowlyGeneric.cl.simple),
|
|
|
|
|
lowlyGenericCCL: µb.arrayFrom(this.lowlyGeneric.cl.complex),
|
2017-10-29 18:58:46 +01:00
|
|
|
|
highSimpleGenericHideArray: µb.arrayFrom(this.highlyGeneric.simple.dict),
|
|
|
|
|
highComplexGenericHideArray: µb.arrayFrom(this.highlyGeneric.complex.dict),
|
2017-10-21 19:43:46 +02:00
|
|
|
|
genericDonthideArray: µb.arrayFrom(this.genericDonthideSet),
|
2015-09-27 16:13:31 +02:00
|
|
|
|
scriptTagFilters: this.scriptTagFilters,
|
2015-12-22 22:32:09 +01:00
|
|
|
|
scriptTagFilterCount: this.scriptTagFilterCount,
|
2017-12-17 14:09:47 +01:00
|
|
|
|
userScripts: selfieFromMap(this.userScripts)
|
2014-09-08 23:46:58 +02:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
FilterContainer.prototype.fromSelfie = function(selfie) {
|
2016-09-12 16:22:25 +02:00
|
|
|
|
var mapFromSelfie = function(selfie) {
|
2017-05-25 23:46:59 +02:00
|
|
|
|
var entries = JSON.parse(selfie),
|
|
|
|
|
out = new Map(),
|
|
|
|
|
entry;
|
|
|
|
|
for ( var i = 0, n = entries.length; i < n; i++ ) {
|
|
|
|
|
entry = entries[i];
|
|
|
|
|
out.set(entry[0], filterFromCompiledData(entry[1]));
|
|
|
|
|
}
|
|
|
|
|
return out;
|
2014-09-08 23:46:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.acceptedCount = selfie.acceptedCount;
|
2016-03-17 18:56:21 +01:00
|
|
|
|
this.discardedCount = selfie.discardedCount;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.specificFilters = mapFromSelfie(selfie.specificFilters);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.proceduralFilters = mapFromSelfie(selfie.proceduralFilters);
|
2016-08-13 22:42:58 +02:00
|
|
|
|
this.hasGenericHide = selfie.hasGenericHide;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.lowlyGeneric.id.simple = new Set(selfie.lowlyGenericSID);
|
|
|
|
|
this.lowlyGeneric.id.complex = new Map(selfie.lowlyGenericCID);
|
|
|
|
|
this.lowlyGeneric.cl.simple = new Set(selfie.lowlyGenericSCL);
|
|
|
|
|
this.lowlyGeneric.cl.complex = new Map(selfie.lowlyGenericCCL);
|
2017-10-29 18:58:46 +01:00
|
|
|
|
this.highlyGeneric.simple.dict = new Set(selfie.highSimpleGenericHideArray);
|
|
|
|
|
this.highlyGeneric.simple.str = selfie.highSimpleGenericHideArray.join(',\n');
|
|
|
|
|
this.highlyGeneric.complex.dict = new Set(selfie.highComplexGenericHideArray);
|
|
|
|
|
this.highlyGeneric.complex.str = selfie.highComplexGenericHideArray.join(',\n');
|
2017-10-21 19:43:46 +02:00
|
|
|
|
this.genericDonthideSet = new Set(selfie.genericDonthideArray);
|
2015-09-27 16:13:31 +02:00
|
|
|
|
this.scriptTagFilters = selfie.scriptTagFilters;
|
|
|
|
|
this.scriptTagFilterCount = selfie.scriptTagFilterCount;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
this.userScripts = mapFromSelfie(selfie.userScripts);
|
2015-01-23 21:02:47 +01:00
|
|
|
|
this.frozen = true;
|
2014-09-08 23:46:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
FilterContainer.prototype.triggerSelectorCachePruner = function() {
|
|
|
|
|
// Of interest: http://fitzgeraldnick.com/weblog/40/
|
|
|
|
|
// http://googlecode.blogspot.ca/2009/07/gmail-for-mobile-html5-series-using.html
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( this.selectorCacheTimer === null ) {
|
|
|
|
|
this.selectorCacheTimer = vAPI.setTimeout(
|
|
|
|
|
this.pruneSelectorCacheAsync.bind(this),
|
|
|
|
|
this.selectorCachePruneDelay
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-12-26 21:26:44 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-08-14 19:59:37 +02:00
|
|
|
|
FilterContainer.prototype.addToSelectorCache = function(details) {
|
|
|
|
|
var hostname = details.hostname;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( typeof hostname !== 'string' || hostname === '' ) { return; }
|
2014-08-14 19:59:37 +02:00
|
|
|
|
var selectors = details.selectors;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( Array.isArray(selectors) === false ) { return; }
|
|
|
|
|
var entry = this.selectorCache.get(hostname);
|
2014-08-14 02:03:55 +02:00
|
|
|
|
if ( entry === undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
entry = SelectorCacheEntry.factory();
|
|
|
|
|
this.selectorCache.set(hostname, entry);
|
|
|
|
|
if ( this.selectorCache.size > this.selectorCacheCountMin ) {
|
|
|
|
|
this.triggerSelectorCachePruner();
|
|
|
|
|
}
|
2014-08-14 02:03:55 +02:00
|
|
|
|
}
|
2016-10-06 16:49:46 +02:00
|
|
|
|
entry.add(details);
|
2014-08-14 02:03:55 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
FilterContainer.prototype.removeFromSelectorCache = function(
|
|
|
|
|
targetHostname,
|
|
|
|
|
type
|
|
|
|
|
) {
|
|
|
|
|
var targetHostnameLength = targetHostname.length,
|
|
|
|
|
hostname, item;
|
|
|
|
|
for ( var entry of this.selectorCache ) {
|
|
|
|
|
hostname = entry[0];
|
|
|
|
|
item = entry[1];
|
2015-01-06 14:01:15 +01:00
|
|
|
|
if ( targetHostname !== '*' ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( hostname.endsWith(targetHostname) === false ) { continue; }
|
|
|
|
|
if (
|
|
|
|
|
hostname.length !== targetHostnameLength &&
|
|
|
|
|
hostname.charAt(hostname.length - targetHostnameLength - 1) !== '.'
|
|
|
|
|
) {
|
2015-01-06 14:01:15 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-12-17 16:47:52 +01:00
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
item.remove(type);
|
2014-12-17 16:32:50 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
FilterContainer.prototype.retrieveFromSelectorCache = function(
|
|
|
|
|
hostname,
|
|
|
|
|
type,
|
|
|
|
|
out
|
|
|
|
|
) {
|
|
|
|
|
var entry = this.selectorCache.get(hostname);
|
|
|
|
|
if ( entry !== undefined ) {
|
|
|
|
|
entry.retrieve(type, out);
|
2014-08-14 02:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
|
FilterContainer.prototype.pruneSelectorCacheAsync = function() {
|
|
|
|
|
this.selectorCacheTimer = null;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( this.selectorCache.size <= this.selectorCacheCountMin ) { return; }
|
2014-08-14 02:03:55 +02:00
|
|
|
|
var cache = this.selectorCache;
|
2014-12-26 21:26:44 +01:00
|
|
|
|
// Sorted from most-recently-used to least-recently-used, because
|
|
|
|
|
// we loop beginning at the end below.
|
|
|
|
|
// We can't avoid sorting because we have to keep a minimum number of
|
|
|
|
|
// entries, and these entries should always be the most-recently-used.
|
2017-10-21 19:43:46 +02:00
|
|
|
|
var hostnames = µb.arrayFrom(cache.keys())
|
|
|
|
|
.sort(function(a, b) {
|
|
|
|
|
return cache.get(b).lastAccessTime -
|
|
|
|
|
cache.get(a).lastAccessTime;
|
|
|
|
|
})
|
|
|
|
|
.slice(this.selectorCacheCountMin);
|
|
|
|
|
var obsolete = Date.now() - this.selectorCacheAgeMax,
|
|
|
|
|
hostname, entry,
|
|
|
|
|
i = hostnames.length;
|
2014-08-14 02:03:55 +02:00
|
|
|
|
while ( i-- ) {
|
2014-12-26 21:26:44 +01:00
|
|
|
|
hostname = hostnames[i];
|
2017-10-21 19:43:46 +02:00
|
|
|
|
entry = cache.get(hostname);
|
|
|
|
|
if ( entry.lastAccessTime > obsolete ) { break; }
|
2014-12-26 21:26:44 +01:00
|
|
|
|
// console.debug('pruneSelectorCacheAsync: flushing "%s"', hostname);
|
|
|
|
|
entry.dispose();
|
2017-10-21 19:43:46 +02:00
|
|
|
|
cache.delete(hostname);
|
|
|
|
|
}
|
|
|
|
|
if ( cache.size > this.selectorCacheCountMin ) {
|
|
|
|
|
this.triggerSelectorCachePruner();
|
2014-08-14 02:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-10-23 18:21:37 +02:00
|
|
|
|
FilterContainer.prototype.randomAlphaToken = function() {
|
|
|
|
|
return String.fromCharCode(Date.now() % 26 + 97) +
|
|
|
|
|
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-12-17 14:09:47 +01:00
|
|
|
|
FilterContainer.prototype.retrieveGenericSelectors = function(request) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( this.acceptedCount === 0 ) { return; }
|
|
|
|
|
if ( !request.ids && !request.classes ) { return; }
|
|
|
|
|
|
2017-10-23 18:21:37 +02:00
|
|
|
|
//console.time('cosmeticFilteringEngine.retrieveGenericSelectors');
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
|
|
|
|
var simpleSelectors = this.setRegister0,
|
|
|
|
|
complexSelectors = this.setRegister1;
|
|
|
|
|
var entry, selectors,
|
|
|
|
|
strEnd, sliceBeg, sliceEnd,
|
2017-10-25 17:42:18 +02:00
|
|
|
|
selector, bucket;
|
|
|
|
|
|
|
|
|
|
var cacheEntry = this.selectorCache.get(request.hostname),
|
|
|
|
|
previousHits = cacheEntry && cacheEntry.cosmetic || this.setRegister2;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
|
|
|
|
for ( var type in this.lowlyGeneric ) {
|
|
|
|
|
entry = this.lowlyGeneric[type];
|
|
|
|
|
selectors = request[entry.canonical];
|
|
|
|
|
if ( typeof selectors !== 'string' ) { continue; }
|
|
|
|
|
strEnd = selectors.length;
|
|
|
|
|
sliceBeg = 0;
|
|
|
|
|
do {
|
|
|
|
|
sliceEnd = selectors.indexOf('\n', sliceBeg);
|
|
|
|
|
if ( sliceEnd === -1 ) { sliceEnd = strEnd; }
|
|
|
|
|
selector = selectors.slice(sliceBeg, sliceEnd);
|
|
|
|
|
sliceBeg = sliceEnd + 1;
|
|
|
|
|
if ( entry.simple.has(selector) === false ) { continue; }
|
|
|
|
|
if ( (bucket = entry.complex.get(selector)) !== undefined ) {
|
|
|
|
|
if ( Array.isArray(bucket) ) {
|
2017-10-25 17:42:18 +02:00
|
|
|
|
for ( selector of bucket ) {
|
|
|
|
|
if ( previousHits.has(selector) === false ) {
|
|
|
|
|
complexSelectors.add(selector);
|
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
}
|
2017-10-25 17:42:18 +02:00
|
|
|
|
} else if ( previousHits.has(bucket) === false ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
complexSelectors.add(bucket);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-10-25 17:42:18 +02:00
|
|
|
|
selector = entry.prefix + selector;
|
|
|
|
|
if ( previousHits.has(selector) === false ) {
|
|
|
|
|
simpleSelectors.add(selector);
|
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
}
|
|
|
|
|
} while ( sliceBeg < strEnd );
|
2014-06-24 00:42:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Apply exceptions: it is the responsibility of the caller to provide
|
|
|
|
|
// the exceptions to be applied.
|
|
|
|
|
if ( Array.isArray(request.exceptions) ) {
|
|
|
|
|
for ( var exception of request.exceptions ) {
|
|
|
|
|
simpleSelectors.delete(exception);
|
|
|
|
|
complexSelectors.delete(exception);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-10-26 12:18:03 +02:00
|
|
|
|
if ( simpleSelectors.size === 0 && complexSelectors.size === 0 ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
var out = {
|
|
|
|
|
simple: µb.arrayFrom(simpleSelectors),
|
2017-10-26 12:18:03 +02:00
|
|
|
|
complex: µb.arrayFrom(complexSelectors),
|
|
|
|
|
injected: ''
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-10-26 12:18:03 +02:00
|
|
|
|
// Cache and inject (if user stylesheets supported) looked-up low generic
|
|
|
|
|
// cosmetic filters.
|
|
|
|
|
if ( typeof request.hostname === 'string' && request.hostname !== '' ) {
|
2017-10-25 17:42:18 +02:00
|
|
|
|
this.addToSelectorCache({
|
|
|
|
|
cost: request.surveyCost || 0,
|
2017-10-26 12:18:03 +02:00
|
|
|
|
hostname: request.hostname,
|
|
|
|
|
injectedHideFilters: '',
|
|
|
|
|
selectors: out.simple.concat(out.complex),
|
|
|
|
|
type: 'cosmetic'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If user stylesheets are supported in the current process, inject the
|
|
|
|
|
// cosmetic filters now.
|
|
|
|
|
if (
|
|
|
|
|
this.supportsUserStylesheets &&
|
2017-12-17 14:09:47 +01:00
|
|
|
|
request.tabId !== undefined &&
|
|
|
|
|
request.frameId !== undefined
|
2017-10-26 12:18:03 +02:00
|
|
|
|
) {
|
|
|
|
|
var injected = [];
|
|
|
|
|
if ( out.simple.length !== 0 ) {
|
|
|
|
|
injected.push(out.simple.join(',\n'));
|
|
|
|
|
out.simple = [];
|
|
|
|
|
}
|
|
|
|
|
if ( out.complex.length !== 0 ) {
|
|
|
|
|
injected.push(out.complex.join(',\n'));
|
|
|
|
|
out.complex = [];
|
|
|
|
|
}
|
|
|
|
|
out.injected = injected.join(',\n');
|
2017-12-17 14:09:47 +01:00
|
|
|
|
vAPI.insertCSS(request.tabId, {
|
2017-10-26 12:18:03 +02:00
|
|
|
|
code: out.injected + '\n{display:none!important;}',
|
|
|
|
|
cssOrigin: 'user',
|
2017-12-17 14:09:47 +01:00
|
|
|
|
frameId: request.frameId,
|
2017-10-26 12:18:03 +02:00
|
|
|
|
runAt: 'document_start'
|
2017-10-25 17:42:18 +02:00
|
|
|
|
});
|
2014-06-24 00:42:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Important: always clear used registers before leaving.
|
|
|
|
|
this.setRegister0.clear();
|
|
|
|
|
this.setRegister1.clear();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-10-23 18:21:37 +02:00
|
|
|
|
//console.timeEnd('cosmeticFilteringEngine.retrieveGenericSelectors');
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
|
|
|
|
return out;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
FilterContainer.prototype.retrieveDomainSelectors = function(
|
|
|
|
|
request,
|
|
|
|
|
options
|
|
|
|
|
) {
|
|
|
|
|
if ( !request.locationURL ) { return; }
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-10-23 18:21:37 +02:00
|
|
|
|
//console.time('cosmeticFilteringEngine.retrieveDomainSelectors');
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
|
var hostname = this.µburi.hostnameFromURI(request.locationURL),
|
|
|
|
|
domain = this.µburi.domainFromHostname(hostname) || hostname,
|
|
|
|
|
pos = domain.indexOf('.'),
|
2017-10-22 14:59:29 +02:00
|
|
|
|
entity = pos === -1 ? '' : domain.slice(0, pos - domain.length) + '.*',
|
2017-12-17 14:09:47 +01:00
|
|
|
|
cacheEntry = this.selectorCache.get(hostname),
|
|
|
|
|
entry;
|
2014-08-14 19:59:37 +02:00
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/587
|
2017-10-26 12:18:03 +02:00
|
|
|
|
// out.ready will tell the content script the cosmetic filtering engine is
|
2015-01-23 21:02:47 +01:00
|
|
|
|
// up and ready.
|
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/497
|
|
|
|
|
// Generic exception filters are to be applied on all pages.
|
|
|
|
|
|
2017-10-26 12:18:03 +02:00
|
|
|
|
var out = {
|
2015-01-23 21:02:47 +01:00
|
|
|
|
ready: this.frozen,
|
2017-10-25 17:42:18 +02:00
|
|
|
|
hostname: hostname,
|
2014-08-14 19:59:37 +02:00
|
|
|
|
domain: domain,
|
2016-09-12 16:22:25 +02:00
|
|
|
|
entity: entity,
|
2017-10-21 19:43:46 +02:00
|
|
|
|
declarativeFilters: [],
|
|
|
|
|
exceptionFilters: [],
|
2017-10-23 18:21:37 +02:00
|
|
|
|
hideNodeAttr: this.randomAlphaToken(),
|
|
|
|
|
hideNodeStyleSheetInjected: false,
|
2017-10-24 15:09:10 +02:00
|
|
|
|
highGenericHideSimple: '',
|
|
|
|
|
highGenericHideComplex: '',
|
2017-10-23 18:21:37 +02:00
|
|
|
|
injectedHideFilters: '',
|
|
|
|
|
networkFilters: '',
|
|
|
|
|
noDOMSurveying: this.hasGenericHide === false,
|
2017-10-21 19:43:46 +02:00
|
|
|
|
proceduralFilters: [],
|
2017-10-23 18:21:37 +02:00
|
|
|
|
scripts: undefined
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
2014-06-24 01:23:36 +02:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( options.noCosmeticFiltering !== true ) {
|
|
|
|
|
var domainHash = makeHash(domain),
|
|
|
|
|
entityHash = entity !== '' ? makeHash(entity) : undefined,
|
|
|
|
|
exception, bucket;
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Exception cosmetic filters: prime with generic exception filters.
|
|
|
|
|
var exceptionSet = this.setRegister0;
|
|
|
|
|
// Genetic exceptions (should be extremely rare).
|
|
|
|
|
for ( exception of this.genericDonthideSet ) {
|
|
|
|
|
exceptionSet.add(exception);
|
2016-08-12 14:55:35 +02:00
|
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// Specific exception cosmetic filters.
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( (bucket = this.specificFilters.get('!' + domainHash)) ) {
|
|
|
|
|
bucket.retrieve(hostname, exceptionSet);
|
|
|
|
|
}
|
2017-10-23 18:21:37 +02:00
|
|
|
|
if ( (bucket = this.proceduralFilters.get('!' + domainHash)) ) {
|
|
|
|
|
bucket.retrieve(hostname, exceptionSet);
|
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Specific entity-based exception cosmetic filters.
|
|
|
|
|
if ( entityHash !== undefined ) {
|
|
|
|
|
if ( (bucket = this.specificFilters.get('!' + entityHash)) ) {
|
|
|
|
|
bucket.retrieve(entity, exceptionSet);
|
|
|
|
|
}
|
2017-10-23 15:01:00 +02:00
|
|
|
|
if ( (bucket = this.proceduralFilters.get('!' + entityHash)) ) {
|
|
|
|
|
bucket.retrieve(entity, exceptionSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Special bucket for those filters without a valid
|
|
|
|
|
// domain name as per PSL.
|
2017-10-23 18:21:37 +02:00
|
|
|
|
if ( (bucket = this.specificFilters.get('!' + this.noDomainHash)) ) {
|
|
|
|
|
bucket.retrieve(hostname, exceptionSet);
|
|
|
|
|
}
|
2017-10-23 15:01:00 +02:00
|
|
|
|
if ( (bucket = this.proceduralFilters.get('!' + this.noDomainHash)) ) {
|
|
|
|
|
bucket.retrieve(hostname, exceptionSet);
|
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( exceptionSet.size !== 0 ) {
|
2017-10-26 12:18:03 +02:00
|
|
|
|
out.exceptionFilters = µb.arrayFrom(exceptionSet);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Declarative cosmetic filters.
|
|
|
|
|
// TODO: Should I go one step further and store specific simple and
|
|
|
|
|
// specific complex in different collections? This could simplify
|
|
|
|
|
// slightly content script code.
|
|
|
|
|
var specificSet = this.setRegister1;
|
|
|
|
|
// Specific cosmetic filters.
|
|
|
|
|
if ( (bucket = this.specificFilters.get(domainHash)) ) {
|
|
|
|
|
bucket.retrieve(hostname, specificSet);
|
|
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// Specific entity-based cosmetic filters.
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( entityHash !== undefined ) {
|
|
|
|
|
if ( (bucket = this.specificFilters.get(entityHash)) ) {
|
|
|
|
|
bucket.retrieve(entity, specificSet);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/188
|
|
|
|
|
// Special bucket for those filters without a valid domain name as per PSL
|
2016-09-12 16:22:25 +02:00
|
|
|
|
if ( (bucket = this.specificFilters.get(this.noDomainHash)) ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
bucket.retrieve(hostname, specificSet);
|
2016-08-12 14:55:35 +02:00
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Cached cosmetic filters: these are always declarative.
|
|
|
|
|
if ( cacheEntry !== undefined ) {
|
|
|
|
|
cacheEntry.retrieve('cosmetic', specificSet);
|
2017-10-26 12:18:03 +02:00
|
|
|
|
if ( out.noDOMSurveying === false ) {
|
|
|
|
|
out.noDOMSurveying = cacheEntry.cosmeticSurveyingMissCount >
|
2017-10-21 19:43:46 +02:00
|
|
|
|
cosmeticSurveyingMissCountMax;
|
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// Procedural cosmetic filters.
|
|
|
|
|
var proceduralSet = this.setRegister2;
|
|
|
|
|
// Specific cosmetic filters.
|
|
|
|
|
if ( (bucket = this.proceduralFilters.get(domainHash)) ) {
|
|
|
|
|
bucket.retrieve(hostname, proceduralSet);
|
|
|
|
|
}
|
|
|
|
|
// Specific entity-based cosmetic filters.
|
|
|
|
|
if ( entityHash !== undefined ) {
|
|
|
|
|
if ( (bucket = this.proceduralFilters.get(entityHash)) ) {
|
|
|
|
|
bucket.retrieve(entity, proceduralSet);
|
2016-08-13 22:42:58 +02:00
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/188
|
|
|
|
|
// Special bucket for those filters without a valid domain name as per PSL
|
|
|
|
|
if ( (bucket = this.proceduralFilters.get(this.noDomainHash)) ) {
|
|
|
|
|
bucket.retrieve(hostname, proceduralSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply exceptions.
|
|
|
|
|
for ( exception of exceptionSet ) {
|
|
|
|
|
specificSet.delete(exception);
|
|
|
|
|
proceduralSet.delete(exception);
|
|
|
|
|
}
|
|
|
|
|
if ( specificSet.size !== 0 ) {
|
2017-10-26 12:18:03 +02:00
|
|
|
|
out.declarativeFilters = µb.arrayFrom(specificSet);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
}
|
|
|
|
|
if ( proceduralSet.size !== 0 ) {
|
2017-10-26 12:18:03 +02:00
|
|
|
|
out.proceduralFilters = µb.arrayFrom(proceduralSet);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Highly generic cosmetic filters: sent once along with specific ones.
|
2017-10-29 18:58:46 +01:00
|
|
|
|
// A most-recent-used cache is used to skip computing the resulting set
|
|
|
|
|
// of high generics for a given set of exceptions.
|
|
|
|
|
// The resulting set of high generics is stored as a string, ready to
|
|
|
|
|
// be used as-is by the content script. The string is stored
|
|
|
|
|
// indirectly in the mru cache: this is to prevent duplication of the
|
|
|
|
|
// string in memory, which I have observed occurs when the string is
|
|
|
|
|
// stored directly as a value in a Map.
|
2017-10-21 19:43:46 +02:00
|
|
|
|
if ( options.noGenericCosmeticFiltering !== true ) {
|
2017-10-29 18:58:46 +01:00
|
|
|
|
var exceptionHash = out.exceptionFilters.join();
|
|
|
|
|
for ( var type in this.highlyGeneric ) {
|
2017-12-17 14:09:47 +01:00
|
|
|
|
entry = this.highlyGeneric[type];
|
2017-10-29 18:58:46 +01:00
|
|
|
|
var str = entry.mru.lookup(exceptionHash);
|
|
|
|
|
if ( str === undefined ) {
|
|
|
|
|
str = { s: entry.str };
|
|
|
|
|
var genericSet = entry.dict;
|
|
|
|
|
var hit = false;
|
|
|
|
|
for ( exception of exceptionSet ) {
|
|
|
|
|
if ( (hit = genericSet.has(exception)) ) { break; }
|
|
|
|
|
}
|
|
|
|
|
if ( hit ) {
|
|
|
|
|
genericSet = new Set(entry.dict);
|
|
|
|
|
for ( exception of exceptionSet ) {
|
|
|
|
|
genericSet.delete(exception);
|
|
|
|
|
}
|
|
|
|
|
str.s = µb.arrayFrom(genericSet).join(',\n');
|
|
|
|
|
}
|
|
|
|
|
entry.mru.add(exceptionHash, str);
|
2017-10-21 19:43:46 +02:00
|
|
|
|
}
|
2017-10-29 18:58:46 +01:00
|
|
|
|
out[entry.canonical] = str.s;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Important: always clear used registers before leaving.
|
|
|
|
|
this.setRegister0.clear();
|
|
|
|
|
this.setRegister1.clear();
|
|
|
|
|
this.setRegister2.clear();
|
2014-08-27 19:50:18 +02:00
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2016-09-12 16:22:25 +02:00
|
|
|
|
// Scriptlet injection.
|
2017-12-17 14:09:47 +01:00
|
|
|
|
out.scripts = this.retrieveUserScripts(domain, hostname, request);
|
2016-09-12 16:22:25 +02:00
|
|
|
|
|
2017-12-17 14:09:47 +01:00
|
|
|
|
// CSS selectors for collapsible blocked elements
|
2017-10-22 14:59:29 +02:00
|
|
|
|
if ( cacheEntry ) {
|
2017-10-23 18:21:37 +02:00
|
|
|
|
var networkFilters = [];
|
|
|
|
|
cacheEntry.retrieve('net', networkFilters);
|
2017-10-26 12:18:03 +02:00
|
|
|
|
out.networkFilters = networkFilters.join(',\n');
|
2017-10-22 14:59:29 +02:00
|
|
|
|
}
|
2014-08-14 02:03:55 +02:00
|
|
|
|
|
2017-10-23 18:21:37 +02:00
|
|
|
|
// https://github.com/gorhill/uBlock/issues/3160
|
|
|
|
|
// If user stylesheets are supported in the current process, inject the
|
2017-10-26 12:18:03 +02:00
|
|
|
|
// cosmetic filters now.
|
|
|
|
|
if (
|
|
|
|
|
this.supportsUserStylesheets &&
|
2017-12-17 14:09:47 +01:00
|
|
|
|
request.tabId !== undefined &&
|
|
|
|
|
request.frameId !== undefined
|
2017-10-26 12:18:03 +02:00
|
|
|
|
) {
|
|
|
|
|
var injectedHideFilters = [];
|
|
|
|
|
if ( out.declarativeFilters.length !== 0 ) {
|
|
|
|
|
injectedHideFilters.push(out.declarativeFilters.join(',\n'));
|
|
|
|
|
out.declarativeFilters = [];
|
|
|
|
|
}
|
|
|
|
|
if ( out.proceduralFilters.length !== 0 ) {
|
|
|
|
|
injectedHideFilters.push('[' + out.hideNodeAttr + ']');
|
|
|
|
|
out.hideNodeStyleSheetInjected = true;
|
|
|
|
|
}
|
|
|
|
|
if ( out.highGenericHideSimple.length !== 0 ) {
|
|
|
|
|
injectedHideFilters.push(out.highGenericHideSimple);
|
|
|
|
|
out.highGenericHideSimple = '';
|
|
|
|
|
}
|
|
|
|
|
if ( out.highGenericHideComplex.length !== 0 ) {
|
|
|
|
|
injectedHideFilters.push(out.highGenericHideComplex);
|
|
|
|
|
out.highGenericHideComplex = '';
|
|
|
|
|
}
|
|
|
|
|
out.injectedHideFilters = injectedHideFilters.join(',\n');
|
|
|
|
|
var details = {
|
|
|
|
|
code: '',
|
|
|
|
|
cssOrigin: 'user',
|
2017-12-17 14:09:47 +01:00
|
|
|
|
frameId: request.frameId,
|
2017-10-26 12:18:03 +02:00
|
|
|
|
runAt: 'document_start'
|
|
|
|
|
};
|
|
|
|
|
if ( out.injectedHideFilters.length !== 0 ) {
|
|
|
|
|
details.code = out.injectedHideFilters + '\n{display:none!important;}';
|
2017-12-17 14:09:47 +01:00
|
|
|
|
vAPI.insertCSS(request.tabId, details);
|
2017-10-26 12:18:03 +02:00
|
|
|
|
}
|
|
|
|
|
if ( out.networkFilters.length !== 0 ) {
|
|
|
|
|
details.code = out.networkFilters + '\n{display:none!important;}';
|
2017-12-17 14:09:47 +01:00
|
|
|
|
vAPI.insertCSS(request.tabId, details);
|
2017-10-26 12:18:03 +02:00
|
|
|
|
out.networkFilters = '';
|
|
|
|
|
}
|
2017-10-23 15:01:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-23 18:21:37 +02:00
|
|
|
|
//console.timeEnd('cosmeticFilteringEngine.retrieveDomainSelectors');
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
2017-10-26 12:18:03 +02:00
|
|
|
|
return out;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
FilterContainer.prototype.getFilterCount = function() {
|
2016-03-17 18:56:21 +01:00
|
|
|
|
return this.acceptedCount - this.discardedCount;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
return new FilterContainer();
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|