From 265e8bf29d38a668bf3e2d92b32578e0e6f145bd Mon Sep 17 00:00:00 2001 From: chylex Date: Sun, 23 Oct 2016 00:51:37 +0200 Subject: [PATCH] Add hooks.js for Ajax request intercepting --- src/tracker/hooks.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/tracker/hooks.js diff --git a/src/tracker/hooks.js b/src/tracker/hooks.js new file mode 100644 index 0000000..57cb1d0 --- /dev/null +++ b/src/tracker/hooks.js @@ -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); + }; + } + }; +})(); \ No newline at end of file