Firefox 扩展观察响应

发布于 2024-12-10 12:13:08 字数 761 浏览 0 评论 0原文

我正在尝试使用代码

// 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);
};

,但行 console.log(data); 没有在控制台日志中打印任何内容。为什么 ?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-12-17 12:13:08

除了 Nickolay 指出的问题之外,观察者还需要实现 QueryInterface() 函数(通常通过 XPCOMUtils.generateQI())。以下是使用附加 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]),
  ...
};

但是,由于您已经需要 observer-service,使用它会更容易:

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

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

这种方法的缺点是observer-service 是一个内部包,其 API 可能会在未来的 Add-on SDK 版本中发生变化。

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.

别低头,皇冠会掉 2024-12-17 12:13:08

这是真实的片段吗?您应该在错误控制台中看到有关 Components 未定义的错误。要么从 require('chrome') 获取它,要么使用 require("observer-service") 中的对象。

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").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文