Skip to content

Instantly share code, notes, and snippets.

@Tiny-Giant
Created September 6, 2017 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiny-Giant/80e4f5d16283dd4e025677285e8fcb0f to your computer and use it in GitHub Desktop.
Save Tiny-Giant/80e4f5d16283dd4e025677285e8fcb0f to your computer and use it in GitHub Desktop.
const XHR = XMLHttpRequest;
unsafeWindow.XMLHttpRequest = new Proxy(XHR, {
construct: (target, args) => {
const listeners = [{
regex: /close\/add/,
get: data => {
if(data.property === 'responseText') {
console.log(xhr.responseText);
const obj = JSON.parse(xhr.responseText);
if(!obj.Count) {
Object.assign(obj, {
"Message":"Your vote has been recorded",
"ResultChangedState":false,
"Count":4,
"CountNeededForStateChange":1
});
}
data.value = JSON.stringify(obj);
}
return data;
}
},
/* For teh debug porpoises
{
regex: /./,
get: data => console.log(xhr.responseURL, 'get', data),
set: data => console.log(xhr.responseURL, 'set', data),
call: data => console.log(xhr.responseURL, 'call', data)
}
*/
];
const callall = (type, data) => (listeners.forEach(e => type in e && e.regex.test(xhr.responseURL) && Object.assign(data, e[type](data))), data);
const xhr = new XHR();
return new Proxy(xhr, {
get: (t, k) => {
if(typeof xhr[k] === 'function') {
return new Proxy(xhr[k], {
apply: async (target, self, args) => {
const result = await xhr[k](...args);
const data = callall('call', { 'method': k, 'args': args.join(', '), 'value': result });
return data.value;
}
});
}
const data = callall('get', { 'property': k, 'value': xhr[k] });
return data.value;
},
set: (t, k, v) => {
const data = callall('set', { 'property': k, 'value': v });
xhr[k] = data.value;
return true;
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment