📜 ⬆️ ⬇️

Do You Know How To: “Preferences” and “Install Location” in FireFox 4 betas?

Hello habr.

Being a developer of add-ons for FireFox, I decided to write my thoughts based on the recent article “Compatibility of add-ons with Firefox 4 ...” from a well-known author.

In my thoughts, I was interested in what was placed in the title, namely the Preferences object and how to get the path to “Install Location”.
')
Under the cut code and description to it.



As it is not difficult to guess the majority of them have already been written in that article, and it was specifically worth it, of course, to pay attention to this .

However, not everything is immediately clear and understandable, so after reviewing my add-on, I decided to tell you, well, what if someone is interested in it?

My addition is actively working with Preferences and the file system, specifically with the folder in which it itself is, it contains a library of books. Therefore, when I learned a couple of weeks ago that in the 4th version nothing works, I decided to do it at my leisure. Leisure happened today, and that's what I discovered.

Immediately make a reservation about the abbreviations used by me:

var CI = Components.interfaces;<br>
var CC = Components.classes;<br>
var pref = " preferences " // "extensions.kbtrainerff." <br>
var ext_id = "id " // "kbtrainerff@gmail.com"



Now, let's get started.

Preferences:



It was:

preferences_object = CC[ "@mozilla.org/preferences-service;1" ].getService(CI.nsIPrefService).getBranch(pref);


It became:

var CU = Components.utils<br>CU.import( "resource://gre/modules/Services.jsm" );<br>preferences_object = Services.prefs.getBranch(pref);


Defining the path to the add-on folder:



Identical to yours [addon | extension | "Application"].

It was:

preferences_object = CC[ "@mozilla.org/extensions/manager;1" ].getService(CI.nsIExtensionManager).getInstallLocation(ext_id);


It became:

It was a problem, but, as it turned out, having rummaged in the object model and in “document.location = about: config” it is completely solvable.
The decision, controversial, but available:

var CU = Components.utils<br>CU.import( "resource://gre/modules/Services.jsm" );<br> var ic = Services.prefs.getBranch( "extensions." );<br> var ics = eval(ic.getCharPref( "installCache" ));<br><br> for ( var i = 0; i < ics.length; i++){<br> var obj = ics[i];<br> if (obj.name == "app-profile" ){<br> if (obj[ "addons" ] != undefined){<br> if (obj[ "addons" ][ext_id] != undefined){<br> if (obj[ "addons" ][ext_id][ "descriptor" ] != undefined){<br> extdir = obj[ "addons" ][ext_id][ "descriptor" ];<br> break ;<br> }<br> }<br> }<br> }<br>}<br><br>alert(extdir);


Make it a file.

var theFile = CC[@mozilla.org/file/local;1"].createInstance(CI.nsILocalFile);<br>Install_Location = theFile.initWithPath(extdir);


That's all :)

Enjoy!

* This source code was highlighted with Source Code Highlighter .

UPD: fresh kbTrainer 0.1.2 for FireFox

Source: https://habr.com/ru/post/99951/


All Articles