firefox-addonfirefox-addon-restartless

How to get the tab Index of the right clicked inactive tab?


Who to get the tab Index of the right clicked tab that fires the Tab context-menu. Tab is NOT the active tab (not the selectedIndex)?

As an example. "Close Tabs to the Right" in Tab context menu works regardless of which tab (active/not-active) tab is right-clicked. How does it get the correct tab index?


Solution

  • Listen for for the popupshown event of the tabContextMenu element.

    Since this a restartless addon I assume that you already have a reference to the ChromeWindow.

    var tabContextMenu = chromewin.document.getElementById("tabContextMenu");
    tabContextMenu.addEventListener("popupshown", function(){
      var rightclickedtab = chromewin.TabContextMenu.contextTab;
      // now proceed as you wish
    }, false);
    

    You can also add your own menu item and listen for its command event.

    In any case, remember to cleanup when your extension gets unloaded.