1

I wanted to know how to modify the URL of a page when it loads, using a Firefox plugin. I was directed to advice such as the following. Specifically, when a user goes to "https://www.google.com" through the address bar, I wanted to redirect to "https://www.google.co.uk".

For my specific purposes, the above solution almost works, but it redirects too many URLs. One problem that I have found with this is as follows: While I can redirect every time, I sometimes redirect when I don't want to. For instance, it seems that in Windows, I not only redirect when the current tab's URL is regular Google, but I also redirect whenever some query that a page makes contains the above link (e.g. a news article).

Is there any way to use Mozilla's observer framework to redirect by checking the current tab's URL ONLY? If not, is there some alternate solution that still uses Mozilla's Add-on SDK?

Edit: Below is my buggy source code.

var myObserver = {
    register: function() {
        var observerService = Components.classes["@mozilla.org/observer-service;1"]
        .getService(Components.interfaces.nsIObserverService);
        observerService.addObserver(this, TOPIC_MODIFY_REQUEST, false);
    },
    //observe function to capture the changed event
    observe : function(aSubject, aTopic, aData) {
        if (TOPIC_MODIFY_REQUEST == aTopic ) {
            var url;
            aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);

            url = aSubject.URI.spec;
            url = encodeURIComponent(url);


            if (url.indexOf("google.nl") !=-1 || url.indexOf("hl%3Dnl") !=-1
               || url.indexOf("google.com%2Fnl") !=-1 || url == "https%3A%2F%2Fwww.google.com" 
               || url == "https%3A%2F%2Fwww.google.com%2Fncr" || url == 
               "https%3A%2F%2Fwww.google.com%2F%3Fgws_rd%3Dssl")
            {
                aSubject.cancel(Components.results.NS_BINDING_ABORTED);
                loadURL("https://www.google.co.uk/");
            }
    }
}

function loadURL(url) {
    // Set the browser window's location to the incoming URL
    window._content.document.location = url;
    // Make sure that we get the focus
    window.content.focus();
}
Community
  • 1
  • 1
Matt
  • 303
  • 1
  • 2
  • 16
  • instead of aSubject.cancel, you should do aSubject.redirectTo, because your loadUL function is only working on the currently selected tab. – Noitidart Nov 06 '14 at 21:45

1 Answers1

1

check for the LOAD_INITIAL_DOCUMENT_URI flag, if it is, then abort/redirct it. Otherwise don't. if this flag exists it is the top level page.

edit: based on your code, you need to to do test the aSubject that was QueryInterfaced if it has flags so like this:

var oHttp = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
if (oHttp.loadFlags & Components.interfaces.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI) {
  //is top level load
} 
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • Took a long time for me to finally test this, but for top-level-only, this works! :D Thanks! – Matt Nov 06 '14 at 21:11