Search code examples
firefoxfirefox-addonfirefox-addon-sdk

Firefox Addon - Change address bar url when domain matches some pattern


I would like to know how to change the address bar url when domain matches some pattern?. The url have to change after the url be loaded. I have tryed the following code. It works, but some times it doesn't. Maybe depending on the load speed, i don't know. What I mean is that this code is not too consistent. I think it could be enhanced. Maybe using an observer with: http-on-examine-response some how?. I'm looking for a consistent code.

var {getMostRecentBrowserWindow} = require("sdk/window/utils");
var {Cc, Ci, Cu} = require("chrome");

var domain = "yahoo.com";
var display_url = "http://www.mybeautifulemail.com"

function change_addressbar_url(url) {
    var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    var uri = ioService.newURI(url, null, null);
    var win = getMostRecentBrowserWindow();
    win.gBrowser.webNavigation.setCurrentURI(uri);
}

var win = getMostRecentBrowserWindow();
win.gBrowser.addEventListener("DOMContentLoaded", function () {
    var url_regexp = new RegExp(escape_regexp(domain)+"$", "i");
    if (this.currentURI.host.match(url_regexp))
        change_addressbar_url(display_url);
}, false);

function escape_regexp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

Thank you very much!

[CODE REVISION 001]

var {getMostRecentBrowserWindow} = require("sdk/window/utils");
var {Cc, Ci, Cu} = require("chrome");

var domain = "yahoo.com";
var display_url = "http://www.mybeautifulemail.com"

function escape_regexp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

function change_addressbar_url(url, tab) {
    var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    var uri = ioService.newURI(url, null, null);
    tab.linkedBrowser.webNavigation.setCurrentURI(uri);
}

var win = getMostRecentBrowserWindow();
win.gBrowser.addEventListener("DOMContentLoaded", function (e) {
    var contWin = e.target;
    if (contWin.top) {
        return; // this is a frame load so ignore it
    }
    var contDoc = contWin.document;
    var DOMWin = contWin.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIWebNavigation)
                    .QueryInterface(Ci.nsIDocShellTreeItem)
                    .rootTreeItem
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIDOMWindow);

    console.log("... CODE NEVER ARRIVES THIS POINT");

    var gBrowser = DOMWin.gBrowser;
    var tab = gBrowser._getTabForContentWindow(contWin);
    var url_regexp = new RegExp(escape_regexp(domain) + "$", "i");
    if (tab.linkedBrowser.currentURI.host.match(url_regexp)) {
        change_addressbar_url(display_url, tab);
        console.log("... host matched pattern!");
    }
}, false);

Solution

  • Your issue is the line win.gBrowser.webNavigation.setCurrentURI AND also the if (this.currentURI.host.match(url_regexp)) these lines work on the currently selected tab in that window. what you need to do is get the correct tab element then set the uri on that tab so like this: win.gBrowser.tabContainer.childNode[0].linkedBrowser.webNavigation.setCurrentURI would set the address bar in tab 1 as notice the .childNode[0]

    So this is how you get the right tab:

    var win = getMostRecentBrowserWindow();
    win.gBrowser.addEventListener("DOMContentLoaded", function (e) {
        var contWin = e.originalTarget.defaultView;
        if (contWin.fameElement) {
            // this is a frame load so ignore it
            return;
        }
        var contDoc = contWin.document;
        var DOMWin = contWin.QueryInterface(Ci.nsIInterfaceRequestor)
                        .getInterface(Ci.nsIWebNavigation)
                        .QueryInterface(Ci.nsIDocShellTreeItem)
                        .rootTreeItem
                        .QueryInterface(Ci.nsIInterfaceRequestor)
                        .getInterface(Ci.nsIDOMWindow);
        var gBrowser = DOMWin.gBrowser;
    
        var tab = gBrowser._getTabForContentWindow(contWin);
        var url_regexp = new RegExp(escape_regexp(domain)+"$", "i");
        if (tab.linkedBrowser.currentURI.host.match(url_regexp))
            change_addressbar_url(display_url, tab);
    }, false);
    
    function change_addressbar_url(url, tab) {
        var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
        var uri = ioService.newURI(url, null, null);
        tab.linkedBrowser.webNavigation.setCurrentURI(uri);
    }