code review: avoid reinjecting already injected low generic cosmetic filters

This commit is contained in:
gorhill 2017-10-25 11:42:18 -04:00
parent 26e9bb7c19
commit 6b65e3f406
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
2 changed files with 51 additions and 27 deletions

View file

@ -240,7 +240,7 @@ vAPI.domWatcher = (function() {
}
}
if ( addedNodeLists.length !== 0 || removedNodes ) {
safeObserverHandlerTimer.start();
safeObserverHandlerTimer.start(1);
}
//console.timeEnd('dom watcher/observer handler');
};
@ -276,11 +276,9 @@ vAPI.domWatcher = (function() {
if ( listeners.indexOf(listener) !== -1 ) { return; }
listeners.push(listener);
listenerIteratorDirty = true;
if ( domIsReady ) {
if ( domIsReady !== true ) { return; }
listener.onDOMCreated();
} else {
startMutationObserver();
}
};
var removeListener = function(listener) {
@ -1004,6 +1002,7 @@ vAPI.domCollapser = (function() {
vAPI.domSurveyor = (function() {
var messaging = vAPI.messaging,
domFilterer,
hostname = '',
queriedIds = new Set(),
queriedClasses = new Set(),
pendingIdNodes = { nodes: [], added: [] },
@ -1176,7 +1175,7 @@ vAPI.domSurveyor = (function() {
'contentscript',
{
what: 'retrieveGenericCosmeticSelectors',
frameURL: window.location.href,
hostname: hostname,
ids: ids.join('\n'),
classes: classes.join('\n'),
exceptions: domFilterer.exceptions,
@ -1245,11 +1244,15 @@ vAPI.domSurveyor = (function() {
}
};
if ( vAPI.domWatcher instanceof Object ) {
var start = function(details) {
if ( vAPI.domWatcher instanceof Object === false ) { return; }
hostname = details.hostname;
vAPI.domWatcher.addListener(domWatcherInterface);
}
};
return {};
return {
start: start
};
})();
/******************************************************************************/
@ -1270,11 +1273,21 @@ vAPI.domSurveyor = (function() {
document.removeEventListener('DOMContentLoaded', bootstrapPhase2);
}
if ( vAPI instanceof Object && vAPI.domWatcher instanceof Object ) {
if ( vAPI instanceof Object === false ) {
return;
}
if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.start();
}
if ( window !== window.top || !vAPI.domFilterer ) { return; }
// Element picker works only in top window for now.
if (
window !== window.top ||
vAPI.domFilterer instanceof Object === false
) {
return;
}
// To send mouse coordinates to main process, as the chrome API fails
// to provide the mouse position to context menu listeners.
@ -1375,6 +1388,10 @@ vAPI.domSurveyor = (function() {
vAPI.injectedScripts = text;
}
if ( vAPI.domSurveyor instanceof Object ) {
vAPI.domSurveyor.start(cfeDetails);
}
// https://github.com/chrisaljoudi/uBlock/issues/587
// If no filters were found, maybe the script was injected before
// uBlock's process was fully initialized. When this happens, pages

View file

@ -1949,7 +1949,10 @@ FilterContainer.prototype.retrieveGenericSelectors = function(request) {
complexSelectors = this.setRegister1;
var entry, selectors,
strEnd, sliceBeg, sliceEnd,
selector, bucket, item;
selector, bucket;
var cacheEntry = this.selectorCache.get(request.hostname),
previousHits = cacheEntry && cacheEntry.cosmetic || this.setRegister2;
for ( var type in this.lowlyGeneric ) {
entry = this.lowlyGeneric[type];
@ -1965,14 +1968,19 @@ FilterContainer.prototype.retrieveGenericSelectors = function(request) {
if ( entry.simple.has(selector) === false ) { continue; }
if ( (bucket = entry.complex.get(selector)) !== undefined ) {
if ( Array.isArray(bucket) ) {
for ( item of bucket ) {
complexSelectors.add(item);
for ( selector of bucket ) {
if ( previousHits.has(selector) === false ) {
complexSelectors.add(selector);
}
} else {
}
} else if ( previousHits.has(bucket) === false ) {
complexSelectors.add(bucket);
}
} else {
simpleSelectors.add(entry.prefix + selector);
selector = entry.prefix + selector;
if ( previousHits.has(selector) === false ) {
simpleSelectors.add(selector);
}
}
} while ( sliceBeg < strEnd );
}
@ -1994,18 +2002,16 @@ FilterContainer.prototype.retrieveGenericSelectors = function(request) {
// Cache looked-up low generic cosmetic filters.
if (
(simpleSelectors.size !== 0 || complexSelectors.size !== 0) &&
(typeof request.frameURL === 'string')
(typeof request.hostname === 'string') &&
(request.hostname !== '')
) {
var hostname = µb.URI.hostnameFromURI(request.frameURL);
if ( hostname !== '' ) {
this.addToSelectorCache({
selectors: out.simple.concat(out.complex),
type: 'cosmetic',
hostname: hostname,
hostname: request.hostname,
cost: request.surveyCost || 0,
});
}
}
// Important: always clear used registers before leaving.
this.setRegister0.clear();
@ -2045,6 +2051,7 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
var r = {
ready: this.frozen,
hostname: hostname,
domain: domain,
entity: entity,
declarativeFilters: [],