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

问题描述 投票:0回答:1

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

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

但是当用户打开许多选项卡时,此方法非常慢。

firefox firefox-addon firefox-addon-sdk
1个回答
1
投票

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

请打开暂存器,将环境设置为浏览器,然后运行此代码以让我知道速度。

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 秒,但没有像第一种方法那样有很多选项卡那样冻结。

© www.soinside.com 2019 - 2024. All rights reserved.