2016-08-25 192 views
0

我发现这个地方和延伸是能够卸载延伸Uninstall/Remove Firefox Extension programmatically?Firefox扩展可以自行卸载吗?

不知同样的方法也将工作延期卸载本身(例如,如果它检测上级扩展取而代之)?下面的代码是我的答案的简单适应上述链接的问题:

Components.utils.import("resource://gre/modules/AddonManager.jsm"); 
AddonManager.getAddonByID("[email protected]", function(betteraddon) { 
    if (betteraddon) { 
     AddonManager.getAddonById("[email protected]", 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. 
     } 
     }); 
    } 
}); 

这听起来很危险的,因为自卸载插件至少从它自带的卸载返回到呼叫等待... 但是这种方法真的很危险吗?毕竟,现在卸载甚至可以撤销,这意味着卸载的插件“仍然存在”一段时间..?

+0

这听起来像你的问题可以通过尝试它只是简单地回答。那么,当你尝试它时发生了什么? – Makyen

+0

@Makyen当我在我自己的环境中在自己的机器上尝试一次时,某些工作可能会在不受我控制的情况下(例如竞争条件) –

回答

2

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

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

uninstallself:My [email protected] 
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'); 
    } 
});