问题描述
$ b
class =lang-js prettyprint-override> //这是goelvivek的一个活动模块8)附加
exports.main = function(){
var httpRequestObserver =
{
观察:函数(主题,主题,数据)
{
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);
不在控制台日志中打印任何东西。为什么?
除了Nickolay提到的问题,观察者需要实现一个 QueryInterface )
函数(通常通过)。下面介绍一下如何使用附加组件SDK:
var {Cc,Ci ,Cr,Cu} =要求(铬);
var {XPCOMUtils} = Cu.import(resource://gre/modules/XPCOMUtils.jsm);
var httpRequestObserver =
{
QueryInterface:XPCOMUtils.generateQI([Ci.nsIObserver,Ci.nsISupportsWeakReference]),
...
};
但是,由于您已经需要,它会更容易使用它:
var observer = require(observer -服务);
observer.add(http-on-examine-response,onHttpRequest);
函数onHttpRequest(主题,数据)
{
...
}
这个方法的缺点是 observer-service
是一个内部包,它的API可能会在将来的附加SDK版本中改变。
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 ?
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.
这篇关于Firefox扩展观察响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!