2016-03-07 96 views
2

假设Firefox浏览器窗口中有10个选项卡。通过Firefox扩展在特定位置打开选项卡

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

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

+0

从的[文件](https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Method/addTab),我不认为你可以看起来。 'addTab'只允许使用'URL','referrerURI','charset','postData','owner'和'allowThirdPartyFixup'作为参数,这些参数都不影响位置。 – GAntoine

回答

3

有没有简单的,直接做你想做的事情的方式。如果你真的想要打开一个标签直接在一个特定的索引,那么你可以看看code forgBrowser.addTab()code forgBrowser.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); 

} 
+0

谢谢@Makyen,作品非常棒。 –

+1

@JohnSewell,我更新了代码,使其具有在指定窗口中的索引处添加选项卡的通用函数。 – Makyen

+0

看起来不错。谢谢。 –

相关问题