1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-18 08:52:26 +02:00

Instantiate procedural filterer instance on demand only

The procedural filterer will be instantiated only when
needed, i.e. only when there are actual procedural
filters to enforce.
This commit is contained in:
Raymond Hill 2020-07-24 19:08:48 -04:00
parent 3b72c7cb04
commit e98ea7ea9b
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -762,6 +762,9 @@ vAPI.injectScriptlet = function(doc, text) {
this.mustApplySelectors = false; this.mustApplySelectors = false;
this.selectors = new Map(); this.selectors = new Map();
this.hiddenNodes = new Set(); this.hiddenNodes = new Set();
if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.addListener(this);
}
} }
addProceduralSelectors(aa) { addProceduralSelectors(aa) {
@ -873,7 +876,7 @@ vAPI.injectScriptlet = function(doc, text) {
onDOMCreated() { onDOMCreated() {
this.domIsReady = true; this.domIsReady = true;
this.domFilterer.commitNow(); this.domFilterer.commit();
} }
onDOMChanged(addedNodes, removedNodes) { onDOMChanged(addedNodes, removedNodes) {
@ -900,12 +903,9 @@ vAPI.injectScriptlet = function(doc, text) {
this.exceptedCSSRules = []; this.exceptedCSSRules = [];
this.reOnlySelectors = /\n\{[^\n]+/g; this.reOnlySelectors = /\n\{[^\n]+/g;
this.exceptions = []; this.exceptions = [];
this.proceduralFilterer = new DOMProceduralFilterer(this); this.proceduralFilterer = null;
this.hideNodeAttr = undefined; this.hideNodeAttr = undefined;
this.hideNodeStyleSheetInjected = false; this.hideNodeStyleSheetInjected = false;
if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.addListener(this);
}
// https://github.com/uBlockOrigin/uBlock-issues/issues/167 // https://github.com/uBlockOrigin/uBlock-issues/issues/167
// By the time the DOMContentLoaded is fired, the content script might // By the time the DOMContentLoaded is fired, the content script might
// have been disconnected from the background page. Unclear why this // have been disconnected from the background page. Unclear why this
@ -1059,13 +1059,15 @@ vAPI.injectScriptlet = function(doc, text) {
entry.injected === false entry.injected === false
) { ) {
userStylesheet.add( userStylesheet.add(
entry.selectors + '\n{' + entry.declarations + '}' `${entry.selectors}\n{${entry.declarations}}`
); );
} }
} }
this.addedCSSRules.clear(); this.addedCSSRules.clear();
userStylesheet.apply(); userStylesheet.apply();
this.proceduralFilterer.commitNow(); if ( this.proceduralFilterer instanceof Object ) {
this.proceduralFilterer.commitNow();
}
} }
commit(commitNow) { commit(commitNow) {
@ -1077,19 +1079,27 @@ vAPI.injectScriptlet = function(doc, text) {
} }
} }
proceduralFiltererInstance() {
if ( this.proceduralFilterer instanceof Object === false ) {
this.proceduralFilterer = new DOMProceduralFilterer(this);
}
return this.proceduralFilterer;
}
addProceduralSelectors(aa) { addProceduralSelectors(aa) {
this.proceduralFilterer.addProceduralSelectors(aa); if ( aa.length === 0 ) { return; }
this.proceduralFiltererInstance().addProceduralSelectors(aa);
} }
createProceduralFilter(o) { createProceduralFilter(o) {
return this.proceduralFilterer.createProceduralFilter(o); return this.proceduralFiltererInstance().createProceduralFilter(o);
} }
getAllSelectors() { getAllSelectors() {
const out = this.getAllSelectors_(false); const out = this.getAllSelectors_(false);
out.procedural = Array.from( out.procedural = this.proceduralFilterer instanceof Object
this.proceduralFilterer.selectors.values() ? Array.from(this.proceduralFilterer.selectors.values())
); : [];
return out; return out;
} }
@ -1104,17 +1114,6 @@ vAPI.injectScriptlet = function(doc, text) {
if ( selectors.length === 0 ) { return 0; } if ( selectors.length === 0 ) { return 0; }
return document.querySelectorAll(selectors.join(',\n')).length; return document.querySelectorAll(selectors.join(',\n')).length;
} }
onDOMCreated() {
this.proceduralFilterer.onDOMCreated();
}
onDOMChanged() {
this.proceduralFilterer.onDOMChanged.apply(
this.proceduralFilterer,
arguments
);
}
}; };
} }
@ -1304,6 +1303,24 @@ vAPI.injectScriptlet = function(doc, text) {
} }
}; };
const stop = function() {
document.removeEventListener('error', onResourceFailed, true);
if ( processTimer !== undefined ) {
clearTimeout(processTimer);
}
if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.removeListener(domWatcherInterface);
}
vAPI.shutdown.remove(stop);
vAPI.domCollapser = null;
};
const start = function() {
if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.addListener(domWatcherInterface);
}
};
const domWatcherInterface = { const domWatcherInterface = {
onDOMCreated: function() { onDOMCreated: function() {
if ( self.vAPI instanceof Object === false ) { return; } if ( self.vAPI instanceof Object === false ) { return; }
@ -1334,12 +1351,7 @@ vAPI.injectScriptlet = function(doc, text) {
document.addEventListener('error', onResourceFailed, true); document.addEventListener('error', onResourceFailed, true);
vAPI.shutdown.add(function() { vAPI.shutdown.add(stop);
document.removeEventListener('error', onResourceFailed, true);
if ( processTimer !== undefined ) {
clearTimeout(processTimer);
}
});
}, },
onDOMChanged: function(addedNodes) { onDOMChanged: function(addedNodes) {
if ( addedNodes.length === 0 ) { return; } if ( addedNodes.length === 0 ) { return; }
@ -1357,11 +1369,7 @@ vAPI.injectScriptlet = function(doc, text) {
} }
}; };
if ( vAPI.domWatcher instanceof Object ) { vAPI.domCollapser = { start };
vAPI.domWatcher.addListener(domWatcherInterface);
}
vAPI.domCollapser = { add, addMany, addIFrame, addIFrames, process };
} }
/******************************************************************************/ /******************************************************************************/
@ -1711,6 +1719,8 @@ vAPI.injectScriptlet = function(doc, text) {
return; return;
} }
vAPI.domCollapser.start();
if ( response.noCosmeticFiltering ) { if ( response.noCosmeticFiltering ) {
vAPI.domFilterer = null; vAPI.domFilterer = null;
vAPI.domSurveyor = null; vAPI.domSurveyor = null;