2020
2121*/
2222
23+ import './abort-current-script.js' ;
2324import './attribute.js' ;
2425import './create-html.js' ;
2526import './href-sanitizer.js' ;
@@ -58,9 +59,6 @@ import { registeredScriptlets } from './base.js';
5859import { safeSelf } from './safe-self.js' ;
5960import { validateConstantFn } from './set-constant.js' ;
6061
61- // Externally added to the private namespace in which scriptlets execute.
62- /* global scriptletGlobals */
63-
6462/* eslint no-prototype-builtins: 0 */
6563
6664export const builtinScriptlets = registeredScriptlets ;
@@ -71,137 +69,6 @@ export const builtinScriptlets = registeredScriptlets;
7169
7270 These are meant to be used as dependencies to injectable scriptlets.
7371
74- *******************************************************************************/
75-
76- builtinScriptlets . push ( {
77- name : 'should-debug.fn' ,
78- fn : shouldDebug ,
79- } ) ;
80- function shouldDebug ( details ) {
81- if ( details instanceof Object === false ) { return false ; }
82- return scriptletGlobals . canDebug && details . debug ;
83- }
84-
85- /******************************************************************************/
86-
87- builtinScriptlets . push ( {
88- name : 'abort-current-script.fn' ,
89- fn : abortCurrentScriptFn ,
90- dependencies : [
91- 'get-exception-token.fn' ,
92- 'safe-self.fn' ,
93- 'should-debug.fn' ,
94- ] ,
95- } ) ;
96- // Issues to mind before changing anything:
97- // https://github.com/uBlockOrigin/uBlock-issues/issues/2154
98- function abortCurrentScriptFn (
99- target = '' ,
100- needle = '' ,
101- context = ''
102- ) {
103- if ( typeof target !== 'string' ) { return ; }
104- if ( target === '' ) { return ; }
105- const safe = safeSelf ( ) ;
106- const logPrefix = safe . makeLogPrefix ( 'abort-current-script' , target , needle , context ) ;
107- const reNeedle = safe . patternToRegex ( needle ) ;
108- const reContext = safe . patternToRegex ( context ) ;
109- const extraArgs = safe . getExtraArgs ( Array . from ( arguments ) , 3 ) ;
110- const thisScript = document . currentScript ;
111- const chain = safe . String_split . call ( target , '.' ) ;
112- let owner = window ;
113- let prop ;
114- for ( ; ; ) {
115- prop = chain . shift ( ) ;
116- if ( chain . length === 0 ) { break ; }
117- if ( prop in owner === false ) { break ; }
118- owner = owner [ prop ] ;
119- if ( owner instanceof Object === false ) { return ; }
120- }
121- let value ;
122- let desc = Object . getOwnPropertyDescriptor ( owner , prop ) ;
123- if (
124- desc instanceof Object === false ||
125- desc . get instanceof Function === false
126- ) {
127- value = owner [ prop ] ;
128- desc = undefined ;
129- }
130- const debug = shouldDebug ( extraArgs ) ;
131- const exceptionToken = getExceptionTokenFn ( ) ;
132- const scriptTexts = new WeakMap ( ) ;
133- const textContentGetter = Object . getOwnPropertyDescriptor ( Node . prototype , 'textContent' ) . get ;
134- const getScriptText = elem => {
135- let text = textContentGetter . call ( elem ) ;
136- if ( text . trim ( ) !== '' ) { return text ; }
137- if ( scriptTexts . has ( elem ) ) { return scriptTexts . get ( elem ) ; }
138- const [ , mime , content ] =
139- / ^ d a t a : ( [ ^ , ] * ) , ( .+ ) $ / . exec ( elem . src . trim ( ) ) ||
140- [ '' , '' , '' ] ;
141- try {
142- switch ( true ) {
143- case mime . endsWith ( ';base64' ) :
144- text = self . atob ( content ) ;
145- break ;
146- default :
147- text = self . decodeURIComponent ( content ) ;
148- break ;
149- }
150- } catch {
151- }
152- scriptTexts . set ( elem , text ) ;
153- return text ;
154- } ;
155- const validate = ( ) => {
156- const e = document . currentScript ;
157- if ( e instanceof HTMLScriptElement === false ) { return ; }
158- if ( e === thisScript ) { return ; }
159- if ( context !== '' && reContext . test ( e . src ) === false ) {
160- // eslint-disable-next-line no-debugger
161- if ( debug === 'nomatch' || debug === 'all' ) { debugger ; }
162- return ;
163- }
164- if ( safe . logLevel > 1 && context !== '' ) {
165- safe . uboLog ( logPrefix , `Matched src\n${ e . src } ` ) ;
166- }
167- const scriptText = getScriptText ( e ) ;
168- if ( reNeedle . test ( scriptText ) === false ) {
169- // eslint-disable-next-line no-debugger
170- if ( debug === 'nomatch' || debug === 'all' ) { debugger ; }
171- return ;
172- }
173- if ( safe . logLevel > 1 ) {
174- safe . uboLog ( logPrefix , `Matched text\n${ scriptText } ` ) ;
175- }
176- // eslint-disable-next-line no-debugger
177- if ( debug === 'match' || debug === 'all' ) { debugger ; }
178- safe . uboLog ( logPrefix , 'Aborted' ) ;
179- throw new ReferenceError ( exceptionToken ) ;
180- } ;
181- // eslint-disable-next-line no-debugger
182- if ( debug === 'install' ) { debugger ; }
183- try {
184- Object . defineProperty ( owner , prop , {
185- get : function ( ) {
186- validate ( ) ;
187- return desc instanceof Object
188- ? desc . get . call ( owner )
189- : value ;
190- } ,
191- set : function ( a ) {
192- validate ( ) ;
193- if ( desc instanceof Object ) {
194- desc . set . call ( owner , a ) ;
195- } else {
196- value = a ;
197- }
198- }
199- } ) ;
200- } catch ( ex ) {
201- safe . uboErr ( logPrefix , `Error: ${ ex } ` ) ;
202- }
203- }
204-
20572/******************************************************************************/
20673
20774builtinScriptlets . push ( {
@@ -394,29 +261,6 @@ function replaceFetchResponseFn(
394261
395262 These are meant to be used in the MAIN (webpage) execution world.
396263
397- *******************************************************************************/
398-
399- builtinScriptlets . push ( {
400- name : 'abort-current-script.js' ,
401- aliases : [
402- 'acs.js' ,
403- 'abort-current-inline-script.js' ,
404- 'acis.js' ,
405- ] ,
406- fn : abortCurrentScript ,
407- dependencies : [
408- 'abort-current-script.fn' ,
409- 'run-at-html-element.fn' ,
410- ] ,
411- } ) ;
412- // Issues to mind before changing anything:
413- // https://github.com/uBlockOrigin/uBlock-issues/issues/2154
414- function abortCurrentScript ( ...args ) {
415- runAtHtmlElementFn ( ( ) => {
416- abortCurrentScriptFn ( ...args ) ;
417- } ) ;
418- }
419-
420264/******************************************************************************/
421265
422266builtinScriptlets . push ( {
0 commit comments