Set preferences in the user branch and unset them on uninstall

六月ゝ 毕业季﹏ 提交于 2019-12-29 07:53:27

问题


I created a firefox add-on with the following lib/main.js:

const {Cc,Ci} = require("chrome");
var pref = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
pref.setIntPref("network.http.response.timeout", 3600*24);

It wasn't accepted with the following reason:

Add-ons which change critical settings must revert the changes when disabled or uninstalled. You should also make the changes in the default, rather than the user, branch.

You need to call getDefaultBranch("") on the preferences service, and call the preference methods on the returned object rather than on the preference service directly.

To revert a preference back to the default, set by setIntPref(), I found out that I have to do this on uninstall:

pref.clearUserPref("network.http.response.timeout")

This command works fine If I call it in another test-addon. I only have to find out How to implement a command, so it is executed when the firefox-addon is uninstalled?

So how do I have to understand these comments? How do I set the preferences in a "user branch"?


回答1:


Here's how I did it just now:

function clearPrefBranch(aPrefBranchName) {                                     
  var defaultBranch = Services.prefs.getDefaultBranch(null);                    
  defaultBranch.deleteBranch(aPrefBranchName);                                  
}    

Then, just call clearPrefBranch with an argument of extensions.mypluginname (assuming you used the naming convention, and you should be able to delete all of your extension's installed preferences.

EDIT:

The code I used inside of my main.js file:

const {Cc,Ci,Cm,Cr,Cu} = require("chrome");                                     
Cu.import("resource://gre/modules/Services.jsm");  

exports.onUnload = function(aOptions, aCallbacks) {                             
  MyPlugin.shutdown();                                                           
};

function clearPrefBranch(aPrefBranchName) {                                     
  var defaultBranch = Services.prefs.getDefaultBranch(null);                    
  defaultBranch.deleteBranch(aPrefBranchName);                                  
}

var MyPlugin = {
  shutdown: function() {                                                      
    prefLoader.clearPrefBranch('extensions.oopstab');                         

  }
}; 



回答2:


I solved the second part (uninstall) like this, that in my main.js I added this code at the end:

exports.onUnload = function(reason) {
    //called when add-on is 
    //    uninstalled
    //    disabled
    //    shutdown
    //    upgraded
    //    downgraded
    pref.clearUserPref("network.http.response.timeout");
};

That worked on disabling and uninstalling the add-on.



来源:https://stackoverflow.com/questions/23985279/set-preferences-in-the-user-branch-and-unset-them-on-uninstall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!