Skip to content

Commit

Permalink
Instantiate procedural filterer instance on demand only
Browse files Browse the repository at this point in the history
The procedural filterer will be instantiated only when
needed, i.e. only when there are actual procedural
filters to enforce.
  • Loading branch information
gorhill committed Jul 24, 2020
1 parent 3b72c7c commit e98ea7e
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,9 @@ vAPI.injectScriptlet = function(doc, text) {
this.mustApplySelectors = false;
this.selectors = new Map();
this.hiddenNodes = new Set();
if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.addListener(this);
}
}

addProceduralSelectors(aa) {
Expand Down Expand Up @@ -873,7 +876,7 @@ vAPI.injectScriptlet = function(doc, text) {

onDOMCreated() {
this.domIsReady = true;
this.domFilterer.commitNow();
this.domFilterer.commit();
}

onDOMChanged(addedNodes, removedNodes) {
Expand All @@ -900,12 +903,9 @@ vAPI.injectScriptlet = function(doc, text) {
this.exceptedCSSRules = [];
this.reOnlySelectors = /\n\{[^\n]+/g;
this.exceptions = [];
this.proceduralFilterer = new DOMProceduralFilterer(this);
this.proceduralFilterer = null;
this.hideNodeAttr = undefined;
this.hideNodeStyleSheetInjected = false;
if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.addListener(this);
}
// https://github.com/uBlockOrigin/uBlock-issues/issues/167
// By the time the DOMContentLoaded is fired, the content script might
// have been disconnected from the background page. Unclear why this
Expand Down Expand Up @@ -1059,13 +1059,15 @@ vAPI.injectScriptlet = function(doc, text) {
entry.injected === false
) {
userStylesheet.add(
entry.selectors + '\n{' + entry.declarations + '}'
`${entry.selectors}\n{${entry.declarations}}`
);
}
}
this.addedCSSRules.clear();
userStylesheet.apply();
this.proceduralFilterer.commitNow();
if ( this.proceduralFilterer instanceof Object ) {
this.proceduralFilterer.commitNow();
}
}

commit(commitNow) {
Expand All @@ -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) {
this.proceduralFilterer.addProceduralSelectors(aa);
if ( aa.length === 0 ) { return; }
this.proceduralFiltererInstance().addProceduralSelectors(aa);
}

createProceduralFilter(o) {
return this.proceduralFilterer.createProceduralFilter(o);
return this.proceduralFiltererInstance().createProceduralFilter(o);
}

getAllSelectors() {
const out = this.getAllSelectors_(false);
out.procedural = Array.from(
this.proceduralFilterer.selectors.values()
);
out.procedural = this.proceduralFilterer instanceof Object
? Array.from(this.proceduralFilterer.selectors.values())
: [];
return out;
}

Expand All @@ -1104,17 +1114,6 @@ vAPI.injectScriptlet = function(doc, text) {
if ( selectors.length === 0 ) { return 0; }
return document.querySelectorAll(selectors.join(',\n')).length;
}

onDOMCreated() {
this.proceduralFilterer.onDOMCreated();
}

onDOMChanged() {
this.proceduralFilterer.onDOMChanged.apply(
this.proceduralFilterer,
arguments
);
}
};
}

Expand Down Expand Up @@ -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 = {
onDOMCreated: function() {
if ( self.vAPI instanceof Object === false ) { return; }
Expand Down Expand Up @@ -1334,12 +1351,7 @@ vAPI.injectScriptlet = function(doc, text) {

document.addEventListener('error', onResourceFailed, true);

vAPI.shutdown.add(function() {
document.removeEventListener('error', onResourceFailed, true);
if ( processTimer !== undefined ) {
clearTimeout(processTimer);
}
});
vAPI.shutdown.add(stop);
},
onDOMChanged: function(addedNodes) {
if ( addedNodes.length === 0 ) { return; }
Expand All @@ -1357,11 +1369,7 @@ vAPI.injectScriptlet = function(doc, text) {
}
};

if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.addListener(domWatcherInterface);
}

vAPI.domCollapser = { add, addMany, addIFrame, addIFrames, process };
vAPI.domCollapser = { start };
}

/******************************************************************************/
Expand Down Expand Up @@ -1711,6 +1719,8 @@ vAPI.injectScriptlet = function(doc, text) {
return;
}

vAPI.domCollapser.start();

if ( response.noCosmeticFiltering ) {
vAPI.domFilterer = null;
vAPI.domSurveyor = null;
Expand Down

0 comments on commit e98ea7e

Please sign in to comment.