Created
September 6, 2017 20:45
-
-
Save Tiny-Giant/80e4f5d16283dd4e025677285e8fcb0f to your computer and use it in GitHub Desktop.
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
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