关闭浏览器中其他选项卡的最快方法

时间:2015-06-30 10:08:04

标签: firefox firefox-addon firefox-addon-sdk

如何快速关闭Firefox中的“其他”标签?我目前正在做以下事情:

var tabs = windows.activeWindow.tabs
for (let n in tabs) {
  let tab = tabs[n];
  tab.close();
}

但是当用户打开许多标签时,这种方法非常缓慢。

1 个答案:

答案 0 :(得分:1)

这就是我这样做的方式,但它可能与上面的方法具有相同的速度。

请打开scrtachpad,将环境设置为浏览器,然后运行此代码告诉我速度。

console.time('time to close all other tabs');
var cTabIndex = gBrowser.selectedTab._tPos;
var cntTabs = gBrowser.tabContainer.childNodes.length;

// close tabs to right
for (var i=cTabIndex+1; i<cntTabs; i++) {
    gBrowser.removeTab(gBrowser.tabContainer.childNodes[i]);
}

// close tabs to left
for (var i=0; i<cTabIndex; i++) {
    gBrowser.removeTab(gBrowser.tabContainer.childNodes[i]);
}
console.timeEnd('time to close all other tabs');

我基于这项工作的另一种方法是:

https://stackoverflow.com/a/26744281/5062337

这种方法的作用是将当前标签移出一个新窗口,并关闭旧窗口:

function moveTabToWin(aTab, tDOMWin) {
  //tDOMWin means target DOMWindow means the window you want the tab in
  //if tDOMWin == 'tabbed' or == 'non-tabbed' it opens in a new window
  //if aTopContWin is the last in its window, then its window is closed
  if (tDOMWin == 'tabbed' || tDOMWin == 'non-tabbed') {
    var sa = Cc["@mozilla.org/supports-array;1"].createInstance(Ci.nsISupportsArray);
    var wuri = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
    wuri.data = 'about:blank';
    sa.AppendElement(wuri);
    let features = "chrome,dialog=no";
    if (tDOMWin == 'tabbed') {
      features += ',all';
    }
    var sDOMWin = aTab.ownerGlobal; //source DOMWindow
    if (PrivateBrowsingUtils.permanentPrivateBrowsing || PrivateBrowsingUtils.isWindowPrivate(sDOMWin)) {
       features += ",private";
    } else {
       features += ",non-private";
    }
    var XULWindow = Services.ww.openWindow(null, 'chrome://browser/content/browser.xul', null, features, sa);
    XULWindow.addEventListener('load', function() {
      var DOMWindow = XULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
      DOMWindow.gBrowser.selectedTab.linkedBrowser.webNavigation.stop(Ci.nsIWebNavigation.STOP_ALL);
      var oldWin = aTab.ownerDocument.defaultView;
      DOMWindow.gBrowser.swapBrowsersAndCloseOther(DOMWindow.gBrowser.selectedTab, aTab);
      //DOMWindow.gBrowser.selectedTab = newTab;
      oldWin.close();
      console.timeEnd('time to move tab to new win and close old win');
    }, false);
  } else if (tDOMWin) {
    //existing dom window
    var newTab = tDOMWin.gBrowser.addTab('about:blank');
    var oldWin = aTab.ownerDocument.defaultView;
    newTab.linkedBrowser.webNavigation.stop(Ci.nsIWebNavigation.STOP_ALL);
    tDOMWin.gBrowser.swapBrowsersAndCloseOther(newTab, aTab);
    tDOMWin.gBrowser.selectedTab = newTab;
    oldWin.close();
    console.timeEnd('time to move tab to new win and close old win');
  }
}

console.time('time to move tab to new win and close old win');

var oldWin = Services.wm.getMostRecentWindow('navigator:browser');
moveTabToWin(oldWin.gBrowser.selectedTab, 'tabbed');

他们两个都附有计时器,两者都可以从暂存器运行,请分享时间。新窗口对我来说大约需要1秒钟,但是没有像第一种方法那样有大量标签的冻结。