Firefox扩展可以自行卸载吗?

时间:2016-08-25 20:52:09

标签: javascript firefox-addon uninstall

我发现此处和扩展程序 A 能够卸载扩展程序 B Uninstall/Remove Firefox Extension programmatically?

我想知道相同的方法是否也适用于卸载自身的扩展(例如,如果它感知到取代它的超级扩展)?以下代码是我对上述相关问题的答案的简单改编:

Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("supersedes-me@id", function(betteraddon) {
   if (betteraddon) {
      AddonManager.getAddonById("my-own@id", function(thisaddon) {
         if (!thisaddon) {
            // this Add-on not present? Should not happen ...
            return;
         }
         if (!(thisaddon.permissions & AddonManager.PERM_CAN_UNINSTALL)) {
            // Add-on cannot be uninstalled
            return;
         }
         thisaddon.uninstall();
         if (thisaddon.pendingOperations & AddonManager.PENDING_UNINSTALL) {
            // Need to restart to finish the uninstall.
            // Might ask the user to do just that. Or not ask and just do.
            // Or just wait until the browser is restarted by the user.
         }
      });
   }
});

这听起来很危险,因为自卸卸插件至少等待从自己的卸载中恢复通话... 但这种方法真的如此危险吗?毕竟,现在卸载甚至可以撤销,这意味着卸载的插件暂时“仍然存在”......?

1 个答案:

答案 0 :(得分:2)

是的,扩展程序可以使用AddonManager.jsm自行卸载。您可以使用Addon获取getAddonByID()对象,该对象具有uninstall()方法。换句话说,或多或少地在你的问题中对它进行编码。

下面是一个卸载自己的附加SDK扩展。安装后,它会短暂显示在about:addons中,但随后会消失。从 .xpi 文件安装时,它在控制台中生成以下输出:

uninstallself:My ID=@uninstallself
uninstallself:This add-on is being loaded for reason= install
uninstallself:Going to uninstall myself
uninstallself:This add-on is being unloaded for reason= uninstall
uninstallself:Called uninstall on myself

如果使用jpm run调用,它还会输出另外两行错误,表明无法删除文件。该文件是由jpm run创建的临时 .xpi 文件。

的package.json

{
    "title": "Test Self Uninstall",
    "name": "uninstallself",
    "version": "0.0.1",
    "description": "Test an add-on uninstalling itself",
    "main": "index.js",
    "author": "Makyen",
    "engines": {
        "firefox": ">=38.0a1",
        "fennec": ">=38.0a1"
    },
    "license": "MIT",
    "keywords": [
        "jetpack"
    ]
}

index.js

/* Firefox Add-on SDK uninstall self */

const { AddonManager } = require("resource://gre/modules/AddonManager.jsm");
var self = require("sdk/self");
var myId = self.id;

var utils = require('sdk/window/utils');
activeWin = utils.getMostRecentBrowserWindow();
// Using activeWin.console.log() is needed to have output to the
// Browser Console when installed as an .xpi file.  In that case,
// console is mapped to dump(), which does not output to the console. 
// This is done to not polute the console from SDK add-ons that are
// logging information when they should not.  Using jpm run,
// console.log outputs to the Browser Console.

activeWin.console.log('My ID=' +myId);

exports.main = function (options) {
activeWin = utils.getMostRecentBrowserWindow();
    activeWin.console.log('This add-on is being loaded for reason=', options.loadReason);
};

exports.onUnload = function (reason) {
    activeWin.console.log('This add-on is being unloaded for reason=',reason);
};

AddonManager.getAddonByID(myId,addon => {
    if(addon && typeof addon.uninstall === 'function'){
        activeWin.console.log('Going to uninstall myself');
        addon.uninstall();
        activeWin.console.log('Called uninstall on myself');
    }
});