2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-12-13 18:30:54 +01:00
|
|
|
Copyright (C) 2014-present 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
|
|
|
|
*/
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
'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
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let µb = µBlock;
|
|
|
|
let cosmeticSurveyingMissCountMax =
|
|
|
|
parseInt(vAPI.localStorage.getItem('cosmeticSurveyingMissCountMax'), 10) ||
|
|
|
|
15;
|
2016-07-10 01:21:46 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let supportsUserStylesheets = vAPI.webextFlavor.soup.has('user_stylesheet');
|
2018-04-22 16:01:33 +02:00
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/8dkvqn/116_broken_loading_custom_filters_from_my_filters/
|
|
|
|
window.addEventListener('webextFlavor', function() {
|
|
|
|
supportsUserStylesheets = vAPI.webextFlavor.soup.has('user_stylesheet');
|
|
|
|
}, { once: true });
|
|
|
|
|
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
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let filterClasses = [];
|
2014-09-08 23:46:58 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let registerFilterClass = function(ctor) {
|
2017-05-25 23:46:59 +02:00
|
|
|
filterClasses[ctor.prototype.fid] = ctor;
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let filterFromCompiledData = function(args) {
|
2017-05-25 23:46:59 +02:00
|
|
|
return filterClasses[args[0]].load(args);
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
// One hostname => one selector
|
|
|
|
|
|
|
|
let FilterOneOne = function(hostname, selector) {
|
2014-06-24 00:42:43 +02:00
|
|
|
this.hostname = hostname;
|
2018-05-31 16:41:03 +02:00
|
|
|
this.selector = selector;
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
FilterOneOne.prototype = {
|
|
|
|
fid: 8,
|
|
|
|
|
|
|
|
// Since this class can hold only one single selector, adding a new
|
|
|
|
// hostname-selector requires to morph the filter instance into a
|
|
|
|
// better-suited class.
|
|
|
|
add: function(hostname, selector) {
|
|
|
|
if ( hostname === this.hostname ) {
|
|
|
|
return new FilterOneMany(
|
|
|
|
this.hostname,
|
|
|
|
[ this.selector, selector ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return new FilterManyAny([
|
|
|
|
[ this.hostname, this.selector ],
|
|
|
|
[ hostname, selector ]
|
|
|
|
]);
|
|
|
|
},
|
2017-05-25 23:46:59 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
retrieve: function(target, out) {
|
|
|
|
if ( target.endsWith(this.hostname) === false ) { return; }
|
|
|
|
let i = target.length - this.hostname.length;
|
|
|
|
if ( i !== 0 && target.charCodeAt(i-1) !== 0x2E /* '.' */ ) { return; }
|
|
|
|
out.add(this.selector);
|
|
|
|
},
|
2014-07-13 08:36:38 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
compile: function() {
|
|
|
|
return [ this.fid, this.hostname, this.selector ];
|
|
|
|
}
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
FilterOneOne.load = function(data) {
|
|
|
|
return new FilterOneOne(data[1], data[2]);
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
registerFilterClass(FilterOneOne);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
2014-08-14 19:59:37 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
// One hostname => many selectors
|
|
|
|
|
|
|
|
let FilterOneMany = function(hostname, selectors) {
|
|
|
|
this.hostname = hostname;
|
|
|
|
this.selectors = selectors;
|
2016-06-28 01:09:04 +02:00
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
FilterOneMany.prototype = {
|
|
|
|
fid: 9,
|
2017-05-25 23:46:59 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
// Since this class can hold selectors for only one specific hostname,
|
|
|
|
// adding a new hostname will require to morph the filter instance into a
|
|
|
|
// better-suited class.
|
|
|
|
add: function(hostname, selector) {
|
|
|
|
if ( hostname === this.hostname ) {
|
|
|
|
this.selectors.push(selector);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return new FilterManyAny([
|
|
|
|
[ this.hostname, this.selectors ],
|
|
|
|
[ hostname, selector ]
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
|
|
|
|
retrieve: function(target, out) {
|
|
|
|
if ( target.endsWith(this.hostname) === false ) { return; }
|
|
|
|
let i = target.length - this.hostname.length;
|
|
|
|
if ( i !== 0 && target.charCodeAt(i-1) !== 0x2E /* '.' */ ) { return; }
|
|
|
|
for ( let selector of this.selectors ) {
|
|
|
|
out.add(selector);
|
|
|
|
}
|
|
|
|
},
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
compile: function() {
|
|
|
|
return [ this.fid, this.hostname, this.selectors ];
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
FilterOneMany.load = function(data) {
|
|
|
|
return new FilterOneMany(data[1], data[2]);
|
2016-06-28 01:09:04 +02:00
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
registerFilterClass(FilterOneMany);
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
// Many hostnames => one or many selectors
|
|
|
|
|
|
|
|
let FilterManyAny = function(entries) {
|
|
|
|
this.entries = new Map(entries);
|
2014-12-26 21:26:44 +01:00
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
FilterManyAny.prototype = {
|
|
|
|
fid: 10,
|
2014-12-26 21:26:44 +01:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
add: function(hostname, selector) {
|
|
|
|
let selectors = this.entries.get(hostname);
|
|
|
|
if ( selectors === undefined ) {
|
|
|
|
this.entries.set(hostname, selector);
|
|
|
|
} else if ( typeof selectors === 'string' ) {
|
|
|
|
this.entries.set(hostname, [ selectors, selector ]);
|
|
|
|
} else {
|
|
|
|
selectors.push(selector);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
retrieve: function(target, out) {
|
|
|
|
for ( let entry of this.entries ) {
|
|
|
|
let hostname = entry[0];
|
|
|
|
if ( target.endsWith(hostname) === false ) { continue; }
|
|
|
|
let i = target.length - hostname.length;
|
|
|
|
if ( i !== 0 && target.charCodeAt(i-1) !== 0x2E /* '.' */ ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let selectors = entry[1];
|
|
|
|
if ( typeof selectors === 'string' ) {
|
|
|
|
out.add(selectors);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for ( let selector of selectors ) {
|
|
|
|
out.add(selector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2014-12-26 21:26:44 +01:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
compile: function() {
|
|
|
|
return [ this.fid, Array.from(this.entries) ];
|
2014-12-26 21:26:44 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
FilterManyAny.load = function(data) {
|
|
|
|
return new FilterManyAny(data[1]);
|
|
|
|
};
|
2014-12-26 21:26:44 +01:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
registerFilterClass(FilterManyAny);
|
2014-12-26 21:26:44 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let SelectorCacheEntry = function() {
|
|
|
|
this.reset();
|
2014-12-26 21:26:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-08-14 19:59:37 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
SelectorCacheEntry.junkyard = [];
|
2014-12-26 21:26:44 +01:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
SelectorCacheEntry.factory = function() {
|
|
|
|
let entry = SelectorCacheEntry.junkyard.pop();
|
|
|
|
if ( entry ) {
|
|
|
|
return entry.reset();
|
2014-08-15 16:34:13 +02:00
|
|
|
}
|
2018-05-31 16:41:03 +02:00
|
|
|
return new SelectorCacheEntry();
|
2014-08-15 16:34:13 +02:00
|
|
|
};
|
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let netSelectorCacheLowWaterMark = 20;
|
|
|
|
let netSelectorCacheHighWaterMark = 30;
|
2014-08-15 16:34:13 +02:00
|
|
|
|
2014-12-26 21:26:44 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
SelectorCacheEntry.prototype = {
|
|
|
|
reset: function() {
|
|
|
|
this.cosmetic = new Set();
|
|
|
|
this.cosmeticSurveyingMissCount = 0;
|
|
|
|
this.net = new Map();
|
|
|
|
this.lastAccessTime = Date.now();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
dispose: function() {
|
|
|
|
this.cosmetic = this.net = null;
|
|
|
|
if ( SelectorCacheEntry.junkyard.length < 25 ) {
|
|
|
|
SelectorCacheEntry.junkyard.push(this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addCosmetic: function(details) {
|
|
|
|
let 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;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.cosmeticSurveyingMissCount = 0;
|
|
|
|
while ( i-- ) {
|
|
|
|
this.cosmetic.add(selectors[i]);
|
|
|
|
}
|
|
|
|
},
|
2014-12-26 21:26:44 +01:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
addNet: function(selectors) {
|
|
|
|
if ( typeof selectors === 'string' ) {
|
|
|
|
this.addNetOne(selectors, Date.now());
|
|
|
|
} else {
|
|
|
|
this.addNetMany(selectors, Date.now());
|
|
|
|
}
|
|
|
|
// 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.
|
|
|
|
if ( this.net.size < netSelectorCacheHighWaterMark ) { return; }
|
|
|
|
let dict = this.net;
|
|
|
|
let keys = Array.from(dict.keys()).sort(function(a, b) {
|
|
|
|
return dict.get(b) - dict.get(a);
|
|
|
|
}).slice(netSelectorCacheLowWaterMark);
|
|
|
|
let i = keys.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
dict.delete(keys[i]);
|
|
|
|
}
|
|
|
|
},
|
2014-08-14 02:03:55 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
addNetOne: function(selector, now) {
|
|
|
|
this.net.set(selector, now);
|
|
|
|
},
|
2014-12-26 21:26:44 +01:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
addNetMany: function(selectors, now) {
|
|
|
|
let i = selectors.length || 0;
|
|
|
|
while ( i-- ) {
|
|
|
|
this.net.set(selectors[i], now);
|
|
|
|
}
|
|
|
|
},
|
2014-12-17 16:32:50 +01:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
add: function(details) {
|
|
|
|
this.lastAccessTime = Date.now();
|
|
|
|
if ( details.type === 'cosmetic' ) {
|
|
|
|
this.addCosmetic(details);
|
|
|
|
} else {
|
|
|
|
this.addNet(details.selectors);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/420
|
|
|
|
remove: function(type) {
|
|
|
|
this.lastAccessTime = Date.now();
|
|
|
|
if ( type === undefined || type === 'cosmetic' ) {
|
|
|
|
this.cosmetic.clear();
|
|
|
|
this.cosmeticSurveyingMissCount = 0;
|
|
|
|
}
|
|
|
|
if ( type === undefined || type === 'net' ) {
|
|
|
|
this.net.clear();
|
|
|
|
}
|
|
|
|
},
|
2014-12-26 21:26:44 +01:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
retrieveToArray: function(iterator, out) {
|
|
|
|
for ( let selector of iterator ) {
|
|
|
|
out.push(selector);
|
|
|
|
}
|
|
|
|
},
|
2017-10-22 14:59:29 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
retrieveToSet: function(iterator, out) {
|
|
|
|
for ( let selector of iterator ) {
|
|
|
|
out.add(selector);
|
|
|
|
}
|
|
|
|
},
|
2017-10-22 14:59:29 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
retrieve: function(type, out) {
|
|
|
|
this.lastAccessTime = Date.now();
|
|
|
|
let 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
|
|
|
// 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.
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let FilterContainer = function() {
|
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})[^"]*"\]$/;
|
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;
|
|
|
|
|
|
|
|
// generic exception filters
|
|
|
|
this.genericDonthideSet = new Set();
|
|
|
|
|
2017-12-28 19:49:02 +01:00
|
|
|
// TODO: Think about reusing µb.staticExtFilteringEngine.HostnameBasedDB
|
|
|
|
// for both specific and procedural filters. This would require some
|
|
|
|
// refactoring.
|
2017-10-21 19:43:46 +02:00
|
|
|
// hostname, entity-based filters
|
|
|
|
this.specificFilters = 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
|
|
|
|
|
|
|
// 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() {
|
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();
|
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();
|
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
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
this.highlyGeneric.simple.str = Array.from(this.highlyGeneric.simple.dict).join(',\n');
|
2018-07-16 15:30:36 +02:00
|
|
|
this.highlyGeneric.simple.mru.reset();
|
2018-05-31 16:41:03 +02:00
|
|
|
this.highlyGeneric.complex.str = Array.from(this.highlyGeneric.complex.dict).join(',\n');
|
2018-07-16 15:30:36 +02:00
|
|
|
this.highlyGeneric.complex.mru.reset();
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
|
|
this.frozen = true;
|
|
|
|
};
|
|
|
|
|
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) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let matches = this.rePlainSelector.exec(selector);
|
2016-10-30 20:19:58 +01:00
|
|
|
if ( matches === null ) { return; }
|
2018-05-31 16:41:03 +02:00
|
|
|
let key = matches[0];
|
2016-10-30 20:19:58 +01:00
|
|
|
if ( key.indexOf('\\') === -1 ) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
matches = this.rePlainSelectorEscaped.exec(selector);
|
|
|
|
if ( matches === null ) { return; }
|
2018-05-31 16:41:03 +02:00
|
|
|
key = '';
|
|
|
|
let escaped = matches[0],
|
2016-10-30 20:19:58 +01:00
|
|
|
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-12-28 19:49:02 +01:00
|
|
|
FilterContainer.prototype.compile = function(parsed, writer) {
|
|
|
|
// 1000 = cosmetic filtering
|
|
|
|
writer.select(1000);
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let hostnames = parsed.hostnames,
|
2017-12-28 19:49:02 +01:00
|
|
|
i = hostnames.length;
|
2014-08-12 18:19:54 +02:00
|
|
|
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.
|
2018-05-31 16:41:03 +02:00
|
|
|
let applyGlobally = true;
|
2015-02-24 05:25:14 +01:00
|
|
|
while ( i-- ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let 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
|
|
|
}
|
2018-05-31 16:41:03 +02:00
|
|
|
this.compileSpecificSelector(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) {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( parsed.exception === false ) {
|
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
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
FilterContainer.prototype.compileGenericHideSelector = function(
|
|
|
|
parsed,
|
|
|
|
writer
|
|
|
|
) {
|
|
|
|
const selector = parsed.suffix;
|
2018-12-15 16:46:17 +01:00
|
|
|
const type = selector.charCodeAt(0);
|
|
|
|
let key;
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( type === 0x23 /* '#' */ ) {
|
2018-12-15 16:46:17 +01:00
|
|
|
key = this.keyFromSelector(selector);
|
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.
|
2018-12-15 16:46:17 +01:00
|
|
|
// - ###ad-bigbox
|
2016-10-30 20:19:58 +01:00
|
|
|
if ( key === selector ) {
|
2018-12-15 16:46:17 +01:00
|
|
|
writer.push([ 0, key.slice(1) ]);
|
2015-03-17 13:26:35 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-12-15 16:46:17 +01:00
|
|
|
} else if ( type === 0x2E /* '.' */ ) {
|
|
|
|
key = this.keyFromSelector(selector);
|
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.
|
2018-12-15 16:46:17 +01:00
|
|
|
// - ##.ads-bigbox
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( key === selector ) {
|
2018-12-15 16:46:17 +01:00
|
|
|
writer.push([ 2, key.slice(1) ]);
|
2017-10-21 19:43:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-08-12 18:19:54 +02:00
|
|
|
}
|
2015-02-24 00:31:29 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const compiled = µb.staticExtFilteringEngine.compileSelector(selector);
|
2018-12-15 16:46:17 +01:00
|
|
|
|
|
|
|
// Invalid cosmetic filter, possible reasons:
|
|
|
|
// - Bad syntax
|
|
|
|
// - Procedural filters (can't be generic): the compiled version of
|
|
|
|
// a procedural selector is NEVER equal to its raw version.
|
|
|
|
if ( compiled === undefined || compiled !== selector ) {
|
|
|
|
const who = writer.properties.get('assetKey') || '?';
|
|
|
|
µb.logger.writeOne({
|
|
|
|
error: `Invalid generic cosmetic filter in ${who} : ##${selector}`
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complex selector-based CSS rule:
|
|
|
|
// - ###tads + div + .c
|
|
|
|
// - ##.rscontainer > .ellip
|
|
|
|
if ( key !== undefined ) {
|
|
|
|
writer.push([
|
|
|
|
type === 0x23 /* '#' */ ? 1 : 3,
|
|
|
|
key.slice(1),
|
|
|
|
selector ]
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
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.
|
2018-12-13 18:30:54 +01:00
|
|
|
const matches = this.rePlainSelectorEx.exec(selector);
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( matches !== null ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
const key = matches[1] || matches[2];
|
2017-10-21 19:43:46 +02:00
|
|
|
writer.push([
|
2018-12-15 16:46:17 +01:00
|
|
|
key.charCodeAt(0) === 0x23 /* '#' */ ? 1 : 3,
|
2017-10-21 19:43:46 +02:00
|
|
|
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-12-28 19:49:02 +01:00
|
|
|
FilterContainer.prototype.compileGenericUnhideSelector = function(
|
|
|
|
parsed,
|
|
|
|
writer
|
|
|
|
) {
|
2016-12-25 22:56:39 +01:00
|
|
|
// Procedural cosmetic filters are acceptable as generic exception filters.
|
2018-05-31 16:41:03 +02:00
|
|
|
let compiled = µb.staticExtFilteringEngine.compileSelector(parsed.suffix);
|
2018-12-15 16:46:17 +01:00
|
|
|
if ( compiled === undefined ) {
|
|
|
|
const who = writer.properties.get('assetKey') || '?';
|
|
|
|
µb.logger.writeOne({
|
|
|
|
error: `Invalid cosmetic filter in ${who} : #@#${parsed.suffix}`
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2016-12-25 22:56:39 +01:00
|
|
|
|
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
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
FilterContainer.prototype.compileSpecificSelector = function(
|
2017-12-28 19:49:02 +01:00
|
|
|
hostname,
|
|
|
|
parsed,
|
|
|
|
writer
|
|
|
|
) {
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/145
|
2018-05-31 16:41:03 +02:00
|
|
|
let unhide = parsed.exception ? 1 : 0;
|
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
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let compiled = µb.staticExtFilteringEngine.compileSelector(parsed.suffix);
|
2018-12-15 16:46:17 +01:00
|
|
|
if ( compiled === undefined ) {
|
|
|
|
const who = writer.properties.get('assetKey') || '?';
|
|
|
|
µb.logger.writeOne({
|
|
|
|
error: `Invalid cosmetic filter in ${who} : ##${parsed.suffix}`
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2015-03-23 15:40:03 +01:00
|
|
|
|
2018-09-09 14:10:09 +02:00
|
|
|
let hash = µb.staticExtFilteringEngine.compileHostnameToHash(hostname);
|
|
|
|
|
|
|
|
// Exception?
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( unhide === 1 ) {
|
2018-09-09 14:10:09 +02:00
|
|
|
hash |= 0b0001;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Procedural?
|
|
|
|
if ( compiled.charCodeAt(0) === 0x7B ) {
|
|
|
|
hash |= 0b0010;
|
2016-09-12 16:22:25 +02:00
|
|
|
}
|
2014-08-12 18:19:54 +02:00
|
|
|
|
2018-09-09 14:10:09 +02:00
|
|
|
writer.push([ 8, hash, hostname, compiled ]);
|
2014-08-14 19:59:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-12-28 19:49:02 +01:00
|
|
|
FilterContainer.prototype.fromCompiledContent = function(reader, options) {
|
|
|
|
if ( options.skipCosmetic ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
this.skipCompiledContent(reader);
|
2016-08-13 22:42:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( options.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-12-28 19:49:02 +01:00
|
|
|
// 1000 = cosmetic filtering
|
|
|
|
reader.select(1000);
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let db, bucket;
|
|
|
|
|
2017-12-28 19:49:02 +01:00
|
|
|
while ( reader.next() ) {
|
2015-02-24 00:31:29 +01:00
|
|
|
this.acceptedCount += 1;
|
2018-05-31 16:41:03 +02:00
|
|
|
let fingerprint = reader.fingerprint();
|
2017-05-25 23:46:59 +02:00
|
|
|
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
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let args = reader.args();
|
2017-05-25 23:46:59 +02:00
|
|
|
|
|
|
|
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) ) {
|
2018-11-26 18:02:32 +01:00
|
|
|
bucket.push(db.prefix + args[1]);
|
2015-02-24 00:31:29 +01:00
|
|
|
} else {
|
2018-11-26 18:02:32 +01:00
|
|
|
db.complex.set(args[1], [ bucket, db.prefix + 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]) ) {
|
2018-11-26 18:02:32 +01:00
|
|
|
db.complex.set(args[1], [ db.prefix + 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
|
|
|
|
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
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
// hash, example.com, .promoted-tweet
|
|
|
|
// hash, example.*, .promoted-tweet
|
2017-05-25 23:46:59 +02:00
|
|
|
case 8:
|
2018-05-31 16:41:03 +02:00
|
|
|
bucket = this.specificFilters.get(args[1]);
|
2017-05-25 23:46:59 +02:00
|
|
|
if ( bucket === undefined ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
this.specificFilters.set(
|
|
|
|
args[1],
|
|
|
|
new FilterOneOne(args[2], args[3])
|
|
|
|
);
|
|
|
|
} else if ( bucket instanceof FilterManyAny ) {
|
|
|
|
bucket.add(args[2], args[3]);
|
|
|
|
} else /* can morph, so we need to replace entry in map */ {
|
|
|
|
this.specificFilters.set(
|
|
|
|
args[1],
|
|
|
|
bucket.add(args[2], args[3])
|
|
|
|
);
|
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-12-28 19:49:02 +01:00
|
|
|
// 1000 = cosmetic filtering
|
|
|
|
reader.select(1000);
|
|
|
|
|
|
|
|
while ( reader.next() ) {
|
2016-08-13 22:42:58 +02:00
|
|
|
this.acceptedCount += 1;
|
2018-12-03 14:07:54 +01:00
|
|
|
const fingerprint = reader.fingerprint();
|
2017-05-25 23:46:59 +02:00
|
|
|
if ( this.duplicateBuster.has(fingerprint) ) {
|
2016-08-13 22:42:58 +02:00
|
|
|
this.discardedCount += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-03 14:07:54 +01:00
|
|
|
const args = reader.args();
|
2017-05-25 23:46:59 +02:00
|
|
|
|
|
|
|
switch ( args[0] ) {
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
// hash, example.com, .promoted-tweet
|
|
|
|
// hash, example.*, .promoted-tweet
|
2017-05-25 23:46:59 +02:00
|
|
|
case 8:
|
2018-12-03 03:41:28 +01:00
|
|
|
this.duplicateBuster.add(fingerprint);
|
2018-12-03 14:07:54 +01:00
|
|
|
const bucket = this.specificFilters.get(args[1]);
|
2016-08-13 22:42:58 +02:00
|
|
|
if ( bucket === undefined ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
this.specificFilters.set(
|
|
|
|
args[1],
|
|
|
|
new FilterOneOne(args[2], args[3])
|
|
|
|
);
|
|
|
|
} else if ( bucket instanceof FilterManyAny ) {
|
|
|
|
bucket.add(args[2], args[3]);
|
|
|
|
} else /* can morph, so we need to replace entry in map */ {
|
|
|
|
this.specificFilters.set(
|
|
|
|
args[1],
|
|
|
|
bucket.add(args[2], args[3])
|
|
|
|
);
|
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) {
|
2017-12-28 19:49:02 +01:00
|
|
|
// 1000 = cosmetic filtering
|
|
|
|
reader.select(1000);
|
2016-08-13 22:42:58 +02:00
|
|
|
|
2017-12-28 19:49:02 +01:00
|
|
|
while ( reader.next() ) {
|
2016-03-17 18:56:21 +01:00
|
|
|
this.acceptedCount += 1;
|
|
|
|
this.discardedCount += 1;
|
2015-02-24 00:31:29 +01:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-08 23:46:58 +02:00
|
|
|
FilterContainer.prototype.toSelfie = function() {
|
2018-05-31 16:41:03 +02:00
|
|
|
let selfieFromMap = function(map) {
|
|
|
|
let entries = [];
|
|
|
|
for ( let entry of map ) {
|
|
|
|
entries.push([ entry[0], entry[1].compile() ]);
|
2014-09-08 23:46:58 +02:00
|
|
|
}
|
2018-05-31 16:41:03 +02:00
|
|
|
return entries;
|
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),
|
2016-08-13 22:42:58 +02:00
|
|
|
hasGenericHide: this.hasGenericHide,
|
2018-05-31 16:41:03 +02:00
|
|
|
lowlyGenericSID: Array.from(this.lowlyGeneric.id.simple),
|
|
|
|
lowlyGenericCID: Array.from(this.lowlyGeneric.id.complex),
|
|
|
|
lowlyGenericSCL: Array.from(this.lowlyGeneric.cl.simple),
|
|
|
|
lowlyGenericCCL: Array.from(this.lowlyGeneric.cl.complex),
|
|
|
|
highSimpleGenericHideArray: Array.from(this.highlyGeneric.simple.dict),
|
|
|
|
highComplexGenericHideArray: Array.from(this.highlyGeneric.complex.dict),
|
|
|
|
genericDonthideArray: Array.from(this.genericDonthideSet)
|
2014-09-08 23:46:58 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FilterContainer.prototype.fromSelfie = function(selfie) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let mapFromSelfie = function(entries) {
|
|
|
|
let out = new Map();
|
|
|
|
for ( let entry of entries ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
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);
|
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-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) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let hostname = details.hostname;
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( typeof hostname !== 'string' || hostname === '' ) { return; }
|
2018-05-31 16:41:03 +02:00
|
|
|
let selectors = details.selectors;
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( Array.isArray(selectors) === false ) { return; }
|
2018-05-31 16:41:03 +02:00
|
|
|
let 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
|
|
|
|
) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let targetHostnameLength = targetHostname.length;
|
|
|
|
for ( let entry of this.selectorCache ) {
|
|
|
|
let hostname = entry[0];
|
|
|
|
let 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
|
|
|
|
) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let entry = this.selectorCache.get(hostname);
|
2017-10-21 19:43:46 +02:00
|
|
|
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; }
|
2018-05-31 16:41:03 +02:00
|
|
|
let 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.
|
2018-05-31 16:41:03 +02:00
|
|
|
let hostnames = Array.from(cache.keys())
|
2017-10-21 19:43:46 +02:00
|
|
|
.sort(function(a, b) {
|
|
|
|
return cache.get(b).lastAccessTime -
|
|
|
|
cache.get(a).lastAccessTime;
|
|
|
|
})
|
|
|
|
.slice(this.selectorCacheCountMin);
|
2018-05-31 16:41:03 +02:00
|
|
|
let obsolete = Date.now() - this.selectorCacheAgeMax,
|
2017-10-21 19:43:46 +02:00
|
|
|
i = hostnames.length;
|
2014-08-14 02:03:55 +02:00
|
|
|
while ( i-- ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let hostname = hostnames[i];
|
|
|
|
let entry = cache.get(hostname);
|
2017-10-21 19:43:46 +02:00
|
|
|
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
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let simpleSelectors = this.setRegister0,
|
2017-10-21 19:43:46 +02:00
|
|
|
complexSelectors = this.setRegister1;
|
2017-10-25 17:42:18 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let cacheEntry = this.selectorCache.get(request.hostname),
|
2017-10-25 17:42:18 +02:00
|
|
|
previousHits = cacheEntry && cacheEntry.cosmetic || this.setRegister2;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
for ( let type in this.lowlyGeneric ) {
|
|
|
|
let entry = this.lowlyGeneric[type];
|
|
|
|
let selectors = request[entry.canonical];
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( typeof selectors !== 'string' ) { continue; }
|
2018-05-31 16:41:03 +02:00
|
|
|
let strEnd = selectors.length;
|
|
|
|
let sliceBeg = 0;
|
2017-10-21 19:43:46 +02:00
|
|
|
do {
|
2018-05-31 16:41:03 +02:00
|
|
|
let sliceEnd = selectors.indexOf('\n', sliceBeg);
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( sliceEnd === -1 ) { sliceEnd = strEnd; }
|
2018-05-31 16:41:03 +02:00
|
|
|
let selector = selectors.slice(sliceBeg, sliceEnd);
|
2017-10-21 19:43:46 +02:00
|
|
|
sliceBeg = sliceEnd + 1;
|
|
|
|
if ( entry.simple.has(selector) === false ) { continue; }
|
2018-05-31 16:41:03 +02:00
|
|
|
let bucket = entry.complex.get(selector);
|
|
|
|
if ( bucket !== undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
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) ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
for ( let exception of request.exceptions ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let out = {
|
|
|
|
simple: Array.from(simpleSelectors),
|
|
|
|
complex: Array.from(complexSelectors),
|
2017-10-26 12:18:03 +02:00
|
|
|
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 (
|
2018-04-22 16:01:33 +02:00
|
|
|
supportsUserStylesheets &&
|
2017-12-17 14:09:47 +01:00
|
|
|
request.tabId !== undefined &&
|
|
|
|
request.frameId !== undefined
|
2017-10-26 12:18:03 +02:00
|
|
|
) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let injected = [];
|
2017-10-26 12:18:03 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
FilterContainer.prototype.retrieveSpecificSelectors = function(
|
2017-10-21 19:43:46 +02:00
|
|
|
request,
|
|
|
|
options
|
|
|
|
) {
|
2018-05-31 16:41:03 +02:00
|
|
|
//console.time('cosmeticFilteringEngine.retrieveSpecificSelectors');
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let hostname = request.hostname,
|
|
|
|
cacheEntry = this.selectorCache.get(hostname);
|
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.
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
let out = {
|
2015-01-23 21:02:47 +01:00
|
|
|
ready: this.frozen,
|
2017-10-25 17:42:18 +02:00
|
|
|
hostname: hostname,
|
2017-12-28 19:49:02 +01:00
|
|
|
domain: request.domain,
|
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-12-28 19:49:02 +01:00
|
|
|
proceduralFilters: []
|
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 ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let entity = request.entity,
|
2018-09-09 14:10:09 +02:00
|
|
|
domainHash = µb.staticExtFilteringEngine.makeHash(request.domain),
|
|
|
|
entityHash = µb.staticExtFilteringEngine.makeHash(entity),
|
|
|
|
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.
|
2018-05-31 16:41:03 +02:00
|
|
|
let exceptionSet = this.setRegister0;
|
2017-10-21 19:43:46 +02:00
|
|
|
// Genetic exceptions (should be extremely rare).
|
2018-05-31 16:41:03 +02:00
|
|
|
for ( let exception of this.genericDonthideSet ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
exceptionSet.add(exception);
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
// Specific exception cosmetic filters.
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( domainHash !== 0 ) {
|
|
|
|
bucket = this.specificFilters.get(domainHash | 0b0001);
|
|
|
|
if ( bucket !== undefined ) {
|
|
|
|
bucket.retrieve(hostname, exceptionSet);
|
|
|
|
}
|
|
|
|
bucket = this.specificFilters.get(domainHash | 0b0011);
|
|
|
|
if ( bucket !== undefined ) {
|
|
|
|
bucket.retrieve(hostname, exceptionSet);
|
|
|
|
}
|
2017-10-23 18:21:37 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
// Specific entity-based exception cosmetic filters.
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( entityHash !== 0 ) {
|
|
|
|
bucket = this.specificFilters.get(entityHash | 0b0001);
|
2018-05-31 16:41:03 +02:00
|
|
|
if ( bucket !== undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
bucket.retrieve(entity, exceptionSet);
|
|
|
|
}
|
2018-09-09 14:10:09 +02:00
|
|
|
bucket = this.specificFilters.get(entityHash | 0b0011);
|
2018-05-31 16:41:03 +02:00
|
|
|
if ( bucket !== undefined ) {
|
2017-10-23 15:01:00 +02:00
|
|
|
bucket.retrieve(entity, exceptionSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Special bucket for those filters without a valid
|
|
|
|
// domain name as per PSL.
|
2018-09-09 14:10:09 +02:00
|
|
|
bucket = this.specificFilters.get(0 | 0b0001);
|
2018-05-31 16:41:03 +02:00
|
|
|
if ( bucket !== undefined ) {
|
2017-10-23 18:21:37 +02:00
|
|
|
bucket.retrieve(hostname, exceptionSet);
|
|
|
|
}
|
2018-09-09 14:10:09 +02:00
|
|
|
bucket = this.specificFilters.get(0 | 0b0011);
|
2018-05-31 16:41:03 +02:00
|
|
|
if ( bucket !== undefined ) {
|
2017-10-23 15:01:00 +02:00
|
|
|
bucket.retrieve(hostname, exceptionSet);
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( exceptionSet.size !== 0 ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
out.exceptionFilters = Array.from(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.
|
2018-05-31 16:41:03 +02:00
|
|
|
let specificSet = this.setRegister1;
|
2017-10-21 19:43:46 +02:00
|
|
|
// Specific cosmetic filters.
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( domainHash !== 0 ) {
|
|
|
|
bucket = this.specificFilters.get(domainHash | 0b0000);
|
|
|
|
if ( bucket !== undefined ) {
|
|
|
|
bucket.retrieve(hostname, specificSet);
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2016-09-12 16:22:25 +02:00
|
|
|
// Specific entity-based cosmetic filters.
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( entityHash !== 0 ) {
|
|
|
|
bucket = this.specificFilters.get(entityHash | 0b0000);
|
2018-05-31 16:41:03 +02:00
|
|
|
if ( bucket !== undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
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
|
2018-09-09 14:10:09 +02:00
|
|
|
// Special bucket for those filters without a valid domain name
|
|
|
|
// as per PSL
|
|
|
|
bucket = this.specificFilters.get(0 | 0b0000);
|
2018-05-31 16:41:03 +02:00
|
|
|
if ( bucket !== undefined ) {
|
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.
|
2018-05-31 16:41:03 +02:00
|
|
|
let proceduralSet = this.setRegister2;
|
2017-10-21 19:43:46 +02:00
|
|
|
// Specific cosmetic filters.
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( domainHash !== 0 ) {
|
|
|
|
bucket = this.specificFilters.get(domainHash | 0b0010);
|
|
|
|
if ( bucket !== undefined ) {
|
|
|
|
bucket.retrieve(hostname, proceduralSet);
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
// Specific entity-based cosmetic filters.
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( entityHash !== 0 ) {
|
|
|
|
bucket = this.specificFilters.get(entityHash | 0b0010);
|
2018-05-31 16:41:03 +02:00
|
|
|
if ( bucket !== undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
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
|
2018-09-09 14:10:09 +02:00
|
|
|
// Special bucket for those filters without a valid domain name
|
|
|
|
// as per PSL
|
|
|
|
bucket = this.specificFilters.get(0 | 0b0010);
|
2018-05-31 16:41:03 +02:00
|
|
|
if ( bucket !== undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
bucket.retrieve(hostname, proceduralSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply exceptions.
|
2018-05-31 16:41:03 +02:00
|
|
|
for ( let exception of exceptionSet ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
specificSet.delete(exception);
|
|
|
|
proceduralSet.delete(exception);
|
|
|
|
}
|
|
|
|
if ( specificSet.size !== 0 ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
out.declarativeFilters = Array.from(specificSet);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
if ( proceduralSet.size !== 0 ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
out.proceduralFilters = Array.from(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 ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let exceptionHash = out.exceptionFilters.join();
|
|
|
|
for ( let type in this.highlyGeneric ) {
|
|
|
|
let entry = this.highlyGeneric[type];
|
|
|
|
let str = entry.mru.lookup(exceptionHash);
|
2017-10-29 18:58:46 +01:00
|
|
|
if ( str === undefined ) {
|
|
|
|
str = { s: entry.str };
|
2018-05-31 16:41:03 +02:00
|
|
|
let genericSet = entry.dict;
|
|
|
|
let hit = false;
|
|
|
|
for ( let exception of exceptionSet ) {
|
2017-10-29 18:58:46 +01:00
|
|
|
if ( (hit = genericSet.has(exception)) ) { break; }
|
|
|
|
}
|
|
|
|
if ( hit ) {
|
|
|
|
genericSet = new Set(entry.dict);
|
2018-05-31 16:41:03 +02:00
|
|
|
for ( let exception of exceptionSet ) {
|
2017-10-29 18:58:46 +01:00
|
|
|
genericSet.delete(exception);
|
|
|
|
}
|
2018-05-31 16:41:03 +02:00
|
|
|
str.s = Array.from(genericSet).join(',\n');
|
2017-10-29 18:58:46 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2017-12-17 14:09:47 +01:00
|
|
|
// CSS selectors for collapsible blocked elements
|
2017-10-22 14:59:29 +02:00
|
|
|
if ( cacheEntry ) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let networkFilters = [];
|
2017-10-23 18:21:37 +02:00
|
|
|
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 (
|
2018-04-22 16:01:33 +02:00
|
|
|
supportsUserStylesheets &&
|
2017-12-17 14:09:47 +01:00
|
|
|
request.tabId !== undefined &&
|
|
|
|
request.frameId !== undefined
|
2017-10-26 12:18:03 +02:00
|
|
|
) {
|
2018-05-31 16:41:03 +02:00
|
|
|
let injectedHideFilters = [];
|
2017-10-26 12:18:03 +02:00
|
|
|
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');
|
2018-05-31 16:41:03 +02:00
|
|
|
let details = {
|
2017-10-26 12:18:03 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-31 16:41:03 +02:00
|
|
|
//console.timeEnd('cosmeticFilteringEngine.retrieveSpecificSelectors');
|
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();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|