2016-07-25 30 views
0

我正在捕獲Firefox插件SDK擴展中的HTTP請求。我需要獲取與請求關聯的DOM窗口。但是,我收到一個NS_NOINTERFACE錯誤。Firefox插件獲取發出HTTP請求的DOM窗口

這裏是我的代碼:

var httpRequestObserver = { 
    observe: function (subject, topic, data) { 
     var httpRequest = subject.QueryInterface(Ci.nsIHttpChannel); 
     var requestUrl = subject.URI.host; 
     var domWin; 
     var assWindow; 
     console.log('URL: ', requestUrl); 
     try { 
     domWin = httpRequest.notificationCallbacks.getInterface(Ci.nsIDOMWindow); 
     assWindow = httpChannel.notificationCallbacks.getInterface(Ci.nsILoadContext) 
           .associatedWindow; 
     console.log(domWin); 
     } catch (e) { 
     console.log(e); 
     } 
     // console.log('TAB: ', tabsLib.getTabForWindow(domWin.top)); 

     var hostName = wn.domWindow.getBrowser().selectedBrowser.contentWindow.location.host; 
     console.log('HOST: ', hostName); 
    }, 

    get observerService() { 
     return Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService); 
    }, 

    register: function() { 
     this.observerService.addObserver(this, 'http-on-modify-request', false); 
    }, 

    unregister: function() { 
     this.observerService.removeObserver(this, 'http-on-modify-request'); 
    } 
    }; 

    httpRequestObserver.register(); 

我都試過nsIDOMWindownsILoadContext,但NS_NOINTERFACE錯誤總是試圖得到window對象出現。

+0

*請*,[編輯]問題把控制檯消息信息**作爲文本,包含在'代碼塊**「中。如果錯誤生成了文本無法傳達的內容,那麼錯誤的圖像可能會有用。但是,在大多數情況下,擁有控制檯消息的圖像顯着比將其作爲文本更有用。以文本格式時,可以複製,粘貼和搜索。作爲文本,它們在回答這個問題和爲將來尋找類似問題的答案的人們提供了更多的幫助。 – Makyen

+0

由於沒有將問題中包含的錯誤信息作爲文本進行投票,因此我投了票並投了票。如果您將錯誤信息作爲文本輸入到問題中,我將刪除我的倒票並收回我的近距離投票。在這種情況下,我可能會投票。請用'@ Makyen'發表評論,以便我收到變更通知。 – Makyen

+0

@Makyen謝謝你的幫助,更新。 –

回答

1

我終於設法讓我通過

httpRequest.notificationCallbacks.getInterface(Ci.nsILoadContext).topFrameElement 

例如,需要獲得該開始請求的文檔的URL數據,我用

httpRequest.notificationCallbacks.getInterface(Ci.nsILoadContext).topFrameElement._documentURI.href 
+1

棒極了!以下是一些可能有所幫助的文檔 - https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Tabbed_browser#Getting_the_browser_that_fires_the_http-on-modify-request_notification_(example_code_updated_for_loadContext) – Noitidart

+1

請注意,雖然這不是很好e10s友好。當你在流程管理員工作。最好從每個選項卡的framescript中使用nsIWebProgressListener - https://github.com/Noitidart/Full-Stop/blob/master/resources/scripts/framescript.js – Noitidart

+0

可以在不使用框架腳本的情況下獲取URI。但觸摸dom窗口將需要在e10s下。 – the8472

0

既然你已經找到了如何從請求中獲取<browser>可以執行以下操作以獲取SDK API:

let browser = ....topFrameElement 
let xulTab = browser.ownerDocument.defaultView.gBrowser.getTabForBrowser(browser) 
let sdkTab = modelFor(xulTab) 

modelFor()記錄在選項卡模塊中。