Skip to content

Instantly share code, notes, and snippets.

@MarkoSh
Last active May 23, 2024 23:08
Show Gist options
  • Save MarkoSh/c2a125a1f16d785507ce3b3f65c3a645 to your computer and use it in GitHub Desktop.
Save MarkoSh/c2a125a1f16d785507ce3b3f65c3a645 to your computer and use it in GitHub Desktop.
Replace XMLHttpRequest
XMLHttpRequest = new Proxy( XMLHttpRequest, {
construct( target ) {
const xhr: any = new target();
( (
open,
setRequestHeader,
send,
onreadystatechange
) => {
xhr.open = function () {
const [ method, url, async, user, password ] = [ ...arguments ];
xhr.openArgs = {
method,
url,
async,
user,
password
};
return open.apply( this, arguments );
};
xhr.setRequestHeader = function () {
const [ header, value ] = [ ...arguments ];
if ( ! xhr.requestHeaders ) xhr.requestHeaders = {};
xhr.requestHeaders[ header ] = value;
return setRequestHeader.apply( this, arguments );
};
xhr.send = function () {
const [ body ] = [ ...arguments ];
xhr.sendArgs = {
body
}
xhr.onreadystatechange = function () {
const [ event ] = [ ...arguments ];
const responseHeadersRAW = xhr.getAllResponseHeaders();
if ( '' != responseHeadersRAW ) {
const responseHeaders = responseHeadersRAW.split( "\r\n" ).filter( ( row: any ) => row ).map( ( row: any ) => {
const split = row.split( ': ' );
const name = split[ 0 ];
const value = split[ 1 ].trim();
const header: any = {};
header[ name ] = value;
return header;
} ).reduce( ( acc: any, next: any ) => {
return Object.assign( acc, next );
} );
xhr.responseHeaders = responseHeaders;
const dateHeader = responseHeaders[ 'date' ];
if ( dateHeader ) {
const date = new Date( dateHeader );
$this.date = date;
}
}
if ( 200 === xhr.status && 4 === xhr.readyState ) {
const responseJSON = JSON.parse( xhr.response );
const url = new URL( xhr.responseURL );
xhr.capturedResponse = {
responseJSON,
url
};
const response = xhr.response;
const responseText = xhr.responseText;
Object.defineProperty( xhr, "response", {
writable: true
} );
Object.defineProperty( xhr, "responseText", {
writable: true
} );
xhr.response = response;
xhr.responseText = responseText;
}
return onreadystatechange ? onreadystatechange.apply( this, arguments ) : null;
};
return send.apply( this, arguments );
};
} )(
xhr.open,
xhr.setRequestHeader,
xhr.send,
xhr.onreadystatechange
);
return xhr;
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment