2015-04-02 59 views
1

这主要参考Firefox add-on SDK: Get http response headers。我正在尝试记录对http请求的响应,但到目前为止,我没有运气调试这个。真的会感激一些建议......一直在我的头上打了几个小时,没有任何意见。Firefox SDK监控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(或任何url)时,没有任何东西会记录到控制台。

我将我的main.js与建议的技巧进行了比较@https://developer.mozilla.org/en-US/docs/Setting_HTTP_request_headers并且一切看起来都是一致的。最后,我打算做一些比记录事情更复杂的事情,但我甚至无法使这些基本功能正常工作!

回答

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') { ... } 
+0

感谢您的回复,你说得对,我的mixup瓦特/响应..但为什么我的代码至少没有记录请求? – user2426679 2015-04-03 16:22:26

+0

对于请求您必须设置“http-on-modify-request”的观察者 - 注意单词“修改” – lexasss 2015-04-03 16:30:42