【发布时间】:2021-02-25 04:45:23
【问题描述】:
我做了一个 Firefox 扩展来获取所有请求的 url 并显示它们。但代码只有在我将其粘贴到控制台时才有效。
当扩展程序加载时它没有显示任何错误,它似乎只是不会运行
这是完整的代码
xhrScript.js
(function(){
const proxiedOpen = XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function ( _, url) {
this.__URL = url;
return proxiedOpen.apply(this, arguments);
};
const proxiedSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.send = function () {
const { protocol, host } = window.location;
// showing only when it paste in console
console.log("full request url ", `${protocol}//${host}${this.__URL}`);
return proxiedSend.apply(this, [].slice.call(arguments));
};
})();
// this works all times
document.body.style.border = "7px solid blue";
manifest.json
{
"manifest_version": 2,
"name": "XHR request urls",
"version": "1.0",
"description": "get all the request url's",
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["xhrScript.js"]
}
]
}
如您所见,最后一行是document.body.style.border = "7px solid blue";,每次都能正常工作。但是 XMLHttpRequest open 和 send 方法不起作用。仅当我将代码粘贴到控制台时才有效。
如果您想查看示例,可以尝试在 devTools 控制台中将 xhrScript.js 代码复制并粘贴到 https://reactjs.org(这是一个 SPA,因此很容易检查我想要的内容),然后查看所有请求。
我不知道为什么这段代码只有在控制台粘贴时才会运行
【问题讨论】:
标签: javascript firefox xmlhttprequest firefox-addon firefox-addon-webextensions