In my firefox extension, onUninstalled event doesn

2019-02-19 05:04发布

问题:

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.

回答1:

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