代码之家  ›  专栏  ›  技术社区  ›  Bojan Babic

关于配置首选项和JS

  •  10
  • Bojan Babic  · 技术社区  · 14 年前

    我想知道是否可以使用javascript在about:config中设置某些首选项的值?

    激励是获得我在用户登陆插件前端时创建的Firefox插件中设置的偏好值。基本上,我试图识别登录fe的用户,而不要求他们明确登录。

    1 回复  |  直到 12 年前
        1
  •  8
  •   gfe    14 年前

    是的,你可以。

    首先,您需要知道Mozilla使用xpcom接口作为首选项系统。

    三个使用的接口是 nsiprefservice服务 , nsiprefraction公司 nsiprefbranch2 .

    首选项服务的实例化方式与 XPCOM service .

    有两个例子可以说明:

    // Get the root branch
    var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                        .getService(Components.interfaces.nsIPrefBranch);
    

    .

    // Get the "extensions.myext." branch
    var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                        .getService(Components.interfaces.nsIPrefService);
    prefs = prefs.getBranch("extensions.myext.");
    

    有三种偏好,它们是 一串 , 整数 布尔值 . 有六种方法 nsiprefraction公司 读写首选项: GetBoolPref()。 , setBoolPref()。 , 获取harpref() , 设置字符首选项() , GetIntPref()。 setintpref()设置 .

    更多的例子:

    // Get the "accessibility." branch
    var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                        .getService(Components.interfaces.nsIPrefService).getBranch("accessibility.");
    
    // prefs is an nsIPrefBranch.
    // Look in the above section for examples of getting one.
    var value = prefs.getBoolPref("typeaheadfind"); // get a pref (accessibility.typeaheadfind)
    prefs.setBoolPref("typeaheadfind", !value); // set a pref (accessibility.typeaheadfind)
    

    也可以使用复杂类型。通过使用nsisupportsstring(用于处理首选项中的字符串),因此,当首选项值可能包含非ASCII字符时,请使用该字符串。

    例子:

    // prefs is an nsIPrefBranch
    
    // Example 1: getting Unicode value
    var value = prefs.getComplexValue("preference.with.non.ascii.value",
          Components.interfaces.nsISupportsString).data;
    
    // Example 2: setting Unicode value
    var str = Components.classes["@mozilla.org/supports-string;1"]
          .createInstance(Components.interfaces.nsISupportsString);
    str.data = "some non-ascii text";
    prefs.setComplexValue("preference.with.non.ascii.value", 
          Components.interfaces.nsISupportsString, str);
    

    我希望你能用这个解决你的疑问。

    更多信息 this page .