简体   繁体   中英

Firefox addon SDK replace request

I tried to replace by a local script the url of scripts loaded by websites.

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.

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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