Skip to content

Instantly share code, notes, and snippets.

@Amice13
Forked from icodejs/ajaxListener.js
Created August 31, 2016 12:32
Show Gist options
  • Save Amice13/a8a09a28694737e1a6d5f5ba713cb341 to your computer and use it in GitHub Desktop.
Save Amice13/a8a09a28694737e1a6d5f5ba713cb341 to your computer and use it in GitHub Desktop.
JS: Listen to ajax calls from Javascript
var open = window.XMLHttpRequest.prototype.open,
send = window.XMLHttpRequest.prototype.send,
onReadyStateChange;
function openReplacement(method, url, async, user, password) {
var syncMode = async !== false ? 'async' : 'sync';
console.warn(
'Preparing ' +
syncMode +
' HTTP request : ' +
method +
' ' +
url
);
return open.apply(this, arguments);
}
function sendReplacement(data) {
console.warn('Sending HTTP request data : ', data);
if(this.onreadystatechange) {
this._onreadystatechange = this.onreadystatechange;
}
this.onreadystatechange = onReadyStateChangeReplacement;
return send.apply(this, arguments);
}
function onReadyStateChangeReplacement() {
console.warn('HTTP request ready state changed : ' + this.readyState);
if(this._onreadystatechange) {
return this._onreadystatechange.apply(this, arguments);
}
}
window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment