@@ -2009,26 +2009,33 @@ function xmlPrune(
2009
2009
if ( typeof selector !== 'string' ) { return ; }
2010
2010
if ( selector === '' ) { return ; }
2011
2011
const reUrl = patternToRegex ( urlPattern ) ;
2012
- const pruner = text => {
2013
- if ( ( / ^ \s * < / . test ( text ) && / > \s * $ / . test ( text ) ) === false ) {
2014
- return text ;
2015
- }
2012
+ const pruneFromDoc = xmlDoc => {
2016
2013
try {
2017
- const xmlParser = new DOMParser ( ) ;
2018
- const xmlDoc = xmlParser . parseFromString ( text , 'text/xml' ) ;
2019
2014
if ( selectorCheck !== '' && xmlDoc . querySelector ( selectorCheck ) === null ) {
2020
- return text ;
2015
+ return xmlDoc ;
2021
2016
}
2022
2017
const elems = xmlDoc . querySelectorAll ( selector ) ;
2023
2018
if ( elems . length !== 0 ) {
2024
2019
for ( const elem of elems ) {
2025
2020
elem . remove ( ) ;
2026
2021
}
2027
- const serializer = new XMLSerializer ( ) ;
2028
- text = serializer . serializeToString ( xmlDoc ) ;
2029
2022
}
2030
2023
} catch ( ex ) {
2031
2024
}
2025
+ return xmlDoc ;
2026
+ } ;
2027
+ const pruneFromText = text => {
2028
+ if ( ( / ^ \s * < / . test ( text ) && / > \s * $ / . test ( text ) ) === false ) {
2029
+ return text ;
2030
+ }
2031
+ try {
2032
+ const xmlParser = new DOMParser ( ) ;
2033
+ const xmlDoc = xmlParser . parseFromString ( text , 'text/xml' ) ;
2034
+ pruneFromDoc ( xmlDoc ) ;
2035
+ const serializer = new XMLSerializer ( ) ;
2036
+ text = serializer . serializeToString ( xmlDoc ) ;
2037
+ } catch ( ex ) {
2038
+ }
2032
2039
return text ;
2033
2040
} ;
2034
2041
const urlFromArg = arg => {
@@ -2044,7 +2051,7 @@ function xmlPrune(
2044
2051
}
2045
2052
return realFetch ( ...args ) . then ( realResponse =>
2046
2053
realResponse . text ( ) . then ( text =>
2047
- new Response ( pruner ( text ) , {
2054
+ new Response ( pruneFromText ( text ) , {
2048
2055
status : realResponse . status ,
2049
2056
statusText : realResponse . statusText ,
2050
2057
headers : realResponse . headers ,
@@ -2053,6 +2060,30 @@ function xmlPrune(
2053
2060
) ;
2054
2061
}
2055
2062
} ) ;
2063
+ self . XMLHttpRequest . prototype . open = new Proxy ( self . XMLHttpRequest . prototype . open , {
2064
+ apply : async ( target , thisArg , args ) => {
2065
+ if ( reUrl . test ( urlFromArg ( args [ 1 ] ) ) === false ) {
2066
+ return Reflect . apply ( target , thisArg , args ) ;
2067
+ }
2068
+ thisArg . addEventListener ( 'readystatechange' , function ( ) {
2069
+ if ( thisArg . readyState !== 4 ) { return ; }
2070
+ const type = thisArg . responseType ;
2071
+ if ( type === 'text' ) {
2072
+ const textin = thisArg . responseText ;
2073
+ const textout = pruneFromText ( textin ) ;
2074
+ if ( textout === textin ) { return ; }
2075
+ Object . defineProperty ( thisArg , 'response' , { value : textout } ) ;
2076
+ Object . defineProperty ( thisArg , 'responseText' , { value : textout } ) ;
2077
+ return ;
2078
+ }
2079
+ if ( type === 'document' ) {
2080
+ pruneFromDoc ( thisArg . response ) ;
2081
+ return ;
2082
+ }
2083
+ } ) ;
2084
+ return Reflect . apply ( target , thisArg , args ) ;
2085
+ }
2086
+ } ) ;
2056
2087
}
2057
2088
2058
2089
/******************************************************************************/
0 commit comments