Add ability to linger for `remove-class` scriptlet

Similar to related change for the `remove-attr` scriptlet:
- 0f330c7359

Related feedback:
- https://www.reddit.com/r/uBlockOrigin/comments/nsroaw/some_elements_isare_not_removed_after_the_cookie/
This commit is contained in:
Raymond Hill 2021-06-06 08:58:40 -04:00
parent c16f080cc7
commit 2de24a1184
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 35 additions and 7 deletions

View File

@ -764,7 +764,10 @@
if ( selector === '' || selector === '{{2}}' ) { if ( selector === '' || selector === '{{2}}' ) {
selector = '.' + tokens.map(a => CSS.escape(a)).join(',.'); selector = '.' + tokens.map(a => CSS.escape(a)).join(',.');
} }
let behavior = '{{3}}';
let timer;
const rmclass = function() { const rmclass = function() {
timer = undefined;
try { try {
const nodes = document.querySelectorAll(selector); const nodes = document.querySelectorAll(selector);
for ( const node of nodes ) { for ( const node of nodes ) {
@ -773,14 +776,39 @@
} catch(ex) { } catch(ex) {
} }
}; };
if ( document.readyState === 'loading' ) { const mutationHandler = mutations => {
window.addEventListener( if ( timer !== undefined ) { return; }
'DOMContentLoaded', let skip = true;
rmclass, for ( let i = 0; i < mutations.length && skip; i++ ) {
{ capture: true, once: true } const { type, addedNodes, removedNodes } = mutations[i];
); if ( type === 'attributes' ) { skip = false; }
} else { for ( let j = 0; j < addedNodes.length && skip; j++ ) {
if ( addedNodes[j].nodeType === 1 ) { skip = false; break; }
}
for ( let j = 0; j < removedNodes.length && skip; j++ ) {
if ( removedNodes[j].nodeType === 1 ) { skip = false; break; }
}
}
if ( skip ) { return; }
timer = self.requestIdleCallback(rmclass, { timeout: 67 });
};
const start = ( ) => {
rmclass(); rmclass();
if ( /\bstay\b/.test(behavior) === false ) { return; }
const observer = new MutationObserver(mutationHandler);
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: [ 'class' ],
childList: true,
subtree: true,
});
};
if ( document.readyState !== 'complete' && /\bcomplete\b/.test(behavior) ) {
self.addEventListener('load', start, { once: true });
} else if ( document.readyState === 'loading' ) {
self.addEventListener('DOMContentLoaded', start, { once: true });
} else {
start();
} }
})(); })();