簡體   English   中英

mozrepl:遍歷firefox所有窗口中的所有選項卡

[英]mozrepl: loop through all tabs in all windows of firefox

我知道當我進入mozrepl會話時,我處於一個特定瀏覽器窗口的上下文中。 在那個窗口我能做到

var tabContainer = window.getBrowser().tabContainer;
var tabs = tabContainer.childNodes;

這將在該窗口中為我提供一系列選項卡。 我需要在所有打開的Firefox窗口中獲取所有選項卡的數組,我該怎么做?

我不確定它是否適用於mozrepl,但在Firefox附加組件中,您可以執行類似以下代碼的操作。 此代碼將在所有打開的瀏覽器窗口中循環。 為每個窗口調用一個函數,在本例中為doWindow

Components.utils.import("resource://gre/modules/Services.jsm");
function forEachOpenWindow(fn)  {
    // Apply a function to all open browser windows

    var windows = Services.wm.getEnumerator("navigator:browser");
    while (windows.hasMoreElements()) {
        fn(windows.getNext().QueryInterface(Ci.nsIDOMWindow));
    }
}

function doWindow(curWindow) {
    var tabContainer = curWindow.getBrowser().tabContainer;
    var tabs = tabContainer.childNodes;
    //Do what you are wanting to do with the tabs in this window
    //  then move to the next.
}

forEachOpenWindow(doWindow);

您可以創建一個包含所有當前選項卡的數組,只需讓doWindow將從tabContainer.childNodes獲取的任何選項卡添加到整個列表中。 我在這里沒有這樣做,因為你從tabContainer.childNodes獲得的是一個實時集合 ,你還沒有說明你如何使用該數組。 您的其他代碼可能會或可能不會假設列表是活動的。

如果您肯定希望所有選項卡都在一個數組中,那么您可以將doWindow變為以下內容:

var allTabs = [];
function doWindow(curWindow) {
    var tabContainer = curWindow.getBrowser().tabContainer;
    var tabs = tabContainer.childNodes;
    //Explicitly convert the live collection to an array, then add to allTabs
    allTabs = allTabs.concat(Array.prototype.slice.call(tabs));
}

注意:循環瀏覽窗口的代碼最初取自將舊的基於疊加層的Firefox擴展轉換為無重新啟動的插件 ,作者將其重新編寫為如何將覆蓋擴展轉換為在MDN 上無重啟的初始部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM