没有简单、直接的方式来做你想做的事。如果您真的想直接在特定索引处打开标签,那么您可以查看code for gBrowser.addTab() 和code for gBrowser.moveTabTo();复制它们并修改它们以执行您想要的操作。请注意,此代码是 JavaScript 的 XML 表示形式。因此,如果你想使用它,你需要重新格式化它。
不过,简单的方法是打开标签gBrowser.addTab()。然后,将其移至您想要的索引,gBrowser.moveTabTo()。
下面的代码会做你想做的事。当我将此代码附加到按钮时,选项卡在视觉上似乎在指定的索引处打开。它确实没有首先在选项卡的末尾打开,然后似乎移动了。这样做,添加然后移动,而不是实际上在指定的索引处添加选项卡,用户没有明显的区别。
function handleButtonCommandEvent(event) {
let window = event.view;
//Create the window variable if it does not exist. It should
// already be defined from event.view.
// This should work from any Firefox context.
if (typeof window === "undefined") {
//If there is no window defined, get the most recent.
var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
}
//Test addTabAtIndex()
addTabAtIndexInWindow(window, 2, "http://www.ebay.com/")
}
/**
* Open a tab in specified window at index.
*/
function addTabAtIndexInWindow(window, index, URL, referrerURI, charset, postData,
owner, allowThirdPartyFixup ) {
//Get the gBrowser for the specified window
let winGBrowser = window.gBrowser;
//Open a new tab:
let newTab = winGBrowser.addTab(URL, referrerURI, charset, postData,
owner, allowThirdPartyFixup );
//Immediately move it to the index desired:
winGBrowser.moveTabTo(newTab,index);
}