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

    Hello Habr.

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

    In my thoughts, I was interested in what was put in the header, namely the Preferences object and how to get the path to “Install Location”.

    Under a cat the code and the description to it.



    As you might guess, a large part has already been written in that article, but it was particularly worthwhile, of course, to pay attention to this .

    However, not everything is immediately clear and understandable, therefore, after revising my supplement, I decided to tell you, well, what if someone would be interested?

    My add-on actively works with Preferences and the file system, specifically with the folder in which it itself lies, the library of books is stored there. Therefore, when I learned a couple of weeks ago that nothing works in the 4th version, I decided to do it at my leisure. Leisure happened today, and this is what revealed to me.

    Immediately make a reservation about the abbreviations I use:

    var CI = Components.interfaces;

    var CC = Components.classes;

    var pref = "ветка вашего дополнения в preferences " // у меня "extensions.kbtrainerff."

    var ext_id = "id вашего дополнения" // у меня "kbtrainerff@gmail.com"



    Now, let's start.

    Preferences:



    It was:

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


    It became:

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


    Determining the path to the add-on folder:



    Identically equal to your [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, rummaging through the object model and in “document.location = about: config” - it is completely solvable.
    Controversial decision, but available:

    var CU = Components.utils
    CU.import("resource://gre/modules/Services.jsm");
    var ic = Services.prefs.getBranch("extensions.");
    var ics = eval(ic.getCharPref("installCache"));

    for(var i = 0; i < ics.length; i++){
        var obj = ics[i];
        if(obj.name == "app-profile"){
            if(obj["addons"] != undefined){
                if(obj["addons"][ext_id] != undefined){
                    if(obj["addons"][ext_id]["descriptor"] != undefined){
                        extdir = obj["addons"][ext_id]["descriptor"];
                        break;
                    }
                }
            }
        }
    }

    alert(extdir);


    We make it a file.

    var theFile = CC[@mozilla.org/file/local;1"].createInstance(CI.nsILocalFile);
    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

    Also popular now: