私はASCIIとUTF-8で分けるのは面倒なので全部UTF-8で保存している
setPref("json", JSON.stringify({ json: "あああ" }));
var Cc = Components.classes, Ci = Components.interfaces;
var gBranch = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("extensions.[your_extensions_name].");
保存:
function setPref(prefName, value) {
if (typeof value === 'boolean') {
gBranch.setBoolPref(prefName, value);
} else if (typeof value === 'number') {
gBranch.setIntPref(prefName, value); // -2,147,483,648~2,147,483,647
} else if (typeof value === 'string') {
var ustr = Cc['@mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString);
ustr.data = value;
gBranch.setComplexValue(prefName, Ci.nsISupportsString, ustr);
// gBranch.setCharPref(prefName, value); // ASCII only 日本語はムリ
}
}
取得:
function getPref(prefName, defValue) {
var type = gBranch.getPrefType(prefName);
const nsIPrefBranch = Ci.nsIPrefBranch;
switch (type) {
case nsIPrefBranch.PREF_STRING:
default:
return gBranch.getComplexValue(prefName, Ci.nsISupportsString).data;
// return gBranch.getCharPref(prefName); // ASCII only
case nsIPrefBranch.PREF_INT:
return gBranch.getIntPref(prefName);
case nsIPrefBranch.PREF_BOOL:
return target.getBoolPref(prefName);
}
return defValue;
}
0 件のコメント:
コメントを投稿