@@ -49,6 +49,12 @@ function safeSelf() {
49
49
'RegExp' : self . RegExp ,
50
50
'RegExp_test' : self . RegExp . prototype . test ,
51
51
'RegExp_exec' : self . RegExp . prototype . exec ,
52
+ 'safeLog' : console . log . bind ( console ) ,
53
+ 'uboLog' : function ( msg ) {
54
+ if ( msg !== '' ) {
55
+ this . safeLog ( `[uBO] ${ msg } ` ) ;
56
+ }
57
+ } ,
52
58
} ;
53
59
scriptletGlobals . set ( 'safeSelf' , safe ) ;
54
60
return safe ;
@@ -90,6 +96,28 @@ function getExceptionToken() {
90
96
return token ;
91
97
}
92
98
99
+ /******************************************************************************/
100
+
101
+ builtinScriptlets . push ( {
102
+ name : 'should-debug.fn' ,
103
+ fn : shouldDebug ,
104
+ } ) ;
105
+ function shouldDebug ( details ) {
106
+ if ( details instanceof Object === false ) { return false ; }
107
+ return scriptletGlobals . has ( 'canDebug' ) && details . debug ;
108
+ }
109
+
110
+ /******************************************************************************/
111
+
112
+ builtinScriptlets . push ( {
113
+ name : 'should-log.fn' ,
114
+ fn : shouldLog ,
115
+ } ) ;
116
+ function shouldLog ( details ) {
117
+ if ( details instanceof Object === false ) { return false ; }
118
+ return scriptletGlobals . has ( 'canDebug' ) && details . log ;
119
+ }
120
+
93
121
/*******************************************************************************
94
122
95
123
Injectable scriptlets
@@ -105,6 +133,9 @@ builtinScriptlets.push({
105
133
dependencies : [
106
134
'pattern-to-regex.fn' ,
107
135
'get-exception-token.fn' ,
136
+ 'safe-self.fn' ,
137
+ 'should-debug.fn' ,
138
+ 'should-log.fn' ,
108
139
] ,
109
140
} ) ;
110
141
// Issues to mind before changing anything:
@@ -117,9 +148,10 @@ function abortCurrentScript(
117
148
const details = typeof arg1 !== 'object'
118
149
? { target : arg1 , needle : arg2 , context : arg3 }
119
150
: arg1 ;
120
- const { target, needle, context } = details ;
151
+ const { target = '' , needle = '' , context = '' } = details ;
121
152
if ( typeof target !== 'string' ) { return ; }
122
153
if ( target === '' ) { return ; }
154
+ const safe = safeSelf ( ) ;
123
155
const reNeedle = patternToRegex ( needle ) ;
124
156
const reContext = patternToRegex ( context ) ;
125
157
const thisScript = document . currentScript ;
@@ -141,6 +173,8 @@ function abortCurrentScript(
141
173
value = owner [ prop ] ;
142
174
desc = undefined ;
143
175
}
176
+ const log = shouldLog ( details ) ;
177
+ const debug = shouldDebug ( details ) ;
144
178
const exceptionToken = getExceptionToken ( ) ;
145
179
const scriptTexts = new WeakMap ( ) ;
146
180
const getScriptText = elem => {
@@ -165,29 +199,38 @@ function abortCurrentScript(
165
199
return text ;
166
200
} ;
167
201
const validate = ( ) => {
202
+ if ( debug ) { debugger ; } // jshint ignore: line
168
203
const e = document . currentScript ;
169
204
if ( e instanceof HTMLScriptElement === false ) { return ; }
170
205
if ( e === thisScript ) { return ; }
206
+ if ( e . src !== '' && log ) { safe . uboLog ( `src: ${ e . src } ` ) ; }
171
207
if ( reContext . test ( e . src ) === false ) { return ; }
172
- if ( reNeedle . test ( getScriptText ( e ) ) === false ) { return ; }
208
+ const scriptText = getScriptText ( e ) ;
209
+ if ( log ) { safe . uboLog ( `script text: ${ scriptText } ` ) ; }
210
+ if ( reNeedle . test ( scriptText ) === false ) { return ; }
173
211
throw new ReferenceError ( exceptionToken ) ;
174
212
} ;
175
- Object . defineProperty ( owner , prop , {
176
- get : function ( ) {
177
- validate ( ) ;
178
- return desc instanceof Object
179
- ? desc . get . call ( owner )
180
- : value ;
181
- } ,
182
- set : function ( a ) {
183
- validate ( ) ;
184
- if ( desc instanceof Object ) {
185
- desc . set . call ( owner , a ) ;
186
- } else {
187
- value = a ;
213
+ if ( debug ) { debugger ; } // jshint ignore: line
214
+ try {
215
+ Object . defineProperty ( owner , prop , {
216
+ get : function ( ) {
217
+ validate ( ) ;
218
+ return desc instanceof Object
219
+ ? desc . get . call ( owner )
220
+ : value ;
221
+ } ,
222
+ set : function ( a ) {
223
+ validate ( ) ;
224
+ if ( desc instanceof Object ) {
225
+ desc . set . call ( owner , a ) ;
226
+ } else {
227
+ value = a ;
228
+ }
188
229
}
189
- }
190
- } ) ;
230
+ } ) ;
231
+ } catch ( ex ) {
232
+ if ( log ) { safe . uboLog ( ex ) ; }
233
+ }
191
234
}
192
235
193
236
/******************************************************************************/
@@ -298,7 +341,6 @@ function abortOnStackTrace(
298
341
const safe = safeSelf ( ) ;
299
342
const reNeedle = patternToRegex ( needle ) ;
300
343
const exceptionToken = getExceptionToken ( ) ;
301
- const log = console . log . bind ( console ) ;
302
344
const ErrorCtor = self . Error ;
303
345
const mustAbort = function ( err ) {
304
346
let docURL = self . location . href ;
@@ -336,7 +378,7 @@ function abortOnStackTrace(
336
378
logLevel === '2' && r ||
337
379
logLevel === '3' && ! r
338
380
) {
339
- log ( stack . replace ( / \t / g, '\n' ) ) ;
381
+ safe . uboLog ( stack . replace ( / \t / g, '\n' ) ) ;
340
382
}
341
383
return r ;
342
384
} ;
@@ -392,6 +434,8 @@ builtinScriptlets.push({
392
434
dependencies : [
393
435
'pattern-to-regex.fn' ,
394
436
'safe-self.fn' ,
437
+ 'should-debug.fn' ,
438
+ 'should-log.fn' ,
395
439
] ,
396
440
} ) ;
397
441
// https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120
@@ -408,7 +452,8 @@ function addEventListenerDefuser(
408
452
const safe = safeSelf ( ) ;
409
453
const reType = patternToRegex ( type ) ;
410
454
const rePattern = patternToRegex ( pattern ) ;
411
- const logfn = console . log . bind ( console ) ;
455
+ const log = shouldLog ( details ) ;
456
+ const debug = shouldDebug ( details ) ;
412
457
const proto = self . EventTarget . prototype ;
413
458
proto . addEventListener = new Proxy ( proto . addEventListener , {
414
459
apply : function ( target , thisArg , args ) {
@@ -422,17 +467,10 @@ function addEventListenerDefuser(
422
467
const matchesHandler = safe . RegExp_test . call ( rePattern , handler ) ;
423
468
const matchesEither = matchesType || matchesHandler ;
424
469
const matchesBoth = matchesType && matchesHandler ;
425
- if (
426
- details . log === 1 && matchesBoth ||
427
- details . log === 2 && matchesEither ||
428
- details . log === 3
429
- ) {
430
- logfn ( `uBO: addEventListener('${ type } ', ${ handler } )` ) ;
470
+ if ( log === 1 && matchesBoth || log === 2 && matchesEither || log === 3 ) {
471
+ safe . uboLog ( `addEventListener('${ type } ', ${ handler } )` ) ;
431
472
}
432
- if (
433
- details . debug === 1 && matchesBoth ||
434
- details . debug === 2 && matchesEither
435
- ) {
473
+ if ( debug === 1 && matchesBoth || debug === 2 && matchesEither ) {
436
474
debugger ; // jshint ignore:line
437
475
}
438
476
if ( matchesBoth ) { return ; }
0 commit comments