@@ -1258,6 +1258,10 @@ function noFetchIf(
1258
1258
builtinScriptlets . push ( {
1259
1259
name : 'refresh-defuser.js' ,
1260
1260
fn : refreshDefuser ,
1261
+ world : 'ISOLATED' ,
1262
+ dependencies : [
1263
+ 'run-at.fn' ,
1264
+ ] ,
1261
1265
} ) ;
1262
1266
// https://www.reddit.com/r/uBlockOrigin/comments/q0frv0/while_reading_a_sports_article_i_was_redirected/hf7wo9v/
1263
1267
function refreshDefuser (
@@ -1273,11 +1277,9 @@ function refreshDefuser(
1273
1277
const ms = Math . max ( parseFloat ( s ) || 0 , 0 ) * 1000 ;
1274
1278
setTimeout ( ( ) => { window . stop ( ) ; } , ms ) ;
1275
1279
} ;
1276
- if ( document . readyState === 'loading' ) {
1277
- document . addEventListener ( 'DOMContentLoaded' , defuse , { once : true } ) ;
1278
- } else {
1280
+ runAt ( ( ) => {
1279
1281
defuse ( ) ;
1280
- }
1282
+ } , 'interactive' ) ;
1281
1283
}
1282
1284
1283
1285
/******************************************************************************/
@@ -1286,6 +1288,9 @@ builtinScriptlets.push({
1286
1288
name : 'remove-attr.js' ,
1287
1289
aliases : [ 'ra.js' ] ,
1288
1290
fn : removeAttr ,
1291
+ dependencies : [
1292
+ 'run-at.fn' ,
1293
+ ] ,
1289
1294
} ) ;
1290
1295
function removeAttr (
1291
1296
token = '' ,
@@ -1338,13 +1343,9 @@ function removeAttr(
1338
1343
subtree : true ,
1339
1344
} ) ;
1340
1345
} ;
1341
- if ( document . readyState !== 'complete' && / \b c o m p l e t e \b / . test ( behavior ) ) {
1342
- self . addEventListener ( 'load' , start , { once : true } ) ;
1343
- } else if ( document . readyState !== 'loading' || / \b a s a p \b / . test ( behavior ) ) {
1346
+ runAt ( ( ) => {
1344
1347
start ( ) ;
1345
- } else {
1346
- self . addEventListener ( 'DOMContentLoaded' , start , { once : true } ) ;
1347
- }
1348
+ } , / \b c o m p l e t e \b / . test ( behavior ) ? 'idle' : 'interactive' ) ;
1348
1349
}
1349
1350
1350
1351
/******************************************************************************/
@@ -1353,6 +1354,9 @@ builtinScriptlets.push({
1353
1354
name : 'remove-class.js' ,
1354
1355
aliases : [ 'rc.js' ] ,
1355
1356
fn : removeClass ,
1357
+ dependencies : [
1358
+ 'run-at.fn' ,
1359
+ ] ,
1356
1360
} ) ;
1357
1361
function removeClass (
1358
1362
token = '' ,
@@ -1403,13 +1407,9 @@ function removeClass(
1403
1407
subtree : true ,
1404
1408
} ) ;
1405
1409
} ;
1406
- if ( document . readyState !== 'complete' && / \b c o m p l e t e \b / . test ( behavior ) ) {
1407
- self . addEventListener ( 'load' , start , { once : true } ) ;
1408
- } else if ( document . readyState === 'loading' ) {
1409
- self . addEventListener ( 'DOMContentLoaded' , start , { once : true } ) ;
1410
- } else {
1410
+ runAt ( ( ) => {
1411
1411
start ( ) ;
1412
- }
1412
+ } , / \b c o m p l e t e \b / . test ( behavior ) ? 'idle' : 'interactive' ) ;
1413
1413
}
1414
1414
1415
1415
/******************************************************************************/
@@ -2550,6 +2550,52 @@ function removeNodeText(
2550
2550
replaceNodeTextCore ( nodeName , '' , '' , 'condition' , condition || '' , ...extraArgs ) ;
2551
2551
}
2552
2552
2553
+ /*******************************************************************************
2554
+ *
2555
+ * set-cookie.js
2556
+ *
2557
+ * Set specified cookie to a specific value.
2558
+ *
2559
+ * Reference:
2560
+ * https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js
2561
+ *
2562
+ **/
2563
+
2564
+ builtinScriptlets . push ( {
2565
+ name : 'set-cookie.js' ,
2566
+ fn : setCookie ,
2567
+ world : 'ISOLATED' ,
2568
+ } ) ;
2569
+ function setCookie (
2570
+ name = '' ,
2571
+ value = '' ,
2572
+ path = '/'
2573
+ ) {
2574
+ if ( name === '' ) { return ; }
2575
+ const validValues = new Set ( [
2576
+ 'true' , 'True' ,
2577
+ 'false' , 'False' ,
2578
+ 'yes' , 'Yes' , 'y' , 'Y' ,
2579
+ 'no' , 'No' , 'n' , 'N' ,
2580
+ 'ok' , 'OK' ,
2581
+ ] ) ;
2582
+ if ( validValues . has ( value ) === false ) {
2583
+ if ( / ^ \d + $ / . test ( value ) === false ) { return ; }
2584
+ const n = parseInt ( value , 10 ) ;
2585
+ if ( n > 15 ) { return ; }
2586
+ }
2587
+ const validPaths = [ '/' , 'none' ] ;
2588
+ if ( validPaths . includes ( path ) === false ) { return ; }
2589
+ const cookieParts = [
2590
+ encodeURIComponent ( name ) , '=' ,
2591
+ encodeURIComponent ( value ) ,
2592
+ ] ;
2593
+ if ( path !== 'none' ) {
2594
+ cookieParts . push ( '; path=/' ) ;
2595
+ }
2596
+ document . cookie = cookieParts . join ( '' ) ;
2597
+ }
2598
+
2553
2599
/*******************************************************************************
2554
2600
*
2555
2601
* Scriplets below this section are only available for filter lists from
@@ -2631,48 +2677,51 @@ function trustedSetConstant(
2631
2677
2632
2678
/*******************************************************************************
2633
2679
*
2634
- * set-cookie.js
2680
+ * trusted- set-cookie.js
2635
2681
*
2636
- * Set specified cookie to a specific value.
2682
+ * Set specified cookie to an arbitrary value.
2637
2683
*
2638
2684
* Reference:
2639
- * https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js
2685
+ * https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/trusted- set-cookie.js#L23
2640
2686
*
2641
2687
**/
2642
2688
2643
2689
builtinScriptlets . push ( {
2644
- name : 'set-cookie.js' ,
2690
+ name : 'trusted- set-cookie.js' ,
2645
2691
requiresTrust : true ,
2646
- fn : setCookie ,
2692
+ fn : trustedSetCookie ,
2647
2693
world : 'ISOLATED' ,
2648
2694
} ) ;
2649
- function setCookie (
2695
+ function trustedSetCookie (
2650
2696
name = '' ,
2651
2697
value = '' ,
2698
+ offsetExpiresSec = '' ,
2652
2699
path = '/'
2653
2700
) {
2654
2701
if ( name === '' ) { return ; }
2655
- const validValues = new Set ( [
2656
- 'true' , 'True' ,
2657
- 'false' , 'False' ,
2658
- 'yes' , 'Yes' , 'y' , 'Y' ,
2659
- 'no' , 'No' , 'n' , 'N' ,
2660
- 'ok' , 'OK' ,
2661
- ] ) ;
2662
- if ( validValues . has ( value ) === false ) {
2663
- if ( / ^ \d + $ / . test ( value ) === false ) { return ; }
2664
- const n = parseInt ( value , 10 ) ;
2665
- if ( n < 0 || n > 15 ) { return ; }
2702
+ const time = new Date ( ) ;
2703
+ if ( value === '$now$' ) {
2704
+ value = Date . now ( ) ;
2705
+ } else if ( value === '$currentDate$' ) {
2706
+ value = time . toUTCString ( ) ;
2707
+ }
2708
+ const validPaths = [ '/' , 'none' ] ;
2709
+ if ( validPaths . includes ( path ) === false ) { return ; }
2710
+ const cookieParts = [ name , '=' , value ] ;
2711
+ if ( offsetExpiresSec !== '' ) {
2712
+ if ( offsetExpiresSec === '1day' ) {
2713
+ time . setDate ( time . getDate ( ) + 1 ) ;
2714
+ } else if ( offsetExpiresSec === '1year' ) {
2715
+ time . setFullYear ( time . getFullYear ( ) + 1 ) ;
2716
+ } else {
2717
+ if ( / ^ \d + $ / . test ( offsetExpiresSec ) === false ) { return ; }
2718
+ time . setSeconds ( time . getSeconds ( ) + parseInt ( offsetExpiresSec , 10 ) ) ;
2719
+ }
2720
+ cookieParts . push ( '; expires=' , time . toUTCString ( ) ) ;
2666
2721
}
2667
- const validPaths = new Set ( [ '/' , 'none' ] ) ;
2668
- if ( validPaths . has ( path ) === false ) { return ; }
2669
- const cookieParts = [
2670
- encodeURIComponent ( name ) , '=' ,
2671
- encodeURIComponent ( value ) ,
2672
- ] ;
2673
2722
if ( path !== 'none' ) {
2674
2723
cookieParts . push ( '; path=/' ) ;
2675
- }
2724
+ }
2676
2725
document . cookie = cookieParts . join ( '' ) ;
2677
2726
}
2678
2727
0 commit comments