Intercept XMLHttpRequest and send a Promise

谁说胖子不能爱 提交于 2021-02-04 21:48:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!