jsbuster
jsbuster

Reputation: 173

Trying to use nsIPrefBranch to store data on Firefox extension gives NS_ERROR_UNEXPECTED

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?

Upvotes: 1

Views: 973

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

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".

Upvotes: 3

Related Questions