@@ -1929,66 +1929,75 @@ builtinScriptlets.push({
1929
1929
] ,
1930
1930
fn : noXhrIf ,
1931
1931
dependencies : [
1932
- 'safe-self.fn' ,
1932
+ 'match-object-properties.fn' ,
1933
+ 'parse-properties-to-match.fn' ,
1933
1934
] ,
1934
1935
} ) ;
1935
1936
function noXhrIf (
1936
- arg1 = ''
1937
+ propsToMatch = '' ,
1938
+ randomize = ''
1937
1939
) {
1938
- if ( typeof arg1 !== 'string' ) { return ; }
1939
- const safe = safeSelf ( ) ;
1940
+ if ( typeof propsToMatch !== 'string' ) { return ; }
1940
1941
const xhrInstances = new WeakMap ( ) ;
1941
- const needles = [ ] ;
1942
- for ( const condition of arg1 . split ( / \s + / ) ) {
1943
- if ( condition === '' ) { continue ; }
1944
- const pos = condition . indexOf ( ':' ) ;
1945
- let key , value ;
1946
- if ( pos !== - 1 ) {
1947
- key = condition . slice ( 0 , pos ) ;
1948
- value = condition . slice ( pos + 1 ) ;
1949
- } else {
1950
- key = 'url' ;
1951
- value = condition ;
1952
- }
1953
- needles . push ( { key, re : safe . patternToRegex ( value ) } ) ;
1954
- }
1955
- const log = needles . length === 0 ? console . log . bind ( console ) : undefined ;
1942
+ const propNeedles = parsePropertiesToMatch ( propsToMatch , 'url' ) ;
1943
+ const log = propNeedles . size === 0 ? console . log . bind ( console ) : undefined ;
1956
1944
self . XMLHttpRequest = class extends self . XMLHttpRequest {
1957
- open ( ...args ) {
1945
+ open ( method , url , ...args ) {
1958
1946
if ( log !== undefined ) {
1959
- log ( `uBO: xhr.open(${ args . join ( ', ' ) } )` ) ;
1947
+ log ( `uBO: xhr.open(${ method } , ${ url } , ${ args . join ( ', ' ) } )` ) ;
1960
1948
} else {
1961
- const argNames = [ 'method' , 'url' ] ;
1962
- const haystack = new Map ( ) ;
1963
- for ( let i = 0 ; i < args . length && i < argNames . length ; i ++ ) {
1964
- haystack . set ( argNames [ i ] , args [ i ] ) ;
1965
- }
1966
- if ( haystack . size !== 0 ) {
1967
- let matches = true ;
1968
- for ( const { key, re } of needles ) {
1969
- matches = re . test ( haystack . get ( key ) || '' ) ;
1970
- if ( matches === false ) { break ; }
1971
- }
1972
- if ( matches ) {
1973
- xhrInstances . set ( this , haystack ) ;
1974
- }
1949
+ const haystack = { method, url } ;
1950
+ if ( matchObjectProperties ( propNeedles , haystack ) ) {
1951
+ xhrInstances . set ( this , haystack ) ;
1975
1952
}
1976
1953
}
1977
- return super . open ( ...args ) ;
1954
+ return super . open ( method , url , ...args ) ;
1978
1955
}
1979
1956
send ( ...args ) {
1980
1957
const haystack = xhrInstances . get ( this ) ;
1981
1958
if ( haystack === undefined ) {
1982
1959
return super . send ( ...args ) ;
1983
1960
}
1984
1961
Object . defineProperties ( this , {
1985
- readyState : { value : 4 , writable : false } ,
1986
- response : { value : '' , writable : false } ,
1987
- responseText : { value : '' , writable : false } ,
1988
- responseURL : { value : haystack . get ( 'url' ) , writable : false } ,
1989
- responseXML : { value : '' , writable : false } ,
1990
- status : { value : 200 , writable : false } ,
1991
- statusText : { value : 'OK' , writable : false } ,
1962
+ readyState : { value : 4 } ,
1963
+ responseURL : { value : haystack . url } ,
1964
+ status : { value : 200 } ,
1965
+ statusText : { value : 'OK' } ,
1966
+ } ) ;
1967
+ let response = '' ;
1968
+ let responseText = '' ;
1969
+ let responseXML = null ;
1970
+ switch ( this . responseType ) {
1971
+ case 'arraybuffer' :
1972
+ response = new ArrayBuffer ( 0 ) ;
1973
+ break ;
1974
+ case 'blob' :
1975
+ response = new Blob ( [ ] ) ;
1976
+ break ;
1977
+ case 'document' : {
1978
+ const parser = new DOMParser ( ) ;
1979
+ const doc = parser . parseFromString ( '' , 'text/html' ) ;
1980
+ response = doc ;
1981
+ responseXML = doc ;
1982
+ break ;
1983
+ }
1984
+ case 'json' :
1985
+ response = { } ;
1986
+ responseText = '{}' ;
1987
+ break ;
1988
+ default :
1989
+ if ( randomize !== 'true' ) { break ; }
1990
+ do {
1991
+ response += Math . random ( ) . toString ( 36 ) . slice ( - 2 ) ;
1992
+ } while ( response . length < 10 ) ;
1993
+ response = response . slice ( - 10 ) ;
1994
+ responseText = response ;
1995
+ break ;
1996
+ }
1997
+ Object . defineProperties ( this , {
1998
+ response : { value : response } ,
1999
+ responseText : { value : responseText } ,
2000
+ responseXML : { value : responseXML } ,
1992
2001
} ) ;
1993
2002
this . dispatchEvent ( new Event ( 'readystatechange' ) ) ;
1994
2003
this . dispatchEvent ( new Event ( 'load' ) ) ;
0 commit comments