2012-08-24 17 views
1

我正在開發Firefox的Addon SDK(v1.9)上的擴展。我的擴展通過實現nsIContentPolicy來阻止或允許資源,並根據要阻止的URI數據庫測試它們的URI。如何從nsIContentPolicy的加載資源中獲取標籤shouldLoad

問題

我需要有從nsIContentPolicy的shouldLoad功能訪問的標籤對象(如果可用)。

我假設這個部分是用於nsISupports的shouldLoad函數的「context」參數。我嘗試過使用getTabForWindow(win),因爲上下文不是nsIDOMWindow(Identify tab that made request in Firefox Addon SDK

回答

2

context參數是文檔或元素。從那裏獲取到窗口並不難:

var {Ci} = require("chrome"); 
if (!(context instanceof Ci.nsIDOMWindow)) 
{ 
    // If this is an element, get the corresponding document 
    if (context instanceof Ci.nsIDOMNode && context.ownerDocument) 
    context = context.ownerDocument; 

    // Now we should have a document, get its window 
    if (context instanceof Ci.nsIDOMDocument) 
    context = context.defaultView; 
    else 
    context = null; 
} 

// If we have a window now - get the tab 
if (context) 
{ 
    var tabsLib = require("tabs/tab.js"); 
    return tabsLib.getTabForWindow(context.top); 
} 
else 
    return null; 

參考:NodeDocumentwindow

+0

再次感謝您的幫助。當我試圖解決這個問題時,它不起作用的原因是因爲我試圖從main_frame(TYPE_DOCUMENT)資源加載的上下文中獲取選項卡。而這仍然無法這樣做,是否有可能從main_frame資源獲取標籤? – josesigna

+0

所以我想出main_frames(TYPE_DOCUMENT)有一個XULElement作爲上下文,所以我們通過調用context._contentWindow.top來獲得窗口對象,當它與getTabForWindow()一起使用時,它提供了正確的Tab對象。因此,要發佈完整答案,問題應該是如何測試XULElement的上下文。 – josesigna

+0

或者我們可以測試context._contentWindow!== undefined – josesigna