在用户分支中设置首选项,并在卸载时取消设置 [英] Set preferences in the user branch and unset them on uninstall

查看:66
本文介绍了在用户分支中设置首选项,并在卸载时取消设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有以下 lib/main.js :

I created a firefox add-on with the following lib/main.js:

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

由于以下原因而未被接受:

It wasn't accepted with the following reason:

更改关键设置的加载项在禁用或卸载后必须还原更改.您还应该在默认分支(而不是用户分支)中进行更改.

Add-ons which change critical settings must revert the changes when disabled or uninstalled. You should also make the changes in the default, rather than the user, branch.

您需要在首选项服务上调用getDefaultBranch(""),并在返回的对象而不是直接在首选项服务上调用首选项方法.

You need to call getDefaultBranch("") on the preferences service, and call the preference methods on the returned object rather than on the preference service directly.

要将首选项恢复为默认值(由设置),我发现必须执行

To revert a preference back to the default, set by setIntPref(), I found out that I have to do this on uninstall:

pref.clearUserPref("network.http.response.timeout")

如果我在另一个测试插件中调用此命令,它将正常工作.我只需要找出

This command works fine If I call it in another test-addon. I only have to find out How to implement a command, so it is executed when the firefox-addon is uninstalled?

那么我该如何理解这些评论?如何在用户分支"中设置首选项?

So how do I have to understand these comments? How do I set the preferences in a "user branch"?

推荐答案

我像这样解决了第二部分(卸载),在我的main.js中,我在末尾添加了以下代码:

I solved the second part (uninstall) like this, that in my main.js I added this code at the end:

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

这可以用来禁用和卸载该加载项.

That worked on disabling and uninstalling the add-on.

这篇关于在用户分支中设置首选项,并在卸载时取消设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆