如何在Firefox的WebExtension中覆盖XMLHttpRequest [英] How to override XMLHttpRequest in Firefox's WebExtension

查看:57
本文介绍了如何在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")

这篇关于如何在Firefox的WebExtension中覆盖XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆