Search code examples
firefoxurlxulsidebar

Get current page URL from a firefox sidebar extension


I'm writing a sidebar extension for Firefox and need a way to get the URL of the current page so I can check it against a database and display the results. How can I do this?


Solution

  • window.top.getBrowser().selectedBrowser.contentWindow.location.href;
    

    might work, otherwise I think you need to use:

    var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                       .getInterface(Components.interfaces.nsIWebNavigation)
                       .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                       .rootTreeItem
                       .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                       .getInterface(Components.interfaces.nsIDOMWindow);
    
    mainWindow.getBrowser().selectedBrowser.contentWindow.location.href;