firefox插件:无法侦听firefox“quit-application”事件 [英] firefox addon: not able to listen firefox "quit-application" event
问题描述
观察:函数(主题,主题,数据)我的代码片段在firefox退出后启用了扩展。 {
)并注册您的观察者
if(topic ==quit-application){
LOG(in quit-application Testing);
var os = Components.classes [@ mozilla.org/observer-service;1]
.getService(Components.interfaces.nsIObserverService);
//os.addObserver(this,http-on-examine-cached-response,false);
os.addObserver(this,quit-application,false);
var appInfo = Components.classes [@ mozilla.org/xre/app-info;1]
.getService(Components.interfaces.nsIXULAppInfo);
var tempappVersion = appInfo.version;
var appVersion = tempappVersion.split(。);
//在插件上为FF4及更高版本添加附加侦听器dsable事件。
if(appVersion [0]> = 4){
setAddonEnableListener();
LOG(\NAP-启动测试从JavaScript文件....);
}
return;
$ b $setAddonEnableListener
我试图启用这样的扩展:
pre $函数setAddonEnableListener(){
尝试{
alert(setAddonEnableListener akbar nsListener called from);
Components.utils.import(resource://gre/modules/AddonManager.jsm);
AddonManager.getAddonByID(somename@extension.com,function(addon)
{
if(addon.userDisabled)
addon.userDisabled = false;
} );
$ catch
$ b我注册了
quit-application
事件var
像这样:myModule = {
registerSelf:function(compMgr,fileSpec,location,type){
var compMgr = compMgr.QueryInterface(Components.interfaces .nsIComponentRegistrar);
compMgr.registerFactoryLocation(this.myCID,
this.myName,
this.myProgID,
fileSpec,
location,
type);
var catMgr = Components.classes [@ mozilla.org/categorymanager;1\"].getService(Components.interfaces.nsICategoryManager);
catMgr.addCategoryEntry(app-startup,this.myName,this.myProgID,true,true);
catMgr.addCategoryEntry(quit-application,this.myName,this.myProgID,true,true);
当Firefox退出时,
/ code>消息没有得到显示。我在这里做错了什么?
解决方案没有类别
quit-application
。您应该从Firefox 4开始接收app-startup
通知(或者更确切地说,是code> change-after-changequit-application :
$ b观察:function(subject,topic,data){
if(topic ==app-startup|| topic ==profile-after-change){
var observerService = Components.classes [ @ mozilla.org / observer-service; 1]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this,quit-application,false);
else if(topic ==quit-application){
var observerService = Components.classes [@ mozilla.org/observer-service;1]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this,quit-application);
...
$ / code>
引用文档:
<除非另有说明,否则您使用nsIObserverService注册主题。
没有关于通过类别管理器注册关于任何关机通知。
顺便说一句,我诚恳地推荐 XPCOMUtils 进行组件注册。您不需要自己编写模块定义。
I have the following code snippet to enable extension after firefox quit,
observe: function (subject, topic, data) {
if (topic == "quit-application") {
LOG("inside quit-application Testing ");
var os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
//os.addObserver(this, "http-on-examine-cached-response", false);
os.addObserver(this, "quit-application", false);
var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
.getService(Components.interfaces.nsIXULAppInfo);
var tempappVersion = appInfo.version;
var appVersion = tempappVersion.split(".");
// adding add-on listener dsable event on add-on for FF4 and later versions.
if (appVersion[0] >= 4) {
setAddonEnableListener();
LOG("\napp-startup Testing from javascript file....");
}
return;
}
}
And inside the setAddonEnableListener
I am trying to enable the extension like this:
function setAddonEnableListener() {
try {
alert("setAddonEnableListener akbar nsListener called from ");
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("somename@extension.com", function(addon)
{
if (addon.userDisabled)
addon.userDisabled = false;
});
} catch (ex) {
}
}
And I am registering the quit-application
event var
like this:
myModule = {
registerSelf: function (compMgr, fileSpec, location, type) {
var compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
compMgr.registerFactoryLocation(this.myCID,
this.myName,
this.myProgID,
fileSpec,
location,
type);
var catMgr = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
catMgr.addCategoryEntry("app-startup", this.myName, this.myProgID, true, true);
catMgr.addCategoryEntry("quit-application", this.myName, this.myProgID, true, true);
}
When Firefox quits, inside quit-application Testing
message is not getting displayed. What am I doing wrong here?
There is no category quit-application
. You should receive app-startup
notification (or rather profile-after-change
starting with Firefox 4) and register your observer for quit-application
:
observe: function (subject, topic, data) {
if (topic == "app-startup" || topic == "profile-after-change") {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "quit-application", false);
}
else if (topic == "quit-application") {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "quit-application");
...
}
}
To quote the documentation:
Unless otherwise noted you register for the topics using the nsIObserverService.
And there is no note about registration via category manager on any shutdown notifications.
Btw, I sincerely recommend XPCOMUtils for component registration. You shouldn't need to write the module definition yourself.
这篇关于firefox插件:无法侦听firefox“quit-application”事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!