Add hooks.js for Ajax request intercepting

This commit is contained in:
chylex committed 2016-10-23 00:51:37 +02:00
1 parent e7e0163fc7
commit 265e8bf29d
1 file changed
+31
+31
View File
@@ -0,0 +1,31 @@
var HOOKS = (function(){
return {
/*
* Intercepts an AJAX response, and passes the original request open() arguments and the request object itself to the provided function.
*/
onAjaxResponse: function(func){
var prevOpen = XMLHttpRequest.prototype.open;
var prevSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function(){
this.$requestArguments = arguments;
return prevOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function(){
var req = this;
var prevEvent = this.onreadystatechange;
this.onreadystatechange = function(){
if (req.readyState === XMLHttpRequest.DONE && req.status === 200){
func(this.$requestArguments, req);
}
return prevEvent.apply(this, arguments);
};
return prevSend.apply(this, arguments);
};
}
};
})();