2010-12-08 42 views
0

我做了这个扩展的Safari浏览器是关闭当前页面Chrome扩展到河套紧密的,非指定的标签

(var tabss = safari.application.activeBrowserWindow.tabs; 

      for (n=0; n<tabss.length; n++) 
       { 
      if(tabss[n] != safari.application.activeBrowserWindow.activeTab) 

     tabss[n].close(); 
      } 
    ) 

我想为Chrome的相同不活动标签页。但Chrome有不同的做事方式。我仍然想要在选项卡的索引上运行循环,如果它们不是选定的选项卡,请关闭它们。我已经能够获得窗口索引的长度,但我不知道如何执行多次关闭的选项卡循环,以确保不会关闭选定的选项卡。我已经能够通过这样做得到长度:

<html> 
    <head> 
    <script> 
    var targetWindow = null; 
    var tabCount = 0; 

    function start(tab) { 
     chrome.windows.getCurrent(getWindows); 
    } 

    function getWindows(win) { 
     targetWindow = win; 
     chrome.tabs.getAllInWindow(targetWindow.id, getTabs); 
    } 

    function getTabs(tabs) { 
     tabCount = tabs.length; 
     alert(tabCount); 

    } 

    // Set up a click handler so that we can merge all the windows. 
    chrome.browserAction.onClicked.addListener(start); 
    </script> 
    </head> 
</html> 

http://code.google.com/chrome/extensions/samples.html收集合并Windows。

现在我想运行循环,但我不知道如何告诉循环不要关闭选定的选项卡。我正在考虑让循环比较循环选项卡和选定窗口的选项卡ID,并且它不会关闭它并移动到循环中的下一个选项卡索引编号。

喜欢的东西:

(
      for (n=0; n<tabCount; n++) 
       { 
      if(chrome.tabs[n].id != tab.id) 

     chrome.tabs[n].remove(); 
      } 
) 

但我不知道如何注入电流tabid因为所有的回调函数有这个JavaScript的黑客/小白难住了。我无法从我理解的其他函数中引入变量。

回答

1

这应做到:

// when a browser action is clicked, the callback is called with the current tab 
chrome.browserAction.onClicked.addListener(function(curtab) 
{ 
    // get the current window 
    chrome.windows.getCurrent(function(win) 
    { 
     // get an array of the tabs in the window 
     chrome.tabs.getAllInWindow(win.id, function(tabs) 
     { 
      for (i in tabs) // loop over the tabs 
      { 
       // if the tab is not the selected one 
       if (tabs[i].id != curtab.id) 
       { 
        // close it 
        chrome.tabs.remove(tabs[i].id) 
       } 
      } 
     }); 
    }); 
}); 
+0

哇。快速敲下这些标签。谢谢。我昨晚花了几个小时试图学习这些东西/弄明白了。大声笑。谢谢。 – Dave 2010-12-08 21:12:57