以编程方式卸载/删除Firefox扩展

Uninstall/Remove Firefox Extension programmatically?

本文关键字:删除 Firefox 扩展 卸载 编程 方式      更新时间:2023-09-26

是否可以通过编程方式卸载Firefox扩展?

是否可以从其他扩展名执行此脚本?
  1. 使用AddonManager.getAddonByID
  2. 获取附加组件的引用
  3. 检查PERM_CAN_UNINSTALL标志是否可以卸载附加组件(例如,用户通常不能卸载系统范围的附加组件,但可以禁用)。
  4. 呼叫Addon.uninstall() .

示例代码(您可能希望添加适当的错误处理,等等):

Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("some@id", function(addon) {
  if (!addon) {
    // Add-on not present
    return;
  }
  if (!(addon.permissions & AddonManager.PERM_CAN_UNINSTALL)) {
    // Add-on cannot be uninstalled
    return;
  }
  addon.uninstall();
  if (addon.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.
  }
});

还有另一种方法可以通过编程方式删除扩展:您可以从firefox配置文件文件夹中删除xpi文件:

    Windows:

' % appdata % ' Mozilla Firefox ' Profiles ' ' * ' ' extension@name.xpi扩展

    Linux/Mac:

/home/*/。mozilla firefox扩展/extension@name.xpi

从安全的角度来看,唯一有意义的事情是如果扩展可以删除自己。尽管问题说明了如何从另一个扩展中做到这一点,但我认为为了完整性起见,添加扩展如何删除自己是很重要的。

这可以使用management.uninstallSelf()来完成。

browser.management.uninstallSelf({ 
  showConfirmDialog: true, 
  dialogMessage: 'Please reconsider, since this is the best extension!' 
}).then(() => alert('Extension uninstalled'))
  .catch(() => alert('User cancelled extension uninstall'))

如果showConfirmDiloag为true,则弹出确认对话框。

如果用户选择保留扩展名,uninstallSelf返回的Promise返回一个错误。

参数对象中的两个属性都是可选的