@@ -163,6 +163,12 @@ function safeSelf() {
163
163
}
164
164
return self . requestAnimationFrame ( fn ) ;
165
165
} ,
166
+ offIdle ( id ) {
167
+ if ( self . requestIdleCallback ) {
168
+ return self . cancelIdleCallback ( id ) ;
169
+ }
170
+ return self . cancelAnimationFrame ( id ) ;
171
+ }
166
172
} ;
167
173
scriptletGlobals . safeSelf = safe ;
168
174
if ( scriptletGlobals . bcSecret === undefined ) { return safe ; }
@@ -258,7 +264,7 @@ builtinScriptlets.push({
258
264
function runAt ( fn , when ) {
259
265
const intFromReadyState = state => {
260
266
const targets = {
261
- 'loading' : 1 ,
267
+ 'loading' : 1 , 'asap' : 1 ,
262
268
'interactive' : 2 , 'end' : 2 , '2' : 2 ,
263
269
'complete' : 3 , 'idle' : 3 , '3' : 3 ,
264
270
} ;
@@ -2266,9 +2272,20 @@ function removeAttr(
2266
2272
if ( safe . logLevel > 1 ) {
2267
2273
safe . uboLog ( logPrefix , `Target selector:\n\t${ selector } ` ) ;
2268
2274
}
2269
- let timer ;
2275
+ const asap = / \b a s a p \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
+ } ;
2270
2284
const rmattr = ( ) => {
2271
- timer = undefined ;
2285
+ if ( timerId !== undefined ) {
2286
+ safe . offIdle ( timerId ) ;
2287
+ timerId = undefined ;
2288
+ }
2272
2289
try {
2273
2290
const nodes = document . querySelectorAll ( selector ) ;
2274
2291
for ( const node of nodes ) {
@@ -2282,7 +2299,7 @@ function removeAttr(
2282
2299
}
2283
2300
} ;
2284
2301
const mutationHandler = mutations => {
2285
- if ( timer !== undefined ) { return ; }
2302
+ if ( timerId !== undefined ) { return ; }
2286
2303
let skip = true ;
2287
2304
for ( let i = 0 ; i < mutations . length && skip ; i ++ ) {
2288
2305
const { type, addedNodes, removedNodes } = mutations [ i ] ;
@@ -2295,7 +2312,7 @@ function removeAttr(
2295
2312
}
2296
2313
}
2297
2314
if ( skip ) { return ; }
2298
- timer = safe . onIdle ( rmattr , { timeout : 67 } ) ;
2315
+ asap ? rmattr ( ) : rmattrAsync ( ) ;
2299
2316
} ;
2300
2317
const start = ( ) => {
2301
2318
rmattr ( ) ;
@@ -2308,9 +2325,7 @@ function removeAttr(
2308
2325
subtree : true ,
2309
2326
} ) ;
2310
2327
} ;
2311
- runAt ( ( ) => {
2312
- start ( ) ;
2313
- } , / \b c o m p l e t e \b / . test ( behavior ) ? 'idle' : 'interactive' ) ;
2328
+ runAt ( ( ) => { start ( ) ; } , behavior . split ( / \s + / ) ) ;
2314
2329
}
2315
2330
2316
2331
/******************************************************************************/
0 commit comments