问题
I am building a proxy for a video player which can intercept the http requests and send back the response using a Promise which can be resolved asynchronously
I have found this way of intercepting a http request
// save original `send` method
const origSend = XMLHttpRequest.prototype.send;
// redefine `send` method
// you could also pass extra parameters if needed
XMLHttpRequest.prototype.send = (...origParams) => {
console.log('send called');
origSend(...origParams);
}
Now I don't want to return the response using origSend like in the above way. What I need is to send the url to my backend engine which will download the content and send me the bytearray which I need to send back to the video player. To achieve this I was thinking of using a Promise which will be resolved by my backend engine when it is able to complete the request
Now what I am unsure is how to return the bytearray fetched by my engine back to the video player from the intercepted code. Would be great to find some relevant code snippets.
来源:https://stackoverflow.com/questions/63864504/intercept-xmlhttprequest-and-send-a-promise