Skip to content

Instantly share code, notes, and snippets.

@CezaryDanielNowak
Created February 28, 2014 15:37
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 CezaryDanielNowak/9273158 to your computer and use it in GitHub Desktop.
Save CezaryDanielNowak/9273158 to your computer and use it in GitHub Desktop.
(function() {
window.xhrs = [];
var tmpSend = XMLHttpRequest.prototype.send,
tmpSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader,
tmpOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(type, url, async, user, pass) {
this._requestHeaders = [];
this._openArgs = arguments;
// this.theRequestUrlAndType = type + ' ' + url
// + (user || pass ? ' (' + user + ' / ' + pass + ')' : '');
return tmpOpen.apply(this, this._openArgs);
};
XMLHttpRequest.prototype.setRequestHeader = function() {
this._requestHeaders.push(arguments);
return tmpSetRequestHeader.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function(/* data is arguments[0] */) {
var t = this;
var args = arguments;
t._edit = function() {
t._openArgs[1] = prompt("Confirm XHR url:\n" + t._openArgs[1], t._openArgs[1]) || t._openArgs[1];
var newData = prompt("Confirm XHR data for:\n" + t._openArgs[1], (args[0] == null ? '' : args[0]) );
if(newData === null)
return;
args[0] = newData;
var xhr = new XMLHttpRequest();
xhr.open.apply(xhr, t._openArgs);
if(t._requestHeaders && this._requestHeaders.length) {
t._requestHeaders.forEach(function(headerArgs){
tmpSetRequestHeader.apply(xhr, headerArgs);
});
}
return xhr.send.apply(xhr, args);
};
xhrs.push(t);
console.log("xhrs[" + (xhrs.length-1) + "]._edit() to resend with new data", args);
return tmpSend.apply(t, args);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment