Skip to content

Instantly share code, notes, and snippets.

@magic890
Last active February 11, 2020 14:30
Show Gist options
  • Save magic890/cee3a4f130f3b7cb3589230e70248c92 to your computer and use it in GitHub Desktop.
Save magic890/cee3a4f130f3b7cb3589230e70248c92 to your computer and use it in GitHub Desktop.
JS HTTP request interceptor
// Intercept all the HTTP requests and inject a tracker if the payload is JSON
!function() {
XMLHttpRequest.prototype._original_send = XMLHttpRequest.prototype.send;
let interceptor_send = function(data) {
try {
obj = JSON.parse(data);
obj._tracker = 'tracker_id';
let new_data = JSON.stringify(obj);
this._original_send(new_data);
}
catch(err) {
this._original_send(data);
}
};
XMLHttpRequest.prototype.send = interceptor_send;
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment