views:

717

answers:

3

I'm writing a firefox extension and really need to listen on TabOpen events and get some details about tab that was opened. But I can't figure out how do I get an actual tab from event object that my callback receives. Is it somewhere in event.data? Is there a way to inspect this object?

Some code that I have tried so far but it doesn't work:

Application.activeWindow.events.addListener("TabOpen",
 function(event) {
  Application.console.log("TabOpen");
  var tab = event.data.target;
  Application.console.log(tab.uri);
 }
);
A: 

here is an example from the MDC but without using FUEL:

// add event listener
var container = gBrowser.mPanelContainer;
container.addEventListener("DOMNodeInserted", exampleTabAdded, false);

function exampleTabAdded(event)
{ // listening for new tabs
  if (event.relatedNode != gBrowser.mPanelContainer)
    return; //Could be anywhere in the DOM (unless bubbling is caught at the interface?)

  var browser;
    browser = event.target.childNodes[1];
  // browser is the XUL element of the browser that's been added
}
Marwan Aouida
Hm, that's definitely cheating :) I'd prefer something more version agnostic.
vava
+1  A: 

In your code, event.data will give you a BrowserTab object. If you want the current URI of the tab, you'd want tab.uri.spec for the string version, or just tab.uri if you want an nsIURI object.

sdwilsh
Thanks, that was working beautifully. Could you also tell me the secret how do you found this out? Because I can't find anything about this in MDC.
vava
I'm a Mozilla developer, so I first went to the source code to look at things. I'm not sure the source code would help someone who isn't somewhat familiar with it though...I'll let our documentation folks know that we might be lacking a bit here.
sdwilsh
Our documentation lead just informed me that he's added a bunch of documentation to help make this more clear.
sdwilsh
A: 

I've added some new content to MDC that should help with this; information on how to pull the tab object out of the TabOpen event is now available in the example here:

https://developer.mozilla.org/En/FUEL/Window

Also did some other cleaning up while I was at it. Hopefully this will help (especially once the search index refreshes).

Thanks, that's awesome! Not only I got an answer to the question but potentially answers to future questions in one go :)
vava