通过火狐扩展在特定位置打开选项卡

Open Tab in a Specific Position by Firefox extension

本文关键字:位置 选项 定位 过火 火狐 扩展      更新时间:2023-09-26

比如说,Firefox浏览器窗口中有10个选项卡。

如何在第二个标签之后通过 Firefox 扩展代码添加选项卡?

gBrowser.addTab方法仅追加到选项卡列表。

没有简单、直接的方法可以做你想做的事。如果您真的直接在特定索引处打开选项卡,那么您可以查看gBrowser.addTab()的代码和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);
}