我不知道是否有可能获得某些偏好设置在about:config使用JavaScript?关于配置偏好和js
激励是为了获得在用户登陆插件前端时创建的Firefox插件中设置的偏好值。基本上,我试图确定登陆FE的用户,而不要求他们明确登录。
我不知道是否有可能获得某些偏好设置在about:config使用JavaScript?关于配置偏好和js
激励是为了获得在用户登陆插件前端时创建的Firefox插件中设置的偏好值。基本上,我试图确定登陆FE的用户,而不要求他们明确登录。
是的,你可以。
首先,您需要知道Mozilla使用XPCOM接口作为首选项系统。
三个使用的接口是nsIPrefService,nsIPrefBranch和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.");
而且有3种类型的偏好,他们串,整数和布尔。有六种方法nsIPrefBranch读取和写入的喜好:getBoolPref(),setBoolPref(),getCharPref(),setCharPref(),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。
Firefox增加了Service.jsm模块,使得访问所有界面变得更加容易:https://developer.mozilla.org/en/JavaScript/Code_modules/Services.jsm – 2010-09-27 16:26:31
嗯,合法的,谢谢你的提示。 ; ) – Guilherme 2010-09-28 01:13:36
感谢您的回复。这种技术我一直在使用插件来存储偏好。但是这对我来说不起作用,因为这段代码是在addon命名空间上执行的,而我需要的是将在FE上加载的js,并从addon检索首选项。它不允许从js命名空间没有用户beeing行动警告。我会尽量避免使用插件脚本来改变HTML,但是如果我发现没有其他解决方案改变HTML是可以接受的。 – 2010-09-29 22:23:34