Skip to content

Commit

Permalink
Improve remove-[attr|class] scriptlets
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Apr 17, 2024
1 parent 5de19ac commit 91dfcbe
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2173,18 +2173,24 @@ builtinScriptlets.push({
fn: removeAttr,
dependencies: [
'run-at.fn',
'safe-self.fn',
],
});
function removeAttr(
token = '',
selector = '',
rawToken = '',
rawSelector = '',
behavior = ''
) {
if ( typeof token !== 'string' ) { return; }
if ( token === '' ) { return; }
const tokens = token.split(/\s*\|\s*/);
if ( selector === '' ) {
selector = `[${tokens.join('],[')}]`;
if ( typeof rawToken !== 'string' ) { return; }
if ( rawToken === '' ) { return; }
const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('remove-attr', rawToken, rawSelector, behavior);
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;
const rmattr = ( ) => {
Expand All @@ -2193,7 +2199,9 @@ function removeAttr(
const nodes = document.querySelectorAll(selector);
for ( const node of nodes ) {
for ( const attr of tokens ) {
if ( node.hasAttribute(attr) === false ) { continue; }
node.removeAttribute(attr);
safe.uboLog(logPrefix, `Removed attribute '${attr}'`);
}
}
} catch(ex) {
Expand All @@ -2213,7 +2221,7 @@ function removeAttr(
}
}
if ( skip ) { return; }
timer = self.requestIdleCallback(rmattr, { timeout: 17 });
timer = self.requestIdleCallback(rmattr, { timeout: 67 });
};
const start = ( ) => {
rmattr();
Expand Down Expand Up @@ -2242,27 +2250,34 @@ builtinScriptlets.push({
world: 'ISOLATED',
dependencies: [
'run-at.fn',
'safe-self.fn',
],
});
function removeClass(
token = '',
selector = '',
rawToken = '',
rawSelector = '',
behavior = ''
) {
if ( typeof token !== 'string' ) { return; }
if ( token === '' ) { return; }
const classTokens = token.split(/\s*\|\s*/);
if ( selector === '' ) {
selector = '.' + classTokens.map(a => CSS.escape(a)).join(',.');
if ( typeof rawToken !== 'string' ) { return; }
if ( rawToken === '' ) { return; }
const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('remove-class', rawToken, rawSelector, behavior);
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);
let timer;
const rmclass = function() {
const rmclass = ( ) => {
timer = undefined;
try {
const nodes = document.querySelectorAll(selector);
for ( const node of nodes ) {
node.classList.remove(...classTokens);
node.classList.remove(...tokens);
safe.uboLog(logPrefix, 'Removed class(es)');
}
} catch(ex) {
}
Expand Down

0 comments on commit 91dfcbe

Please sign in to comment.