Modify URL before loading page in firefox

后端 未结 4 858
别那么骄傲
别那么骄傲 2020-12-03 05:52

I want to prefix URLs which match my patterns. When I open a new tab in Firefox and enter a matching URL the page should not be loaded normally, the URL should first be modi

相关标签:
4条回答
  • 2020-12-03 06:37

    You could listen for the page load event or maybe the DOMContentLoaded event instead. Or you can make an nsIURIContentListener but that's probably more complicated.

    0 讨论(0)
  • 2020-12-03 06:46

    Thanks to Iwburk, I have been able to do this.

    We can do this my overriding the nsiHttpChannel with a new one, doing this is slightly complicated but luckily the add-on https-everywhere implements this to force a https connection.

    https-everywhere's source code is available here

    Most of the code needed for this is in the files

    IO Util.js ChannelReplacement.js

    We can work with the above files alone provided we have the basic variables like Cc,Ci set up and the function xpcom_generateQI defined.

    var httpRequestObserver =
    { 
      observe: function(subject, topic, data) {
        if (topic == "http-on-modify-request") {
    
            var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);     
            var requestURL = subject.URI.spec;
    
            if(isToBeReplaced(requestURL))  {
    
                var newURL = getURL(requestURL);        
                 ChannelReplacement.runWhenPending(subject, function() {
                        var cr = new ChannelReplacement(subject, ch);
                        cr.replace(true,null);
                        cr.open();
                    });
            }
        }
    
      },
    
      get observerService() {
        return Components.classes["@mozilla.org/observer-service;1"]
                         .getService(Components.interfaces.nsIObserverService);
      },
    
      register: function() {
        this.observerService.addObserver(this, "http-on-modify-request", false);
    
      },
    
      unregister: function() {
        this.observerService.removeObserver(this, "http-on-modify-request");
    
      }
    };
    
    
    httpRequestObserver.register();
    

    The code will replace the request not redirect.

    While I have tested the above code well enough, I am not sure about its implementation. As far I can make out, it copies all the attributes of the requested channel and sets them to the channel to be overridden. After which somehow the output requested by original request is supplied using the new channel.

    P.S. I had seen a SO post in which this approach was suggested.

    0 讨论(0)
  • 2020-12-03 06:55

    Browsing the HTTPS Everywhere add-on suggests the following steps:

    1. Register an observer for the "http-on-modify-request" observer topic with nsIObserverService
    2. Proceed if the subject of your observer notification is an instance of nsIHttpChannel and subject.URI.spec (the URL) matches your criteria
    3. Create a new nsIStandardURL
    4. Create a new nsIHttpChannel
    5. Replace the old channel with the new. The code for doing this in HTTPS Everywhere is quite dense and probably much more than you need. I'd suggest starting with chrome/content/IOUtils.js.

    Note that you should register a single "http-on-modify-request" observer for your entire application, which means you should put it in an XPCOM component (see HTTPS Everywhere for an example).

    The following articles do not solve your problem directly, but they do contain a lot of sample code that you might find helpful:

    • https://developer.mozilla.org/en/Setting_HTTP_request_headers
    • https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads
    0 讨论(0)
  • 2020-12-03 06:57

    Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?

    YES it is possible.

    Use page-mod of the Addon-SDK by setting contentScriptWhen: "start"

    Then after completely preventing the document from getting parsed you can either

    1. fetch a different document from the same domain and inject it in the page.
    2. after some document.URL processing do a location.replace() call

    Here is an example of doing 1. https://stackoverflow.com/a/36097573/6085033

    0 讨论(0)
提交回复
热议问题