Created
April 22, 2022 10:27
-
-
Save davidsharp/60f9300494731058eae3deaebbff9da2 to your computer and use it in GitHub Desktop.
Proxying a XMLHttpRequest to force the status received
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
XMLHttpRequest = new Proxy(XMLHttpRequest, { | |
construct:function(t,a){ | |
const req = new t(); | |
return new Proxy(req, { | |
get:function(o,p){ | |
if(p=='status')return 9001 | |
return typeof o[p] == 'function'?o[p].bind(o):o[p] | |
}, | |
set: function(target, prop, value) { | |
Reflect.set(target, prop, value) // or target[prop] = value | |
return true; | |
}, | |
}) | |
} | |
}) | |
var oReq = new XMLHttpRequest(); | |
oReq.addEventListener("load", transferComplete); | |
oReq.addEventListener("error", transferComplete); | |
oReq.open("GET", "https://blossom-emery.glitch.me/yo.json"); | |
oReq.responseType = "json"; | |
oReq.send() | |
console.log(oReq) | |
function transferComplete(evt) { | |
console.log(evt); | |
console.log(oReq.status,oReq.response); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed this for some reason a couple of years back, and only just got the SO answer that got this properly working recently