0

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?

Makyen
  • 31,849
  • 12
  • 86
  • 121
John Bernard
  • 777
  • 2
  • 7
  • 16
  • 1
    What [kind of Firefox extension](https://developer.mozilla.org/en-US/Add-ons) are you making ([WebExtensions](https://developer.mozilla.org/en-US/Add-ons/WebExtensions), [Add-on SDK](https://developer.mozilla.org/en-US/Add-ons/SDK), [Bootstraped](https://developer.mozilla.org/en-US/Add-ons/Bootstrapped_extensions), or [Overlay/XUL/Legacy](https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions))? Please [edit] the appropriate tag into your question. From your use of `window.gBrowser` it is more likely to be Bootstraped, or Overlay/XUL/Legacy. – Makyen Sep 02 '16 at 06:45
  • yes it is Bootstraped, or Overlay/XUL/Legacy. I suppose I cant add new tags by myself? – John Bernard Sep 03 '16 at 16:51
  • You are always able to edit your own question, including adding or removing tags. I have added tags for bootstrapped/restartless and overlay. – Makyen Sep 03 '16 at 17:40

1 Answers1

1

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");        
    //*/
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Makyen, same add-on but now I need to insert css tag into head element of active document by toolbar button. Is it possible for e10s without content script? Or should I ask new question? – John Bernard Sep 07 '16 at 16:39
  • @JohnBernard, It is a new question. However, you are correct in your belief that it would require a content script of some sort. The separation of the active document from background scripts is one of the primary effects on add-ons of multiprocess (e10s). Only a very limited amount of information (e.g. the URL as in above) is available to the background process without the use of a content script. – Makyen Sep 07 '16 at 16:43
  • Thanks so much @Makyen. Appreciated. – John Bernard Sep 07 '16 at 16:59