Firefox Addon:如何在卸载插件时删除首选项?

时间:2013-08-30 12:29:33

标签: javascript firefox firefox-addon

我的Firefox插件中有以下代码:

var firstrun = Services.prefs.getBoolPref("extensions.CustomButton.firstrun");

if (firstrun) {
  // alert("first run");
  Services.prefs.setBoolPref("extensions.CustomButton.firstrun", false);
  installButton("nav-bar", "custom-button-1");
} else {
  // alert("not first run");
} 

在addon_dir / defaults / preferences / pref.js中,我有以下字符串:

pref("extensions.CustomButton.firstrun", true);

当addon第一次运行时,上面的代码理解它并在工具栏上安装一个按钮。此外,它还将以下字符串添加到profile_dir / prefs.js:

user_pref("extensions.CustomButton.firstrun", false);

工作正常。唯一困扰的是当我卸载插件时,profile_dir / prefs.js中的这个字符串不会被清除。因此,如果我第二次安装此插件,则第四个值为false,并且该按钮不会添加到工具栏中。

问题:是否可以删除插件首选项(在我的情况下,user_pref(“extensions.CustomButton.firstrun”,false);)卸载插件后?

注意:我已阅读this article,但仍不知道要等待的事件。任何工作的例子?我相信对于插件创作者来说这是一个常见的操作,我很惊讶没有文章详细解释这些问题。

3 个答案:

答案 0 :(得分:2)

my Add-On中,我在安装时设置了它:

const {Cc,Ci} = require("chrome");
var pref = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
pref.setIntPref("network.http.response.timeout", 3600*24);

我这样解决了,在我的main.js我最后添加了这段代码:

exports.onUnload = function(reason) {
    //called when add-on is 
    //    uninstalled
    //    disabled
    //    shutdown
    //    upgraded
    //    downgraded
    pref.clearUserPref("network.http.response.timeout");
};

用于禁用和卸载插件。

注意:这仍然不完美,请参阅评论。我将不得不努力......

答案 1 :(得分:2)

对于bootstrap插件,有一个uninstall()过程,但要触发它必须使用install()过程,只需传递两个参数并将该函数留空即可。

以下是我在附加组件中使用的内容: https://gist.github.com/Noitidart/e0d3c21ab38822fbfd17

function uninstall(aData, aReason) {
    console.info('UNINSTALLING ZOOMR reason = ', aReason);
    if (aReason == ADDON_UNINSTALL) { //have to put this here because uninstall fires on upgrade/downgrade too
        //this is real uninstall
        var prefPrefix = 'extensions.my-addon@jetpack.';
        console.log('deleting branch of: ' + prefPrefix);
        Services.prefs.deleteBranch(prefPrefix);
    }
}

答案 2 :(得分:0)

AddonManager.addAddonListener({
    onUninstalling: function(addon){
        // alert("uninstalling!");   
        Services.prefs.setBoolPref("extensions.CustomButton.firstrun", true);
    }
  });