@@ -1446,9 +1446,6 @@ function replaceFetchResponseFn(
1446
1446
builtinScriptlets . push ( {
1447
1447
name : 'proxy-apply.fn' ,
1448
1448
fn : proxyApplyFn ,
1449
- dependencies : [
1450
- 'safe-self.fn' ,
1451
- ] ,
1452
1449
} ) ;
1453
1450
function proxyApplyFn (
1454
1451
target = '' ,
@@ -1465,12 +1462,28 @@ function proxyApplyFn(
1465
1462
}
1466
1463
const fn = context [ prop ] ;
1467
1464
if ( typeof fn !== 'function' ) { return ; }
1465
+ const fnname = fn . name ;
1466
+ const toString = ( function toString ( ) {
1467
+ return `function ${ fnname } () { [native code] }` ;
1468
+ } ) . bind ( null ) ;
1468
1469
if ( fn . prototype && fn . prototype . constructor === fn ) {
1469
- context [ prop ] = new Proxy ( fn , { construct : handler } ) ;
1470
- return ( ...args ) => { return Reflect . construct ( ...args ) ; } ;
1470
+ context [ prop ] = new Proxy ( fn , {
1471
+ construct : handler ,
1472
+ get ( target , prop , receiver ) {
1473
+ if ( prop === 'toString' ) { return toString ; }
1474
+ return Reflect . get ( target , prop , receiver ) ;
1475
+ } ,
1476
+ } ) ;
1477
+ return ( ...args ) => Reflect . construct ( ...args ) ;
1471
1478
}
1472
- context [ prop ] = new Proxy ( fn , { apply : handler } ) ;
1473
- return ( ...args ) => { return Reflect . apply ( ...args ) ; } ;
1479
+ context [ prop ] = new Proxy ( fn , {
1480
+ apply : handler ,
1481
+ get ( target , prop , receiver ) {
1482
+ if ( prop === 'toString' ) { return toString ; }
1483
+ return Reflect . get ( target , prop , receiver ) ;
1484
+ } ,
1485
+ } ) ;
1486
+ return ( ...args ) => Reflect . apply ( ...args ) ;
1474
1487
}
1475
1488
1476
1489
/*******************************************************************************
@@ -1676,6 +1689,7 @@ builtinScriptlets.push({
1676
1689
] ,
1677
1690
fn : addEventListenerDefuser ,
1678
1691
dependencies : [
1692
+ 'proxy-apply.fn' ,
1679
1693
'run-at.fn' ,
1680
1694
'safe-self.fn' ,
1681
1695
'should-debug.fn' ,
@@ -1732,44 +1746,29 @@ function addEventListenerDefuser(
1732
1746
}
1733
1747
return matchesBoth ;
1734
1748
} ;
1735
- const trapEddEventListeners = ( ) => {
1736
- const eventListenerHandler = {
1737
- apply : function ( target , thisArg , args ) {
1738
- let t , h ;
1739
- try {
1740
- t = String ( args [ 0 ] ) ;
1741
- if ( typeof args [ 1 ] === 'function' ) {
1742
- h = String ( safe . Function_toString ( args [ 1 ] ) ) ;
1743
- } else if ( typeof args [ 1 ] === 'object' && args [ 1 ] !== null ) {
1744
- if ( typeof args [ 1 ] . handleEvent === 'function' ) {
1745
- h = String ( safe . Function_toString ( args [ 1 ] . handleEvent ) ) ;
1746
- }
1747
- } else {
1748
- h = String ( args [ 1 ] ) ;
1749
+ runAt ( ( ) => {
1750
+ proxyApplyFn ( 'EventTarget.prototype.addEventListener' , function ( target , thisArg , args ) {
1751
+ let t , h ;
1752
+ try {
1753
+ t = String ( args [ 0 ] ) ;
1754
+ if ( typeof args [ 1 ] === 'function' ) {
1755
+ h = String ( safe . Function_toString ( args [ 1 ] ) ) ;
1756
+ } else if ( typeof args [ 1 ] === 'object' && args [ 1 ] !== null ) {
1757
+ if ( typeof args [ 1 ] . handleEvent === 'function' ) {
1758
+ h = String ( safe . Function_toString ( args [ 1 ] . handleEvent ) ) ;
1749
1759
}
1750
- } catch ( ex ) {
1751
- }
1752
- if ( type === '' && pattern === '' ) {
1753
- safe . uboLog ( logPrefix , `Called: ${ t } \n${ h } \n${ elementDetails ( thisArg ) } ` ) ;
1754
- } else if ( shouldPrevent ( thisArg , t , h ) ) {
1755
- return safe . uboLog ( logPrefix , `Prevented: ${ t } \n${ h } \n${ elementDetails ( thisArg ) } ` ) ;
1756
- }
1757
- return Reflect . apply ( target , thisArg , args ) ;
1758
- } ,
1759
- get ( target , prop , receiver ) {
1760
- if ( prop === 'toString' ) {
1761
- return target . toString . bind ( target ) ;
1760
+ } else {
1761
+ h = String ( args [ 1 ] ) ;
1762
1762
}
1763
- return Reflect . get ( target , prop , receiver ) ;
1764
- } ,
1765
- } ;
1766
- self . EventTarget . prototype . addEventListener = new Proxy (
1767
- self . EventTarget . prototype . addEventListener ,
1768
- eventListenerHandler
1769
- ) ;
1770
- } ;
1771
- runAt ( ( ) => {
1772
- trapEddEventListeners ( ) ;
1763
+ } catch ( ex ) {
1764
+ }
1765
+ if ( type === '' && pattern === '' ) {
1766
+ safe . uboLog ( logPrefix , `Called: ${ t } \n${ h } \n${ elementDetails ( thisArg ) } ` ) ;
1767
+ } else if ( shouldPrevent ( thisArg , t , h ) ) {
1768
+ return safe . uboLog ( logPrefix , `Prevented: ${ t } \n${ h } \n${ elementDetails ( thisArg ) } ` ) ;
1769
+ }
1770
+ return Reflect . apply ( target , thisArg , args ) ;
1771
+ } ) ;
1773
1772
} , extraArgs . runAt ) ;
1774
1773
}
1775
1774
@@ -2475,6 +2474,7 @@ builtinScriptlets.push({
2475
2474
] ,
2476
2475
fn : noSetIntervalIf ,
2477
2476
dependencies : [
2477
+ 'proxy-apply.fn' ,
2478
2478
'safe-self.fn' ,
2479
2479
] ,
2480
2480
} ) ;
@@ -2495,35 +2495,27 @@ function noSetIntervalIf(
2495
2495
delay = parseInt ( delay , 10 ) ;
2496
2496
}
2497
2497
const reNeedle = safe . patternToRegex ( needle ) ;
2498
- self . setInterval = new Proxy ( self . setInterval , {
2499
- apply : function ( target , thisArg , args ) {
2500
- const a = args [ 0 ] instanceof Function
2501
- ? String ( safe . Function_toString ( args [ 0 ] ) )
2502
- : String ( args [ 0 ] ) ;
2503
- const b = args [ 1 ] ;
2504
- if ( needle === '' && delay === undefined ) {
2505
- safe . uboLog ( logPrefix , `Called:\n${ a } \n${ b } ` ) ;
2506
- return Reflect . apply ( target , thisArg , args ) ;
2507
- }
2508
- let defuse ;
2509
- if ( needle !== '' ) {
2510
- defuse = reNeedle . test ( a ) !== needleNot ;
2511
- }
2512
- if ( defuse !== false && delay !== undefined ) {
2513
- defuse = ( b === delay || isNaN ( b ) && isNaN ( delay ) ) !== delayNot ;
2514
- }
2515
- if ( defuse ) {
2516
- args [ 0 ] = function ( ) { } ;
2517
- safe . uboLog ( logPrefix , `Prevented:\n${ a } \n${ b } ` ) ;
2518
- }
2498
+ proxyApplyFn ( 'setInterval' , function setInterval ( target , thisArg , args ) {
2499
+ const a = args [ 0 ] instanceof Function
2500
+ ? String ( safe . Function_toString ( args [ 0 ] ) )
2501
+ : String ( args [ 0 ] ) ;
2502
+ const b = args [ 1 ] ;
2503
+ if ( needle === '' && delay === undefined ) {
2504
+ safe . uboLog ( logPrefix , `Called:\n${ a } \n${ b } ` ) ;
2519
2505
return Reflect . apply ( target , thisArg , args ) ;
2520
- } ,
2521
- get ( target , prop , receiver ) {
2522
- if ( prop === 'toString' ) {
2523
- return target . toString . bind ( target ) ;
2524
- }
2525
- return Reflect . get ( target , prop , receiver ) ;
2526
- } ,
2506
+ }
2507
+ let defuse ;
2508
+ if ( needle !== '' ) {
2509
+ defuse = reNeedle . test ( a ) !== needleNot ;
2510
+ }
2511
+ if ( defuse !== false && delay !== undefined ) {
2512
+ defuse = ( b === delay || isNaN ( b ) && isNaN ( delay ) ) !== delayNot ;
2513
+ }
2514
+ if ( defuse ) {
2515
+ args [ 0 ] = function ( ) { } ;
2516
+ safe . uboLog ( logPrefix , `Prevented:\n${ a } \n${ b } ` ) ;
2517
+ }
2518
+ return Reflect . apply ( target , thisArg , args ) ;
2527
2519
} ) ;
2528
2520
}
2529
2521
@@ -2538,6 +2530,7 @@ builtinScriptlets.push({
2538
2530
] ,
2539
2531
fn : noSetTimeoutIf ,
2540
2532
dependencies : [
2533
+ 'proxy-apply.fn' ,
2541
2534
'safe-self.fn' ,
2542
2535
] ,
2543
2536
} ) ;
@@ -2558,35 +2551,27 @@ function noSetTimeoutIf(
2558
2551
delay = parseInt ( delay , 10 ) ;
2559
2552
}
2560
2553
const reNeedle = safe . patternToRegex ( needle ) ;
2561
- self . setTimeout = new Proxy ( self . setTimeout , {
2562
- apply : function ( target , thisArg , args ) {
2563
- const a = args [ 0 ] instanceof Function
2564
- ? String ( safe . Function_toString ( args [ 0 ] ) )
2565
- : String ( args [ 0 ] ) ;
2566
- const b = args [ 1 ] ;
2567
- if ( needle === '' && delay === undefined ) {
2568
- safe . uboLog ( logPrefix , `Called:\n${ a } \n${ b } ` ) ;
2569
- return Reflect . apply ( target , thisArg , args ) ;
2570
- }
2571
- let defuse ;
2572
- if ( needle !== '' ) {
2573
- defuse = reNeedle . test ( a ) !== needleNot ;
2574
- }
2575
- if ( defuse !== false && delay !== undefined ) {
2576
- defuse = ( b === delay || isNaN ( b ) && isNaN ( delay ) ) !== delayNot ;
2577
- }
2578
- if ( defuse ) {
2579
- args [ 0 ] = function ( ) { } ;
2580
- safe . uboLog ( logPrefix , `Prevented:\n${ a } \n${ b } ` ) ;
2581
- }
2554
+ proxyApplyFn ( 'setTimeout' , function setTimeout ( target , thisArg , args ) {
2555
+ const a = args [ 0 ] instanceof Function
2556
+ ? String ( safe . Function_toString ( args [ 0 ] ) )
2557
+ : String ( args [ 0 ] ) ;
2558
+ const b = args [ 1 ] ;
2559
+ if ( needle === '' && delay === undefined ) {
2560
+ safe . uboLog ( logPrefix , `Called:\n${ a } \n${ b } ` ) ;
2582
2561
return Reflect . apply ( target , thisArg , args ) ;
2583
- } ,
2584
- get ( target , prop , receiver ) {
2585
- if ( prop === 'toString' ) {
2586
- return target . toString . bind ( target ) ;
2587
- }
2588
- return Reflect . get ( target , prop , receiver ) ;
2589
- } ,
2562
+ }
2563
+ let defuse ;
2564
+ if ( needle !== '' ) {
2565
+ defuse = reNeedle . test ( a ) !== needleNot ;
2566
+ }
2567
+ if ( defuse !== false && delay !== undefined ) {
2568
+ defuse = ( b === delay || isNaN ( b ) && isNaN ( delay ) ) !== delayNot ;
2569
+ }
2570
+ if ( defuse ) {
2571
+ args [ 0 ] = function ( ) { } ;
2572
+ safe . uboLog ( logPrefix , `Prevented:\n${ a } \n${ b } ` ) ;
2573
+ }
2574
+ return Reflect . apply ( target , thisArg , args ) ;
2590
2575
} ) ;
2591
2576
}
2592
2577
@@ -2815,6 +2800,7 @@ builtinScriptlets.push({
2815
2800
] ,
2816
2801
fn : noWindowOpenIf ,
2817
2802
dependencies : [
2803
+ 'proxy-apply.fn' ,
2818
2804
'safe-self.fn' ,
2819
2805
] ,
2820
2806
} ) ;
@@ -2845,51 +2831,49 @@ function noWindowOpenIf(
2845
2831
setTimeout ( ( ) => { decoyElem . remove ( ) ; } , autoRemoveAfter * 1000 ) ;
2846
2832
return decoyElem ;
2847
2833
} ;
2848
- window . open = new Proxy ( window . open , {
2849
- apply : function ( target , thisArg , args ) {
2850
- const haystack = args . join ( ' ' ) ;
2851
- if ( rePattern . test ( haystack ) !== targetMatchResult ) {
2852
- if ( safe . logLevel > 1 ) {
2853
- safe . uboLog ( logPrefix , `Allowed (${ args . join ( ', ' ) } )` ) ;
2854
- }
2855
- return Reflect . apply ( target , thisArg , args ) ;
2856
- }
2857
- safe . uboLog ( logPrefix , `Prevented (${ args . join ( ', ' ) } )` ) ;
2858
- if ( autoRemoveAfter < 0 ) { return null ; }
2859
- const decoyElem = decoy === 'obj'
2860
- ? createDecoy ( 'object' , 'data' , ...args )
2861
- : createDecoy ( 'iframe' , 'src' , ...args ) ;
2862
- let popup = decoyElem . contentWindow ;
2863
- if ( typeof popup === 'object' && popup !== null ) {
2864
- Object . defineProperty ( popup , 'closed' , { value : false } ) ;
2865
- } else {
2866
- const noopFunc = ( function ( ) { } ) . bind ( self ) ;
2867
- popup = new Proxy ( self , {
2868
- get : function ( target , prop ) {
2869
- if ( prop === 'closed' ) { return false ; }
2870
- const r = Reflect . get ( ...arguments ) ;
2871
- if ( typeof r === 'function' ) { return noopFunc ; }
2872
- return target [ prop ] ;
2873
- } ,
2874
- set : function ( ) {
2875
- return Reflect . set ( ...arguments ) ;
2876
- } ,
2877
- } ) ;
2878
- }
2879
- if ( safe . logLevel !== 0 ) {
2880
- popup = new Proxy ( popup , {
2881
- get : function ( target , prop ) {
2882
- safe . uboLog ( logPrefix , 'window.open / get' , prop , '===' , target [ prop ] ) ;
2883
- return Reflect . get ( ...arguments ) ;
2884
- } ,
2885
- set : function ( target , prop , value ) {
2886
- safe . uboLog ( logPrefix , 'window.open / set' , prop , '=' , value ) ;
2887
- return Reflect . set ( ...arguments ) ;
2888
- } ,
2889
- } ) ;
2834
+ proxyApplyFn ( 'open' , function open ( target , thisArg , args ) {
2835
+ const haystack = args . join ( ' ' ) ;
2836
+ if ( rePattern . test ( haystack ) !== targetMatchResult ) {
2837
+ if ( safe . logLevel > 1 ) {
2838
+ safe . uboLog ( logPrefix , `Allowed (${ args . join ( ', ' ) } )` ) ;
2890
2839
}
2891
- return popup ;
2840
+ return Reflect . apply ( target , thisArg , args ) ;
2841
+ }
2842
+ safe . uboLog ( logPrefix , `Prevented (${ args . join ( ', ' ) } )` ) ;
2843
+ if ( autoRemoveAfter < 0 ) { return null ; }
2844
+ const decoyElem = decoy === 'obj'
2845
+ ? createDecoy ( 'object' , 'data' , ...args )
2846
+ : createDecoy ( 'iframe' , 'src' , ...args ) ;
2847
+ let popup = decoyElem . contentWindow ;
2848
+ if ( typeof popup === 'object' && popup !== null ) {
2849
+ Object . defineProperty ( popup , 'closed' , { value : false } ) ;
2850
+ } else {
2851
+ const noopFunc = function open ( ) { } ;
2852
+ popup = new Proxy ( self , {
2853
+ get : function ( target , prop ) {
2854
+ if ( prop === 'closed' ) { return false ; }
2855
+ const r = Reflect . get ( ...arguments ) ;
2856
+ if ( typeof r === 'function' ) { return noopFunc ; }
2857
+ return target [ prop ] ;
2858
+ } ,
2859
+ set : function ( ) {
2860
+ return Reflect . set ( ...arguments ) ;
2861
+ } ,
2862
+ } ) ;
2863
+ }
2864
+ if ( safe . logLevel !== 0 ) {
2865
+ popup = new Proxy ( popup , {
2866
+ get : function ( target , prop ) {
2867
+ safe . uboLog ( logPrefix , 'window.open / get' , prop , '===' , target [ prop ] ) ;
2868
+ return Reflect . get ( ...arguments ) ;
2869
+ } ,
2870
+ set : function ( target , prop , value ) {
2871
+ safe . uboLog ( logPrefix , 'window.open / set' , prop , '=' , value ) ;
2872
+ return Reflect . set ( ...arguments ) ;
2873
+ } ,
2874
+ } ) ;
2892
2875
}
2876
+ return popup ;
2893
2877
} ) ;
2894
2878
}
2895
2879
0 commit comments