Last active
November 12, 2016 08:25
-
-
Save jasperck/130651ca255eec1772074e5d5428c363 to your computer and use it in GitHub Desktop.
To intercept and modify the response, browser could render with your mutation
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
(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