Open the tab next to the active tab in the Firefox Add-on SDK

I am trying to open a new tab next to the active tab in addition to firefox. Here is my code:

var tabs = require("sdk/tabs");
tabs.open("http://www.google.com");

      

A new tab will open at the end of the list of tabs. I couldn't figure out how to get it to place a new tab right after the active tab.

+3


source to share


2 answers


Get the index of the current tab and then set the index of the new tab to + 1

var tabs = require("sdk/tabs");
var index = tabs.activeTab.index;
tabs.open("http://www.google.com");
tabs.activeTab.index = index + 1;

      

Alternatively, if you look at the docs , you will see that there is a constructor parameter named

inBackground: boolean. If present and correct, a new tab will open to the right of the active tab and will not be active.



By combining this with the onOpen event, you can achieve the desired effect:

var tabs = require("sdk/tabs");
tabs.open({
  url: "http://www.google.com",
  inBackground: true,
  onOpen: function(tab) {
    tab.activate();
  }
});

      

I haven't tested any of them, so some debugging may be required.

+2


source


Another way, a lower level API like gtranslate does :

const { getMostRecentBrowserWindow } = require('sdk/window/utils')
const browser = getMostRecentBrowserWindow().gBrowser
const tab = browser.loadOneTab(url, {relatedToCurrent: true})
browser.selectedTab = tab

      



Please note that this may not work with e10s .

0


source







All Articles