Find out the path to the addon folder in the Thunderbird API
-
08-07-2021 - |
Question
I am modifying an addon for thunderbird and found the path to the addon hardcoded in one of the javascript files. This seemed sloppy to me and I thought that it had to be possible to find out this path programmatically. However, after much googeling, I still can't find an answer to that question.
Do you know how I can find out the path to the folder (containing the install.rdf
) of a Thunderbird addon?
Solution
You can use the AddonManager
API for that:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("foo@example.com", function(addon)
{
var uri = addon.getResourceURI("install.rdf");
if (uri instanceof Components.interfaces.nsIFileURL)
{
var file = uri.file;
alert(file.parent.path);
}
});
For reference: Addon, nsIFileURL, nsIFile.
The assumption in the code above is that the extension is unpacked upon installation, your predecessor probably added <em:unpack>true</em:unpack>
to install.rdf
. Normally this flag shouldn't be specified, leaving the extension packed on disk is better for performance. If you simply need to read a file from your extension you can use XMLHttpRequest
for that without requiring that it is a physical file on disk:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("foo@example.com", function(addon)
{
var uri = addon.getResourceURI("example.txt");
var request = new XMLHttpRequest("GET", uri.spec);
request.addEventListener("load", function()
{
alert(request.responseText);
}, false);
request.send();
});