gpt4 book ai didi

javascript - Firefox 扩展可以自行卸载吗?

转载 作者:行者123 更新时间:2023-12-02 14:14:38 26 4
gpt4 key购买 nike

我发现扩展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.
}
});
}
});

这听起来很危险,因为自卸载插件至少正在等待从其自己的卸载返回调用......但这种做法真的那么危险吗?毕竟,现在卸载甚至是可撤消的,这意味着卸载的插件会“仍然存在”一段时间..?

最佳答案

是的,扩展程序可以使用 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');
}
});

关于javascript - Firefox 扩展可以自行卸载吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39154543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com