The other day I submitted an addon to Mozilla Addons for the preliminary review. The reviewer gave me this recommendation:

Use JavaScript modules where possible. These will only be imported if they haven't been loaded yet and reduce memory consumption and response times. See https://developer.mozilla.org/en-US/docs/JavaScript_code_modules and https://developer.mozilla.org/en-US/docs/JavaScript_code_modules/Services.jsm . Services.prefs ... is interesting for you.

Now, I was using this to load my prefs:

var prefs = Components.classes["@mozilla.org/preferences-service;1"] .getService(Components.interfaces.nsIPrefBranch);

and later...

var mypref= this.prefs.getBoolPref("extensions.myaddon.mypref");

Now, I looked at the page references and am not sure how to do it the way the reviewer suggests. I can't find an example of how to do it that way. The best I could do was to add this:

Components.utils.import('resource://textlink-modules/prefs.js'),

...but I couldn't figure out how to refer to mypref after that. I am new to addons, so if someone could help me out, that would be great. How can I do it his way?

有帮助吗?

解决方案

You can't import your prefs.js as a module, because it isn't one.

What you can, and are encouraged to, do is import once the Services module and then reference the various services it offers through the global variable Services.

Edit:

Components.utils.import("resource://gre/modules/Services.jsm");
var prefs = Services.prefs;
var mypref = prefs.getBoolPref("extensions.myaddon.mypref");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top