Improve `remove-[attr|class]` scriptlets

Related issue:
https://github.com/uBlockOrigin/uBlock-issues/issues/3215
This commit is contained in:
Raymond Hill 2024-04-17 09:17:49 -04:00
parent 5de19ace91
commit 91dfcbef2a
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 32 additions and 17 deletions

View File

@ -2173,18 +2173,24 @@ builtinScriptlets.push({
fn: removeAttr, fn: removeAttr,
dependencies: [ dependencies: [
'run-at.fn', 'run-at.fn',
'safe-self.fn',
], ],
}); });
function removeAttr( function removeAttr(
token = '', rawToken = '',
selector = '', rawSelector = '',
behavior = '' behavior = ''
) { ) {
if ( typeof token !== 'string' ) { return; } if ( typeof rawToken !== 'string' ) { return; }
if ( token === '' ) { return; } if ( rawToken === '' ) { return; }
const tokens = token.split(/\s*\|\s*/); const safe = safeSelf();
if ( selector === '' ) { const logPrefix = safe.makeLogPrefix('remove-attr', rawToken, rawSelector, behavior);
selector = `[${tokens.join('],[')}]`; const tokens = rawToken.split(/\s*\|\s*/);
const selector = tokens
.map(a => `${rawSelector}[${CSS.escape(a)}]`)
.join(',');
if ( safe.logLevel > 1 ) {
safe.uboLog(logPrefix, `Target selector:\n\t${selector}`);
} }
let timer; let timer;
const rmattr = ( ) => { const rmattr = ( ) => {
@ -2193,7 +2199,9 @@ function removeAttr(
const nodes = document.querySelectorAll(selector); const nodes = document.querySelectorAll(selector);
for ( const node of nodes ) { for ( const node of nodes ) {
for ( const attr of tokens ) { for ( const attr of tokens ) {
if ( node.hasAttribute(attr) === false ) { continue; }
node.removeAttribute(attr); node.removeAttribute(attr);
safe.uboLog(logPrefix, `Removed attribute '${attr}'`);
} }
} }
} catch(ex) { } catch(ex) {
@ -2213,7 +2221,7 @@ function removeAttr(
} }
} }
if ( skip ) { return; } if ( skip ) { return; }
timer = self.requestIdleCallback(rmattr, { timeout: 17 }); timer = self.requestIdleCallback(rmattr, { timeout: 67 });
}; };
const start = ( ) => { const start = ( ) => {
rmattr(); rmattr();
@ -2242,27 +2250,34 @@ builtinScriptlets.push({
world: 'ISOLATED', world: 'ISOLATED',
dependencies: [ dependencies: [
'run-at.fn', 'run-at.fn',
'safe-self.fn',
], ],
}); });
function removeClass( function removeClass(
token = '', rawToken = '',
selector = '', rawSelector = '',
behavior = '' behavior = ''
) { ) {
if ( typeof token !== 'string' ) { return; } if ( typeof rawToken !== 'string' ) { return; }
if ( token === '' ) { return; } if ( rawToken === '' ) { return; }
const classTokens = token.split(/\s*\|\s*/); const safe = safeSelf();
if ( selector === '' ) { const logPrefix = safe.makeLogPrefix('remove-class', rawToken, rawSelector, behavior);
selector = '.' + classTokens.map(a => CSS.escape(a)).join(',.'); const tokens = rawToken.split(/\s*\|\s*/);
const selector = tokens
.map(a => `${rawSelector}.${CSS.escape(a)}`)
.join(',');
if ( safe.logLevel > 1 ) {
safe.uboLog(logPrefix, `Target selector:\n\t${selector}`);
} }
const mustStay = /\bstay\b/.test(behavior); const mustStay = /\bstay\b/.test(behavior);
let timer; let timer;
const rmclass = function() { const rmclass = ( ) => {
timer = undefined; timer = undefined;
try { try {
const nodes = document.querySelectorAll(selector); const nodes = document.querySelectorAll(selector);
for ( const node of nodes ) { for ( const node of nodes ) {
node.classList.remove(...classTokens); node.classList.remove(...tokens);
safe.uboLog(logPrefix, 'Removed class(es)');
} }
} catch(ex) { } catch(ex) {
} }