Using WebExtension APIs
Extending the simple example extension to make use of WebExtension APIs.
Last updated
Extending the simple example extension to make use of WebExtension APIs.
Last updated
In the second part of the Hello World Extension Tutorial, we will add a "Details" button to the message header toolbar. A click on it will show some information about the currently displayed message, which we retrieve using Thunderbird's WebExtension APIs.
message_display_action
Similar to adding the browser_action
in the first part of the Hello World Extension Tutorial, we have to extend the manifest.json
to add the message_display_action
.:
The HTML file for our popup needs some place-holders, which we can later fill using JavaScript and Thunderbird's WebExtension APIs. We create a messagePopup
folder inside the hello-world
project folder and place the following popup.html
file in the newly created folder:
We place the following popup.css
file in the same folder as the popup.html
file.
Instead of tables, we use modern CSS styling to format our HTML into a tabular view. The display: grid
container defines how our 6 DIV elements inside the container DIV are aligned. Check the grid documentation or this grid guide for more details.
All WebExtension API functions return a Promise instead of an actual value. This aims to simplify the handling of asynchronous functions.
The author of this example prefers the async
/await
syntax over the .then()
approach for handling Promises. This allows us to avoid the so called callback-hell of asynchronous functions and instead keep writing sequential code by simply awaiting all the returned Promises.
The popup.js
file was loaded as a top level ES6 module by specifying type="module"
in its script
tag. This allows us to use the await
keyword directly in file scope code. Otherwise we would need to use an asynchronous wrapper function:
async function load() {
let tabs = await messenger.tabs.query({
active: true,
currentWindow: true,
});
...
}
load();
In Thunderbird, all WebExtension API can be accessed through the browser.*
namespace, as with Firefox, but also through the messenger.*
namespace, which is a better fit for Thunderbird.
The tabs API provides access to Thunderbird's tabs. We need to get hold of the current active tab to learn which message is displayed there. We use the query method to find it in line 3
.
Using messenger.tabs.getCurrent()
will not work, as that always returns the tab in which it is being called from. In our case, the call is executed from inside the popup of the message_display_action
and not from inside the tab we are looking for.
The getDisplayedMessage method of the messageDisplay API provides access to the currently viewed message in a given tab. It returns a Promise for a MessageHeader object from the messages API with basic information about the message in line 9
.
At this stage we are interested in the subject (line 12
) and the author (line 13
).
The getDisplayMessage method requires the messagesRead
permission, which needs to be added to the permissions
key of our manifest.json
file.
We also want to get the received
header from the message. That information is not part of the general MessageHeader
object, so we have to request the full message.
The getFull method in line 16
returns a Promise for a MessagePart object, which relates to messages containing multiple MIME parts. The headers
member of the part returned by getFull
includes the headers of the message (excluding headers which are part of nested MIME parts available through the parts
member).
Let's double-check that we made the correct changes and have all the files in the right places:
This is how our manifest.json
should now look like:
As described in the first part of the Hello World Extension Tutorial, go to the Add-ons Manager to open the Debug Add-on Page and temporarily install the extension.
Open any message and you will find a "Details" button in the message header toolbar. A click on it will show you the subject
, the author
and the first found received
header of the currently viewed message.