是的你可以。
首先,您需要知道 Mozilla 使用 XPCOM 接口作为首选项系统。
使用的三个接口是nsIPref服务, nsIPref分支 and nsIPrefBranch2.
首选项服务的实例化方式与实例化任何服务的方式相同XPCOM服务.
两个例子可以清楚地说明:
// 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.");
有 3 种类型的偏好,它们是string, integer and boolean。里面有六种方法nsIPref分支读取和写入首选项:getBoolPref(), setBoolPref(), getCharPref(), setCharPref(), getIntPref() and 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 字符时,请使用它。
Example:
// 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);
我希望你能以此解决你的疑惑。
More on 这一页.