Firefox extension observing response

一笑奈何 提交于 2020-02-02 15:03:09

问题


I am trying using code

// This is an active module of the goelvivek (8) Add-on
exports.main = function() {
  var httpRequestObserver =  
  {  
    observe: function(subject, topic, data)   
    { 
      if (topic == "http-on-examine-response") {  
        if(console)
          console.log(data);
      }  
    }  
  }; 
  var {Cc, Ci, Cr} = require("chrome");
  var observer = require("observer-service");   
  observerService = Components.classes["@mozilla.org/observer-service;1"].  
    getService(Components.interfaces.nsIObserverService);  
    observerService.addObserver(httpRequestObserver, "http-on-examine-response", false);
};

but line console.log(data); is not printing any thing in console log. why ?


回答1:


In addition to the issue noted by Nickolay, an observer needs to implement a QueryInterface() function (typically by means of XPCOMUtils.generateQI()). Here is how one would do it with the Add-on SDK:

var {Cc, Ci, Cr, Cu} = require("chrome");
var {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm");
var httpRequestObserver =
{
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
  ...
};

However, since you already require observer-service package, it would be easier to use it:

var observer = require("observer-service");
observer.add("http-on-examine-response", onHttpRequest);

function onHttpRequest(subject, data)
{
  ...
}

The downside of this approach is that observer-service is an internal package and its API might change in future Add-on SDK versions.




回答2:


Is it the real snippet? You should see an error about Components being undefined in the Error Console. Either get it from require('chrome') or use the object from require("observer-service").



来源:https://stackoverflow.com/questions/7812604/firefox-extension-observing-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!