这主要是参考Firefox add-on SDK: Get http response headers。我正在尝试记录对http请求的响应,但到目前为止,我没有运气调试这个。真的很感激一些建议......几个小时以来一直在敲打我的脑袋而无处可去。
这是我的小扩展程序的main.js:
var {Cc, Ci} = require("chrome");
var httpRequestObserver =
{
init: function() {
var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
observerService.addObserver( this, 'http-on-examine-response', false );
observerService.addObserver( this, 'http-on-examine-cached-response', false );
observerService.addObserver( this, 'http-on-examine-merged-response', false );
},
observe: function(subject, topic, data) {
if (topic === 'http-on-examine-response' || topic === 'http-on-examine-cached-response' || topic === 'http-on-examine-merged-response' ) {
subject.QueryInterface(Ci.nsIHttpChannel);
this.onExamineResponse(subject);
}
},
onExamineResponse: function(oHttp) {
try
{
console.log( oHttp.URI.spec );
}
catch(err)
{
console.log(err);
}
}
};
当我执行firefox附加sdk时,测试浏览器启动正常,如果我打开工具>加载项>扩展,我看到我的列表。但是,当我将测试浏览器指向google.com(或任何网址)时,没有任何内容记录到控制台。
我将main.js与@ https://developer.mozilla.org/en-US/docs/Setting_HTTP_request_headers建议的技术进行了比较,一切似乎都是一致的。最后,我计划做一些比记录事情更复杂的事情,但我甚至无法使这些基本功能正常工作!
答案 0 :(得分:2)
似乎您为请求设置了观察者,而不是响应(http-on-examine-request)。
在init函数中使用它:
observerService.addObserver( this, 'http-on-examine-response', false );
observerService.addObserver( this, 'http-on-examine-cached-response', false );
observerService.addObserver( this, 'http-on-examine-merged-response', false );
同样,在观察功能中使用它:
if (topic === 'http-on-examine-response' || topic === 'http-on-examine-cached-response' || topic === 'http-on-examine-merged-response' ) { ... }