[mv3] Improve `remove-attr.js` scriptlet

Related issue:
https://github.com/uBlockOrigin/uBOL-home/issues/166
This commit is contained in:
Raymond Hill 2024-08-06 14:47:03 -04:00
parent 417dab538c
commit fb037e97d0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 26 additions and 9 deletions

View File

@ -163,6 +163,12 @@ function safeSelf() {
} }
return self.requestAnimationFrame(fn); return self.requestAnimationFrame(fn);
}, },
offIdle(id) {
if ( self.requestIdleCallback ) {
return self.cancelIdleCallback(id);
}
return self.cancelAnimationFrame(id);
}
}; };
scriptletGlobals.safeSelf = safe; scriptletGlobals.safeSelf = safe;
if ( scriptletGlobals.bcSecret === undefined ) { return safe; } if ( scriptletGlobals.bcSecret === undefined ) { return safe; }
@ -258,7 +264,7 @@ builtinScriptlets.push({
function runAt(fn, when) { function runAt(fn, when) {
const intFromReadyState = state => { const intFromReadyState = state => {
const targets = { const targets = {
'loading': 1, 'loading': 1, 'asap': 1,
'interactive': 2, 'end': 2, '2': 2, 'interactive': 2, 'end': 2, '2': 2,
'complete': 3, 'idle': 3, '3': 3, 'complete': 3, 'idle': 3, '3': 3,
}; };
@ -2266,9 +2272,20 @@ function removeAttr(
if ( safe.logLevel > 1 ) { if ( safe.logLevel > 1 ) {
safe.uboLog(logPrefix, `Target selector:\n\t${selector}`); safe.uboLog(logPrefix, `Target selector:\n\t${selector}`);
} }
let timer; const asap = /\basap\b/.test(behavior);
let timerId;
const rmattrAsync = ( ) => {
if ( timerId !== undefined ) { return; }
timerId = safe.onIdle(( ) => {
timerId = undefined;
rmattr();
}, { timeout: 17 });
};
const rmattr = ( ) => { const rmattr = ( ) => {
timer = undefined; if ( timerId !== undefined ) {
safe.offIdle(timerId);
timerId = undefined;
}
try { try {
const nodes = document.querySelectorAll(selector); const nodes = document.querySelectorAll(selector);
for ( const node of nodes ) { for ( const node of nodes ) {
@ -2282,7 +2299,7 @@ function removeAttr(
} }
}; };
const mutationHandler = mutations => { const mutationHandler = mutations => {
if ( timer !== undefined ) { return; } if ( timerId !== undefined ) { return; }
let skip = true; let skip = true;
for ( let i = 0; i < mutations.length && skip; i++ ) { for ( let i = 0; i < mutations.length && skip; i++ ) {
const { type, addedNodes, removedNodes } = mutations[i]; const { type, addedNodes, removedNodes } = mutations[i];
@ -2295,7 +2312,7 @@ function removeAttr(
} }
} }
if ( skip ) { return; } if ( skip ) { return; }
timer = safe.onIdle(rmattr, { timeout: 67 }); asap ? rmattr() : rmattrAsync();
}; };
const start = ( ) => { const start = ( ) => {
rmattr(); rmattr();
@ -2308,9 +2325,7 @@ function removeAttr(
subtree: true, subtree: true,
}); });
}; };
runAt(( ) => { runAt(( ) => { start(); }, behavior.split(/\s+/));
start();
}, /\bcomplete\b/.test(behavior) ? 'idle' : 'interactive');
} }
/******************************************************************************/ /******************************************************************************/

View File

@ -91,7 +91,9 @@ async function commitFilteringMode() {
setFilteringMode(actualLevel); setFilteringMode(actualLevel);
} }
if ( actualLevel !== beforeLevel && popupPanelData.autoReload ) { if ( actualLevel !== beforeLevel && popupPanelData.autoReload ) {
browser.tabs.reload(currentTab.id); self.setTimeout(( ) => {
browser.tabs.reload(currentTab.id);
}, 437);
} }
} }