Skip to content

Commit fb037e9

Browse files
committedAug 6, 2024
[mv3] Improve remove-attr.js scriptlet
Related issue: uBlockOrigin/uBOL-home#166
1 parent 417dab5 commit fb037e9

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed
 

‎assets/resources/scriptlets.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ function safeSelf() {
163163
}
164164
return self.requestAnimationFrame(fn);
165165
},
166+
offIdle(id) {
167+
if ( self.requestIdleCallback ) {
168+
return self.cancelIdleCallback(id);
169+
}
170+
return self.cancelAnimationFrame(id);
171+
}
166172
};
167173
scriptletGlobals.safeSelf = safe;
168174
if ( scriptletGlobals.bcSecret === undefined ) { return safe; }
@@ -258,7 +264,7 @@ builtinScriptlets.push({
258264
function runAt(fn, when) {
259265
const intFromReadyState = state => {
260266
const targets = {
261-
'loading': 1,
267+
'loading': 1, 'asap': 1,
262268
'interactive': 2, 'end': 2, '2': 2,
263269
'complete': 3, 'idle': 3, '3': 3,
264270
};
@@ -2266,9 +2272,20 @@ function removeAttr(
22662272
if ( safe.logLevel > 1 ) {
22672273
safe.uboLog(logPrefix, `Target selector:\n\t${selector}`);
22682274
}
2269-
let timer;
2275+
const asap = /\basap\b/.test(behavior);
2276+
let timerId;
2277+
const rmattrAsync = ( ) => {
2278+
if ( timerId !== undefined ) { return; }
2279+
timerId = safe.onIdle(( ) => {
2280+
timerId = undefined;
2281+
rmattr();
2282+
}, { timeout: 17 });
2283+
};
22702284
const rmattr = ( ) => {
2271-
timer = undefined;
2285+
if ( timerId !== undefined ) {
2286+
safe.offIdle(timerId);
2287+
timerId = undefined;
2288+
}
22722289
try {
22732290
const nodes = document.querySelectorAll(selector);
22742291
for ( const node of nodes ) {
@@ -2282,7 +2299,7 @@ function removeAttr(
22822299
}
22832300
};
22842301
const mutationHandler = mutations => {
2285-
if ( timer !== undefined ) { return; }
2302+
if ( timerId !== undefined ) { return; }
22862303
let skip = true;
22872304
for ( let i = 0; i < mutations.length && skip; i++ ) {
22882305
const { type, addedNodes, removedNodes } = mutations[i];
@@ -2295,7 +2312,7 @@ function removeAttr(
22952312
}
22962313
}
22972314
if ( skip ) { return; }
2298-
timer = safe.onIdle(rmattr, { timeout: 67 });
2315+
asap ? rmattr() : rmattrAsync();
22992316
};
23002317
const start = ( ) => {
23012318
rmattr();
@@ -2308,9 +2325,7 @@ function removeAttr(
23082325
subtree: true,
23092326
});
23102327
};
2311-
runAt(( ) => {
2312-
start();
2313-
}, /\bcomplete\b/.test(behavior) ? 'idle' : 'interactive');
2328+
runAt(( ) => { start(); }, behavior.split(/\s+/));
23142329
}
23152330

23162331
/******************************************************************************/

‎platform/mv3/extension/js/popup.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ async function commitFilteringMode() {
9191
setFilteringMode(actualLevel);
9292
}
9393
if ( actualLevel !== beforeLevel && popupPanelData.autoReload ) {
94-
browser.tabs.reload(currentTab.id);
94+
self.setTimeout(( ) => {
95+
browser.tabs.reload(currentTab.id);
96+
}, 437);
9597
}
9698
}
9799

0 commit comments

Comments
 (0)