尝试使用 nsIPrefBranch 在 Firefox 扩展上存储数据会出现 NS_ERROR_UNEXPECTED
我正在尝试在 Firefox 扩展中保存少量持久数据。
因此,我尝试像这样使用 nsIPrefBranch:
var db = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
if(db.getCharPref('epoch')){ ///do something.. }
但它似乎不起作用,并且出现此错误:
“组件返回失败代码:0x8000ffff (NS_ERROR_UNEXPECTED) [nsIPrefBranch.getCharPref]"
有人可以告诉我我做错了什么吗?
I'm trying to save a small amount of persistent data in a Firefox extension.
So, I'm trying to use nsIPrefBranch
like this:
var db = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
if(db.getCharPref('epoch')){ ///do something.. }
but it doesn't seem to work and I'm getting this error:
"Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED)
[nsIPrefBranch.getCharPref]"
Can somebody please tell what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不存在称为“纪元”的偏好 - 这就是此消息告诉您的内容。您必须先设置此首选项,然后
getCharPref()
才能成功。这就是为什么扩展通常将对 nsIPrefBranch 的调用包装到 try .. catch 块中 - 错误是预料之中的。另一方面:首选项是一个共享空间(请在
about:config
下查看它们)。您应该明确某个首选项属于您的扩展程序,并确保它不会与其他扩展程序或浏览器的首选项发生冲突。换句话说,使用“extensions.myExtension.epoch”之类的内容而不是“epoch”。There is no preference called "epoch" - that's what this message is telling you. You have to set this preference before you can expect
getCharPref()
to succeed. Which is why extensions usually wrap calls tonsIPrefBranch
intotry .. catch
blocks - errors are expected.On a different note: preferences are a shared space (have a look at them under
about:config
). You should make clear that a preference belongs to your extension and make sure it won't conflict with preferences of other extensions or the browser. In other words, use something like "extensions.myExtension.epoch" rather than "epoch".