尝试使用 nsIPrefBranch 在 Firefox 扩展上存储数据会出现 NS_ERROR_UNEXPECTED

发布于 2025-01-01 22:49:50 字数 420 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2025-01-08 22:49:50

不存在称为“纪元”的偏好 - 这就是此消息告诉您的内容。您必须先设置此首选项,然后 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 to nsIPrefBranch into try .. 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文