Firefox 扩展保存在首选项中
我想将一个简单的字符串保存到我的 Firefox 扩展的首选项中。该字符串应该可以在不同的浏览器会话之间以及重新启动后访问。我找到了以下代码,但由于某种原因它不起作用。我之前尝试过 localStorage,但这也不起作用:
保存功能代码:
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var str = Components.classes["@mozilla.org/supports-string;1"]
.createInstance(Components.interfaces.nsISupportsString);
str.data = window['myglobalvariable'];
prefs.setComplexValue("myglobvar",
Components.interfaces.nsISupportsString, str);
检索功能代码:
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var value = prefs.getComplexValue("myglobvar",
Components.interfaces.nsISupportsString).data;
我不明白为什么它不起作用。
I want to save a simple string into preferences in my firefox extension. The string should be accessible between separate browser sessions and after reboot. I found the following code but for some reason it is not working. I tried localStorage before but this was also not working:
Code for saving function:
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var str = Components.classes["@mozilla.org/supports-string;1"]
.createInstance(Components.interfaces.nsISupportsString);
str.data = window['myglobalvariable'];
prefs.setComplexValue("myglobvar",
Components.interfaces.nsISupportsString, str);
Code for retrieval function:
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var value = prefs.getComplexValue("myglobvar",
Components.interfaces.nsISupportsString).data;
I cannot figure out why is it not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的首选项是整数或布尔值 getCharPref 不起作用,您将不得不使用
http://blog.mozilla.org/addons/2009/01/28/how-to-develop-a-firefox-extension/
if your preference is a integer or boolean getCharPref wont work you would have to use
http://blog.mozilla.org/addons/2009/01/28/how-to-develop-a-firefox-extension/