Javascript Firefox扩展可以自行卸载吗?

Javascript Firefox扩展可以自行卸载吗?,javascript,firefox-addon,uninstallation,Javascript,Firefox Addon,Uninstallation,我发现这是扩展A可以卸载扩展B的地方: 我想知道同样的方法是否也适用于卸载自身的扩展(例如,如果它检测到一个高级扩展取代了它)?以下代码是我对上述链接问题答案的简单改编: Components.utils.import("resource://gre/modules/AddonManager.jsm"); AddonManager.getAddonByID("supersedes-me@id", function(betteraddon) { if (betteraddon) {

我发现这是扩展A可以卸载扩展B的地方:

我想知道同样的方法是否也适用于卸载自身的扩展(例如,如果它检测到一个高级扩展取代了它)?以下代码是我对上述链接问题答案的简单改编:

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.
         }
      });
   }
});
这听起来很危险,因为自卸载插件至少正在等待从其自身卸载返回调用。。。
但这种做法真的如此危险吗?毕竟,现在卸载甚至是可以撤销的,这意味着卸载的插件“仍然存在”一段时间

是的,扩展可以使用卸载自己。您可以使用获取一个对象,该对象具有一个方法。换句话说,在你的问题中你或多或少是如何编码的

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

uninstallself:My ID=@uninstallself
卸载自我:正在加载此加载项,原因是=安装
卸载自我:要卸载我自己吗
卸载自我:正在卸载此加载项,原因是=卸载
卸载自我:对我自己调用卸载
如果使用
jpm run
调用,它还会输出另外两行错误,指示无法删除文件。该文件是由
jpm run
创建的临时.xpi文件

package.json:

index.js:


是的,扩展可以使用卸载自己。您可以使用获取一个对象,该对象具有一个方法。换句话说,在你的问题中你或多或少是如何编码的

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

uninstallself:My ID=@uninstallself
卸载自我:正在加载此加载项,原因是=安装
卸载自我:要卸载我自己吗
卸载自我:正在卸载此加载项,原因是=卸载
卸载自我:对我自己调用卸载
如果使用
jpm run
调用,它还会输出另外两行错误,指示无法删除文件。该文件是由
jpm run
创建的临时.xpi文件

package.json:

index.js:


听起来你的问题只要试一下就可以回答。那么,当你尝试它时发生了什么?@Makyen当我在自己的机器上在自己的环境中尝试一次时,它仍然会在我无法控制的情况下失败(例如,比赛条件)。听起来你的问题可以通过尝试来回答。那么,当你尝试它时发生了什么?@Makyen当我在自己的机器上在自己的环境中尝试它一次时,它仍然会在我无法控制的情况下失败(例如,比赛条件)
{
    "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"
    ]
}
/* 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');
    }
});