在Firefox Add-on SDK中打开活动选项卡旁边的选项卡

时间:2014-09-16 06:13:09

标签: firefox firefox-addon-sdk

我正在尝试在firefox附加组件的活动标签旁边打开一个新标签页。这是我的代码:

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

这将打开选项卡列表末尾的新选项卡。我无法弄清楚如何强制它在活动标签后立即定位新标签。

2 个答案:

答案 0 :(得分:2)

获取当前标签的索引,然后将新标签的索引设置为+ 1

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

或者,如果您查看the docs,您会看到有一个名为

的构造函数参数
  

inBackground:boolean。如果存在且为true,则新选项卡将在活动选项卡的右侧打开,并且不会处于活动状态。

通过将此与onOpen事件相结合,您可以获得所需的效果:

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

我还没有测试过这些,所以可能需要进行一些调试。

答案 1 :(得分:0)

另一种方法是使用lower-level APIs之类的gtranslate does

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

请注意,它可能不适用于e10s

相关问题