简体   繁体   中英

XUL get current tab number when when user closes previous tabs

我有一个插件在加载时会监听某个tabindex编号,例如在tabindex 3中,但是如果用户关闭1或2或所有以前的选项卡,则有一种方法可以知道tabindex 3现在是1还是2?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM