2010-09-21 45 views
1

在Firefox擴展中,我暫時將數據保存在JavaScript中以顯示在側邊欄中。當選中標籤時,我想將側邊欄中的文本更改爲正確的數據。什麼是最好的對象用作每個標籤的「標識符」?我試圖使用標籤對象。但是,我堅持如何將數據關聯到選項卡。目前,我在一個選項卡中的文檔上得到一個加載事件。然後我處理文檔中的數據。在顯示標籤中的新數據之後,我想將其保存到地圖中,並將選項卡作爲關鍵字,以便我可以在SelectTab事件中檢索它。但是,我不確定如何在加載事件中查找文檔中的選項卡。Mozilla擴展 - 每個打開的標籤跟蹤數據

所以,請告訴我如何從文檔對象中獲取標籤對象,或者使用更好的東西作爲關鍵。

以下是我找到的一種方法,但我確定有更優雅的東西。

getTabForDocument : function (document) { 
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] 
        .getService(Components.interfaces.nsIWindowMediator); 
    for (var found = false, index = 0, tabbrowser = wm.getEnumerator('navigator:browser').getNext().gBrowser; 
     index < tabbrowser.tabContainer.childNodes.length && !found; 
     index++) { 

    // Get the next tab 
    var currentTab = tabbrowser.tabContainer.childNodes[index]; 

     if (currentTab.linkedBrowser.contentWindow.document == document) 
    { 
     return currentTab; 
    } 
    } 
    return null; 
}, 

謝謝...

回答

2

在看看:https://developer.mozilla.org/en/Code_snippets/Tabbed_browser,尤其是gBrowser.selectedBrowser部分和https://developer.mozilla.org/En/Listening_to_events_on_all_tabs

將數據與一個標籤,你可以使用setUserData方法上的關聯瀏覽器。

要獲得文檔的瀏覽器,請在gBrowser global對象上使用getBrowserForDocument方法。

+0

這有助於解決存儲問題。您是否也有辦法輕鬆從文檔中獲取瀏覽器? – Andrew 2010-09-23 12:54:25

+0

我編輯了我的答案,並提供瞭如何做到這一點的信息。 – 2010-09-23 14:29:03