【发布时间】:2019-03-21 13:28:01
【问题描述】:
在带有扩展注入自定义XMLHttpRequest 类的网页上,我需要在文档的其余部分引发load 事件之前拦截和修改某些响应。现在,我的修改代码对加载事件做出反应。在load 事件被触发之前,我将如何使用函数来修改响应?
这是我正在使用的代码:
let oldXHROpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
var originalresp = this;
//Do something with the method, url and etc.
originalresp.addEventListener('load', async function () {
//Do something with the response text
//***This function needs to execute before load is raised for the rest of the document***
value = '<note><body>This is a test XML document.</body></note>';
parser = new DOMParser();
apiresponsexml = parser.parseFromString(value, 'application/xml');
//Replace data in the response
Object.defineProperties(originalresp, {
'response': {
value: value
},
'responseXML': {
value: apiresponsexml,
},
'responseText': {
value: value,
}
});
});
return oldXHROpen.apply(originalresp, arguments);
};
这个问题是this previous thread的延续。
【问题讨论】:
标签: javascript html google-chrome-extension xmlhttprequest