如何在Firefox的WebExtension中覆盖XMLHttpRequest

问题描述:

我正在尝试覆盖Firefox的WebExtension中的 XMLHttpRequest.protype.open 方法.我已经在内容脚本中编写了以下代码

I am trying to override the XMLHttpRequest.protype.open method in Firefox's WebExtension. I have written a following code in content script

var oldOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url, async, user, pass) {
    console.log("url :"+url+"\n method: "+method);
    oldOpen.apply(this,arguments);
};

但是此代码不起作用.如果有人知道如何重写XMLHttpRequest.prototype.open方法,请告诉我.

But this code is not working. If anyone know that how to override the XMLHttpRequest.prototype.open method then please let me know.

发出XHR请求的脚本无法访问内容脚本.您的内容脚本必须将包含代码的脚本插入页面.插入的页面脚本可以通过消息与内容脚本进行通信.有关将脚本插入页面的详细信息,请参见此处:使用内容脚本将代码插入页面上下文一个>.有关传达页面脚本和内容脚本的详细信息,请参见此处: https://developer.chrome.com/extensions/content_scripts(与嵌入页面的通信"部分)

The scripts making XHR requests have no access to the content script. Your content script has to insert a script with your code into the page. The inserted page script can communicate with the content script by messages. For details inserting a script into a page see here: Insert code into the page context using a content script. For details communicating page script and content script see here: https://developer.chrome.com/extensions/content_scripts (section "Communication with the embedding page")