简体   繁体   English

Firefox插件SDK替换请求

[英]Firefox addon SDK replace request

I tried to replace by a local script the url of scripts loaded by websites. 我试图用本地脚本替换网站加载的脚本的URL。

I tried to channel.redirectTo() with data.url() and chrome:// (with contentaccessible=yes flag in manifest), but doesn't work, so I compared a regex pattern, if true, it will cancel the XHR GET request. 我试图将channel.redirectTo()data.url()chrome:// (清单中带有contentaccessible=yes标志)一起使用,但没有用,所以我比较了一个regex模式,如果为true,它将取消XHR GET请求。

For example 例如

<script src="http://url/to/script.js"></script>

become 成为

<script src="resource://url/to/new/script.js"></script>

or 要么

<script src="chrome://url/to/new/script.js"></script>

Now I need to replace the url or inject my new script to the page 现在,我需要替换网址或将新脚本插入页面

main.js main.js

var listener = function (event) {
    var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
    var match = someFunctionToMatchRegex(channel.URI.spec);
    if (match) {
        channel.cancel(Cr.NS_BINDING_ABORTED);
    }
};

events.on("http-on-modify-request", listener);

I have a redirect module here which is a good example for what you want to do. 我在这里有一个重定向模块,这是您想要做的一个很好的例子。 You might be able to use it, though the module only takes specific urls and not reg expressions at the moment. 您可能可以使用它,尽管此刻模块仅接受特定的url,而不接受reg表达式。 I'd certainly take a pull request to make this change though. 我当然会提出要求,以进行此更改。

The code basically just uses redirectTo as you mention, so something else is wrong. 正如您所提到的,代码基本上只使用redirectTo ,所以其他地方是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM