Reputation: 15045
I'm writing a Firefox extension. I'd like to have my code executed whenever a new tab opens.
I've tried this:
for (var i=0; i<Application.windows.length; i++) {
var w = Application.windows[i];
w.events.addListener("TabOpen", function(event){
alert( 'tab #'+w.activeTab.index +' opened');
});
}
It doesn't work right if windows.length > 1. For example, we have two windows. I open new tab and event fires 2 times, for every window. I wanna only one event call for current window.
Upvotes: 1
Views: 1097
Reputation: 37268
no way i didnt that works i akways attached it to gbrowser, this is how i do it: https://gist.github.com/Noitidart/8673632
where i attach domContentLoad i also attach TabOpen
Upvotes: 0
Reputation: 32073
Without FUEL: https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed. I like that better than using Application.activeWindow
, since from looking at the code there's no guarantee activeWindow
always refers to the same window that your code is executing in. (If they are different, you'll end up with weird hard to reproduce problems).
The reason your original code didn't work was that you're presumably putting your code in a script for an overlay to the main browser window. Such code is executed in every instance of the browser window the user opens. So when you open the second window, the loop executed again and added a second listener for the 1st window and the first (only) listener to the 2nd window.
Upvotes: 2
Reputation: 15045
Answer to my own question:
window.addEventListener('load', function(){
var w = Application.activeWindow;
w.events.addListener("TabOpen", function(event){
alert( 'tab #'+w.activeTab.index +' opened');
});
}, false);
Upvotes: 2