在我的Firefox扩展中,onUninstalled事件似乎不会触发 [英] In my firefox extension, onUninstalled event doesn't seem to fire

查看:117
本文介绍了在我的Firefox扩展中,onUninstalled事件似乎不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firefox 4公开了一些事件来检测用户何时卸载扩展。我可以获取 onUninstalling 来触发(当用户点击删除扩展名时),但是当 onUninstalled fires(这应该在重新启动后发生)。

$ p $ Components.utils.import(resource:// gre /modules/AddonManager.jsm);
AddonManager.addAddonListener({
onUninstalled:function(addon){
//不工作
alert(uninstalled!);
},
onUninstalling:function(addon){
//按预期工作
alert(uninstalling!);
}
});

我最终希望在用户卸载我的扩展后显示一个页面。

解决方案

方法 onUninstalled 在扩展名被删除之后调用 - 所以你的扩展名不可能收到它自己的卸载事件,它不再接收它。但是,您可以获取有关正在卸载的其他附加组件的通知,这就是此事件的要点。看看源代码,似乎有一个额外的捕获 - 这个通知只发送加载项,不需要重新启动。这是有道理的,因为重新启动可以从根本上改变情况,因为外部应用程序添加或删除扩展目录中的附加组件。因此,Firefox甚至不会尝试传达在未运行时发生的更改,任何侦听器都应该重新读取每个浏览器启动时的加载项列表。



<总而言之,作为一个经典的扩展,你必须使用 onUninstalling ,即使这个事件不能保证扩展将被卸载(用户仍然可以恢复他的选择)。引导式扩展(不需要重新启动的扩展)请参阅 https://developer.mozilla.org / en / Extensions / Bootstrapped_extensions ),它们的bootstrap.js中的方法 uninstall()将会被调用,即使扩展被禁用。然而,把你的扩展转换成自举的扩展并不总是那么容易,有一些附加的东西: http://adblockplus.org/blog/how-many-hacks-does-it-take-to-make-你的扩展安装没有重新启动


Firefox 4 exposes some events to detect when the user is uninstalling an extension. I can get onUninstalling to fire (when the user clicks to remove an extension), but I can't get anything to happen when onUninstalled fires (which should occur after the restart).

Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.addAddonListener({
    onUninstalled: function(addon){
        //does not work
        alert("uninstalled!");
    },
    onUninstalling: function(addon){
        //works as expected
        alert("uninstalling!");   
    }
  });

I'm ultimately hoping to show a page after the user uninstalls my extension. I'd prefer to do it after onUninstalled if I could.

解决方案

Method onUninstalled is called after the extension has been removed - so your extension cannot possibly receive its own uninstall event, it is no longer around to receive it. You can get the notification about other add-ons being uninstalled however, that's the point of this event. Looking at the source code, there appears to be an additional catch - this notification is only sent for add-ons that don't require a restart. This makes sense given that a restart can change the situation radically because external applications added or removed add-ons in the extensions directory. So Firefox doesn't even attempt to communicate changes that happened while it wasn't running, any listeners should simply re-read the list of add-ons on each browser start.

To sum up, as a "classic" extension you have to go with onUninstalling even though this event doesn't guarantee that the extension will be uninstalled (the user can still revert his choice). Bootstrapped extensions (the ones that don't require a restart, see https://developer.mozilla.org/en/Extensions/Bootstrapped_extensions) have it easier, method uninstall() in their bootstrap.js will be called even if the extension is disabled. However, turning your extension into a bootstrapped one isn't always easy, there is a number of catches attached to that: http://adblockplus.org/blog/how-many-hacks-does-it-take-to-make-your-extension-install-without-a-restart

这篇关于在我的Firefox扩展中,onUninstalled事件似乎不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆