Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mozilla (Firefox, Thunderbird) Extension: How to get extension id (from install.rdf)?

If you are developing an extension for one of the mozilla applications (e.g. Firefox, Thunderbird, etc.) you define a extension id in the install.rdf.

If for some reason you need to know the extension id e.g. to retrieve the extension dir in local file system (1) or if you want to send it to a webservice (useage statistic) etc. it would be nice to get it from the install.rdf in favour to have it hardcoded in your javascript code.

But how to access the extension id from within my extension?

1) example code:

var extId = "[email protected]";
var filename = "install.rdf";
var file = extManager.getInstallLocation(extId).getItemFile(extId, filename);
var fullPathToFile = file.path;
like image 732
DanielaWaranie Avatar asked Mar 23 '11 00:03

DanielaWaranie


People also ask

How do I find Firefox extension ID?

Open about:memory . Click "measure" in Show memory reports. In the Main Process section, scroll down to Other Measurements. There you will find the installed (active) extensions with their names and their ids displayed as baseURL=moz-extension://[random-ids].

How do I see installed extensions in Firefox?

Viewing and managing your installed add-onsClick the menu button. and choose Add-ons and Themes. The Add-ons Manager tab will open. Select the panel for the type of add-on you wish to view or manage, such as the Extensions or Themes panel.

How do I access extensions in Thunderbird?

Start Thunderbird. Go to Tools -> Addons -> Extensions. Select the icon that looks like a gear (its actually a list box) near the top of the window and then "Install add-on from file".

Where is extension data stored Firefox?

Most add-on data is stored in a folder in the Firefox user profile. However, some information is stored in the profile folder also. It's possible that there is a problem with the file(s) that store the extensions registry. Type about:support in the address bar and press enter.


2 Answers

I'm fairly sure the 'hard-coded ID' should never change throughout the lifetime of an extension. That's the entire purpose of the ID: it's unique to that extension, permanently. Just store it as a constant and use that constant in your libraries. There's nothing wrong with that.

What IS bad practice is using the install.rdf, which exists for the sole purpose of... well, installing. Once the extension is developed, the install.rdf file's state is irrelevant and could well be inconsistent.

"An Install Manifest is the file an Add-on Manager-enabled XUL application uses to determine information about an add-on as it is being installed" [1]

To give it an analogy, it's like accessing the memory of a deleted object from an overflow. That object still exists in memory but it's not logically longer relevant and using its data is a really, really bad idea.

[1] https://developer.mozilla.org/en/install_manifests

like image 169
Rushyo Avatar answered Nov 15 '22 05:11

Rushyo


Like lwburk, I don't think its available through Mozilla's API's, but I have an idea which works, but it seems like a complex hack. The basic steps are:

  1. Set up a custom resource url to point to your extension's base directory
  2. Read the file and parse it into XML
  3. Pull the id out using XPath

Add the following line to your chrome.manifest file

resource    packagename-base-dir chrome/../

Then we can grab and parse the file with the following code:

function myId(){
    var req = new XMLHttpRequest();

    // synchronous request
    req.open('GET', "resource://packagename-base-dir/install.rdf", false); 
    req.send(null);

    if( req.status !== 0){
        throw("file not found");
    }

    var data = req.responseText;

    // this is so that we can query xpath with namespaces
    var nsResolver = function(prefix){
        var ns = {
            "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
            "em" : "http://www.mozilla.org/2004/em-rdf#"
        };
        return ns[prefix] || null;
    };

    var parser = CCIN("@mozilla.org/xmlextras/domparser;1", Ci.nsIDOMParser);
    var doc = parser.parseFromString(data, "text/xml");
    // you might have to change this xpath expression a bit to fit your setup
    var myExtId = doc.evaluate("//em:targetApplication//em:id", doc, nsResolver, 
                            Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null);

    return myExtId.singleNodeValue.textContent;
}

I chose to use a XMLHttpRequest(as opposed to simply reading from a file) to retrieve the contents since in Firefox 4, extensions aren't necessarily unzipped. However, XMLHttpRequest will still work if the extension remains packed (haven't tested this, but have read about it).

Please note that resource URL's are shared by all installed extensions, so if packagename-base-dir isn't unique, you'll run into problems. You might be able to leverage Programmatically adding aliases to solve this problem.

This question prompted me to join StackOverflow tonight, and I'm looking forward participating more... I'll be seeing you guys around!

like image 26
skabbes Avatar answered Nov 15 '22 05:11

skabbes