Skip to content

Instantly share code, notes, and snippets.

@scips
Created December 6, 2021 08:31
Show Gist options
  • Save scips/ec1100e72624067d5b4bca412fb92cbe to your computer and use it in GitHub Desktop.
Save scips/ec1100e72624067d5b4bca412fb92cbe to your computer and use it in GitHub Desktop.
<!-- begin snippet: js hide: false console: false babel: null -->
<!-- language: lang-js -->
<!-- @credit: EtherDream https://stackoverflow.com/users/4565883/etherdream -->
//
// firefox, ie8+
//
var accessor = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'responseText');
Object.defineProperty(XMLHttpRequest.prototype, 'responseText', {
get: function() {
console.log('get responseText');
return accessor.get.call(this);
},
set: function(str) {
console.log('set responseText: %s', str);
//return accessor.set.call(this, str);
},
configurable: true
});
//
// chrome, safari (accessor == null)
//
var rawOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
if (!this._hooked) {
this._hooked = true;
setupHook(this);
}
rawOpen.apply(this, arguments);
}
function setupHook(xhr) {
function getter() {
console.log('get responseText');
delete xhr.responseText;
var ret = xhr.responseText;
setup();
return ret;
}
function setter(str) {
console.log('set responseText: %s', str);
}
function setup() {
Object.defineProperty(xhr, 'responseText', {
get: getter,
set: setter,
configurable: true
});
}
setup();
}
<!-- end snippet -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment