Preferences

So at first I was having trouble with the preference names it would use extensions.<uuid>.sdk.<pref-name> or extensions.@<name>.sdk.<pref-name>. I managed to change it with the preferences-branch option. However, when I tested uninstalling my add-on the preferences were not removed. Is there a way to (optionally) remove them on an uninstall. I looked in the lower-level preferences/service module, but there wasn’t anything about removing them.

Preferences should start with

 extensions.YOUR_ADDON_ID_HERE_FROM_INSTALL_RDF.

Then on uninstall you should remove with

Services.prefs.clearUserPref('extensions.YOUR_ADDON_ID_HERE_FROM_INSTALL_RDF.YOUR_PREF_NAME')

Reason for that format, is if you want to sync the preferences you do it like this:

I changed my extension preferences to extensions.<name>.<pref-name> because the UUID was long and not easy to remember and I kept forgetting the @ and sdk part of the preferences (when not using the UUID).

However that didn’t quite answer my question as clearUserPref only resets the preference to the default value, not actually remove it. Also just loading the preferences/service module did nothing. It threw an error clearUserPref is not a function.

exports.onUnload = function ( reason )
{
    var prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);

    console.log ( 'Unload Reason: ' + reason );
    pref = 'extensions.<name>.enable_notifications';
    prefService.clearUserPref ( pref );
}

Oh you are correct, to delete a preference you have to delete the branch, this will delete the on the default branch and the active branch:

  Services.prefs.deleteBranch('extensions.Profilist@jetpack.');

Be sure to include that final . in there.

If in your install.rdf you have this:

      <Description about="urn:mozilla:install-manifest">
        <em:id>Profilist@jetpack</em:id>

I think it is strongly recommended you use exactly what is in the <id> tag, due to syncing and other stuff I think, Im not exactly sure. So you would use extensions.Profilist@jetpack.<pref-name>

Is there a way to do that with just the preferences/service module or do I have to import/require the chrome module and call the nsIPrefBranch?

const {Cc,Ci} = require("chrome");
prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
exports.onUnload = function ( reason )
{
    console.log ( 'Unload Reason: ' + reason );
    var prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
    prefService.deleteBranch ( 'extensions.@<name>.' ); // JPM bundles the name property in the package.json as `@<extenstion-name>-<version>.xpi`
}

I’m not sure I would have to read the docs - https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/simple-prefs

and then possibly compare and see what its doing internally - https://github.com/mozilla/addon-sdk

Its good of you to investigate this. As I hate when addons leave stuff over after uninstallation of it. But I think if you use simple-prefs, it handles remove the preferences on: uninstall of the addon. then restart of the browser. Please verify before you waste your time digging into this. :slight_smile:

Well I’ve looked at both the high and low level modules and neither mention a way to delete a preference. Which I would say that’s an oversight. Shouldn’t have to call a low level way of handling settings.

Setting a preference to the default value effectively deletes it. Preferences with a default value, sometimes called being on the default branch, do not exist explicitly. They are simply looked up from the code in the addon which defines the default value.

If you give a preference a non-default value then it is actually written into the preferences file. Resetting to the default removes it from this file. Once your addon is also removed the preference will disappear completely.

I just tested that. The only preferences that were removed after I uninstalled it was the following

extensions.@mytest.sdk.baseURI
extensions.@mytest.sdk.domain
extensions.@mytest.sdk.load.reason
extensions.@mytest.sdk.rootURI
extensions.@mytest.sdk.version

However, the preference defined in the package.json file extensions.@mytest.test with a default value was not deleted after I removed the test add-on.