Skip to content

Instantly share code, notes, and snippets.

@chappy84
Last active April 18, 2024 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chappy84/a2c0b0d071b456e56fee856b2baae34b to your computer and use it in GitHub Desktop.
Save chappy84/a2c0b0d071b456e56fee856b2baae34b to your computer and use it in GitHub Desktop.
const functionsToOverride = ['overrideMimeType', 'send'];
functionsToOverride.forEach(function(fnName) {
window.XMLHttpRequest.prototype[fnName] = new Proxy(window.XMLHttpRequest.prototype[fnName], {
apply: (target, thisArg, argumentsList) => {
console.log(fnName + ' arguments', argumentsList);
thisArg[fnName + 'Args'] = [...argumentsList];
const retVal = target.apply(thisArg, argumentsList);
console.log(fnName + ' retVal', retVal);
return retVal;
}
});
});
window.XMLHttpRequest.prototype.open = new Proxy(window.XMLHttpRequest.prototype.open, {
apply: (target, thisArg, argumentsList) => {
console.log('open arguments', argumentsList);
thisArg.openArgs = [...argumentsList];
const listenEvents = ['onreadystatechange', 'abort', 'error', 'load', 'loadstart', 'loadend', 'progress', 'timeout'];
listenEvents.forEach(function(eName) {
this.addEventListener(eName, function() {
console.log('event ' + eName + ' arguments', arguments);
});
}, thisArg);
const retVal = target.apply(thisArg, argumentsList);
console.log('open retVal', retVal);
return retVal;
}
});
window.XMLHttpRequest.prototype.setRequestHeader = new Proxy(window.XMLHttpRequest.prototype.setRequestHeader, {
apply: (target, thisArg, argumentsList) => {
console.log('setRequestHeader arguments', argumentsList);
if (!this.setRequestHeaderArgs) {
thisArg.setRequestHeaderArgs = new Array();
}
thisArg.setRequestHeaderArgs.push([...argumentsList]);
var retVal = target.apply(thisArg, argumentsList);
console.log('setRequestHeader retVal', retVal);
return retVal;
}
});
// Standard properties containing info about the request
// XMLHttpRequest.readyState
// XMLHttpRequest.response
// XMLHttpRequest.responseText
// XMLHttpRequest.responseType
// XMLHttpRequest.responseURL
// XMLHttpRequest.responseXML
// XMLHttpRequest.status
// XMLHttpRequest.statusText
// XMLHttpRequest.timeout
// XMLHttpRequest.withCredentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment