Last active
February 11, 2020 14:30
-
-
Save magic890/cee3a4f130f3b7cb3589230e70248c92 to your computer and use it in GitHub Desktop.
JS HTTP request interceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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