Skip to content

Commit

Permalink
Ensure procedural filters are applied at least once
Browse files Browse the repository at this point in the history
Related feedback:
- uBlockOrigin/uBlock-issues#2261 (comment)

Cosmetic filters will be applied unconditionally at least
once at DOMContentLoaded time.
  • Loading branch information
gorhill committed Nov 12, 2022
1 parent 3d24b89 commit 73c2dec
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
10 changes: 2 additions & 8 deletions src/js/contentscript-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ PSelectorRoot.prototype.hit = false;
class ProceduralFilterer {
constructor(domFilterer) {
this.domFilterer = domFilterer;
this.domIsReady = false;
this.domIsWatched = false;
this.mustApplySelectors = false;
this.selectors = new Map();
this.masterToken = vAPI.randomToken();
Expand All @@ -455,7 +453,7 @@ class ProceduralFilterer {

addProceduralSelectors(selectors) {
const addedSelectors = [];
let mustCommit = this.domIsWatched;
let mustCommit = false;
for ( const selector of selectors ) {
if ( this.selectors.has(selector.raw) ) { continue; }
let style, styleToken;
Expand Down Expand Up @@ -483,9 +481,7 @@ class ProceduralFilterer {
}

commitNow() {
if ( this.selectors.size === 0 || this.domIsReady === false ) {
return;
}
if ( this.selectors.size === 0 ) { return; }

this.mustApplySelectors = false;

Expand Down Expand Up @@ -567,8 +563,6 @@ class ProceduralFilterer {
}

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

onDOMChanged(addedNodes, removedNodes) {
Expand Down
17 changes: 4 additions & 13 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,26 +495,13 @@ vAPI.DOMFilterer = class {
this.commitTimer = new vAPI.SafeAnimationFrame(
( ) => { this.commitNow(); }
);
this.domIsReady = document.readyState !== 'loading';
this.disabled = false;
this.listeners = [];
this.stylesheets = [];
this.exceptedCSSRules = [];
this.exceptions = [];
this.convertedProceduralFilters = [];
this.proceduralFilterer = null;
// 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
// would happen, so far seems to be a Chromium-specific behavior at
// launch time.
if ( this.domIsReady !== true ) {
document.addEventListener('DOMContentLoaded', ( ) => {
if ( vAPI instanceof Object === false ) { return; }
this.domIsReady = true;
this.commit();
});
}
}

explodeCSS(css) {
Expand Down Expand Up @@ -1241,6 +1228,10 @@ vAPI.DOMFilterer = class {
what: 'shouldRenderNoscriptTags',
});

if ( vAPI.domFilterer instanceof Object ) {
vAPI.domFilterer.commitNow();
}

if ( vAPI.domWatcher instanceof Object ) {
vAPI.domWatcher.start();
}
Expand Down

0 comments on commit 73c2dec

Please sign in to comment.