Thunderbird WebExtensions / MailExtensions development - How to deal with events such as "new mail"?

269 Views Asked by At

I'm trying to write my very first Thunderbird extension. If possible, I'd like to only use the newer WebExtensions / MailExtensions APIs.

Two things my extension needs to do:

  • Performs an action when a new mail arrives and is not junk.
  • When a message is read, check if there are still unread messages and, if not, performs an action.

The only examples I've found online dealing with "new mail event" hooks look like there are not using the newer APIs. For example:

Components.classes["@mozilla.org/messenger/msgnotificationservice;1"]
    .getService(Components.interfaces.nsIMsgFolderNotificationService);
notificationService.addListener(myListener, notificationService.msgAdded);

or

Components.classes['@mozilla.org/messenger/services/session;1']
    .getService(Components.interfaces.nsIMsgMailSession)
    .AddFolderListener(myListener, Components.interfaces.nsIFolderListener.all);

... where myListener would be called when a new email arrives.

Those codes generate the error Components.classes is undefined in Thunderbird 91. If I understand properly this is because more stuff is required to stay compatible with the legacy API.

My question:

What is the proper way to listen to a new email event, using the WebExtensions / MailExtensions APIs?


Links I did read (but maybe I missed something!):

1

There are 1 best solutions below

0
On

Oh! I found it!

background.js :

browser.messages.onNewMailReceived.addListener((folder, messages) => {
  // ...
});

Those permissions are required: messagesRead and accountsRead.