Tony
Tony

Reputation: 3489

XUL get current tab number when when user closes previous tabs

I've a plugin listenning to a certain tabindex number when it loads, for example in tabindex 3, but if the user closes 1 or 2 or all previous tabs to it is there a way to know that the tabindex 3 is now the 1 or 2?

Upvotes: 1

Views: 350

Answers (1)

Filipe Silva
Filipe Silva

Reputation: 21657

Yes. The index will always be updated to represent the order of the tabs from left to right, starting at 0. If you remove the tab at index 0, 1 or 2 your tab that was original at index 3 will be at 2.

You can test this out yourself:

window.addEventListener("load", function () {
    var container = gBrowser.tabContainer;
    container.addEventListener("TabSelect", function () {
        console.log("SELECT: " + gBrowser.selectedTab.linkedPanel
                            + " - " + gBrowser.tabContainer.selectedIndex);
    }, false);
    container.addEventListener("TabClose",  function () {
        window.setTimeout(function(){
           console.log("CLOSE: " + gBrowser.selectedTab.linkedPanel
              + " - " + gBrowser.tabContainer.selectedIndex);}, 2000)
    }, false);

}, false);

This listens to TabSelect and TabClose events to show them change. The setTimeout is to let the tab close, since the events fire before they actually happen.

To test it, open 4 tabs and select the 4th tab. Open the console and then close one of the first tabs. You will see that it changes.


If you need a more "unique" you could use the tab's linkedPanel id to identify the tab you want. Although this will change if you move the tab to a different Window.

Upvotes: 1

Related Questions