Firefox 扩展观察响应
我正在尝试使用代码
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 Nickolay 指出的问题之外,观察者还需要实现 QueryInterface() 函数(通常通过 XPCOMUtils.generateQI())。以下是使用附加 SDK 的方法:
但是,由于您已经需要
observer-service
包,使用它会更容易:这种方法的缺点是
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:However, since you already require
observer-service
package, it would be easier to use it:The downside of this approach is that
observer-service
is an internal package and its API might change in future Add-on SDK versions.这是真实的片段吗?您应该在错误控制台中看到有关
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 fromrequire('chrome')
or use the object fromrequire("observer-service")
.