How to get the active tab URL in an e10s add-on

434 Views Asked by At

For a toolbar button click, I need to get the URL address of the active tab.

But

window.gBrowser.selectedBrowser.contentDocument

gets a CPOW error.

How can I get the URL location of the active tab URL within an e10s add-on?

1

There are 1 best solutions below

3
On BEST ANSWER

Looking around at the objects available, and in the source code, it looks like where you should get the URI for the active tab is:

From the current nsIURI:

window.gBrowser.currentURI.spec

The object window.gBrowser.currentURI returns a nsIURI which has a number of properties from which you could get the URI, including:

[nsIURI].spec //Returns a string representation of the URI. 
[nsIURI].asciiSpec //The URI spec with an ASCII compatible encoding. 
[nsIURI].specIgnoringRef //Returns a string representation of the URI without the ref
                         //  (part after the #) portion.

You can also get the nsIURI for the current selected tab as:

window.gBrowser.selectedBrowser._documentURI

From the urlbar:
You could, of course, pull the URL out of the urlbar:

window.document.getElementById('urlbar').value

Finding window:
All of the above assume that you have set window appropriately to the currently active window. For example, by doing something like:

    //  Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
    /* Add-on SDK:
    let window = require('sdk/window/utils').getMostRecentBrowserWindow();
    //*/
    //* Overlay and bootstrap (from almost any context/scope):
    Components.utils.import("resource://gre/modules/Services.jsm"); //Services
    let window=Services.wm.getMostRecentWindow("navigator:browser");        
    //*/