我在Firefox Add-on SDK扩展中捕获HTTP请求。我需要获得与请求关联的DOM窗口。但是,我收到NS_NOINTERFACE
错误。
这是我的代码:
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();
我已尝试nsIDOMWindow
和nsILoadContext
,但尝试获取NS_NOINTERFACE
对象时始终显示window
错误。
答案 0 :(得分:1)
我终于设法通过
获取了我需要的数据httpRequest.notificationCallbacks.getInterface(Ci.nsILoadContext).topFrameElement
例如,要获取启动请求的文档的URL,我使用了
httpRequest.notificationCallbacks.getInterface(Ci.nsILoadContext).topFrameElement._documentURI.href
答案 1 :(得分:0)
由于您已经找到了如何从请求中获取<browser>
,因此您可以执行以下操作以返回SDK API:
let browser = ....topFrameElement
let xulTab = browser.ownerDocument.defaultView.gBrowser.getTabForBrowser(browser)
let sdkTab = modelFor(xulTab)
标签模块中记录了