【问题标题】:Open Tab in a Specific Position by Firefox extension通过 Firefox 扩展在特定位置打开选项卡
【发布时间】:2016-03-07 18:47:06
【问题描述】:

比如说,Firefox 浏览器窗口中有 10 个标签页。

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

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

【问题讨论】:

  • Docs 的外观来看,我认为您不能。 addTab 只允许将URLreferrerURIcharsetpostDataownerallowThirdPartyFixup 作为参数,这些参数似乎都不会影响位置。

标签: javascript firefox firefox-addon firefox-addon-bootstrap firefox-addon-overlay


【解决方案1】:

没有简单、直接的方式来做你想做的事。如果您真的直接在特定索引处打开标签,那么您可以查看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);

}

【讨论】:

  • @JohnSewell,我更新了代码,使其具有通用函数,用于在指定窗口的索引处添加选项卡。
  • 看起来不错。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多