Skip to content

Instantly share code, notes, and snippets.

@jasperck
Last active November 12, 2016 08:25
Show Gist options
  • Save jasperck/130651ca255eec1772074e5d5428c363 to your computer and use it in GitHub Desktop.
Save jasperck/130651ca255eec1772074e5d5428c363 to your computer and use it in GitHub Desktop.
To intercept and modify the response, browser could render with your mutation
(function () {
const XHR = window.XMLHttpRequest;
function newXHR () {
var xhr = new XHR();
xhr.addEventListener("readystatechange", () => {
if (xhr.readyState === 4) {
var originalResponse = JSON.parse(xhr.responseText); // store first
Object.defineProperty(xhr, "responseText", { writable: true }); // make it writable, careful it will also clear it
originalResponse.payload = '我改到你了'; // do what you want with the response
xhr.responseText = JSON.stringify(originalResponse);
}
}, false);
return xhr;
}
window.XMLHttpRequest = newXHR;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment