handle
handle

Reputation: 6369

How to write a Thunderbird extension (webextension) to modify the message display?

I'd like to write an extension for Thunderbird that modifies the message display (e.g. insert/replace text/markup/image).
Unfortunately, the documentation is lacking (due to recent changes?).

Some examples can be found at

Building on https://github.com/thundernest/sample-extensions/tree/master/messageDisplay

I've modified background.js

browser.messageDisplay.onMessageDisplayed.addListener((tabId, message) => {
  console.log(`Message displayed in tab ${tabId}: ${message.subject}`);
  console.log(message.id);
  browser.messages.getFull(message.id).then((messagepart) => {
      console.log(messagepart);
      body = messagepart['parts'][0]['parts'][0]['body'];
      console.log(body);
      body += "modified!";
      console.log(body);
  });
  browser.windows.getCurrent().then((window)=>{
    console.log(window.type);
  });

  browser.tabs.getCurrent().then((tab)=>{
    console.log("tab",tab);
  });
});

which gives me the message body (using magic indexes) but expectedly, the change is not reflected in the message display.
The window type returned is normal, not messageDisplay.
The tab is undefined despite adding permissions

  "permissions": [
    "messagesRead",
    "activeTab",
    "tabs",
    "tabHide"
  ],

but I assume that's because the script is running as background.

So I'd need a script running on the content / access to the tab and then some hints on how to modify the displayed message content (I do not want to modify the message).

Where would I find the equivalent documentation to

specific to Thunderbird?


Specifying content_scripts in manifest.json causes "Error: Error reloading addon [email protected]: undefined".

executeScript() from background does not seem to work either, even with tabId specified.

Upvotes: 6

Views: 2712

Answers (2)

Qeole
Qeole

Reputation: 9184

This was not possible to do when you wrote your question, the API for modifying displayed messages was missing.

As of this writing (September 2020), the browser.messageDisplayScripts API landed a few days ago, see bug 1504475 and related patch for examples. It works as follows: You can register your content script (to modify the displayed messages) like this

let myPromise = browser.messageDisplayScripts.register({
    css: [{
        file: "/style.css",
    }],
    js: [{
        file: "/content_script.js",
    }],
});

And you can later unregister with

myPromise.then((script) => { script.unregister(); });

You need to register the script just once for all messages (you do not need a listener that would load it each time a message is displayed).

Note that your manifest.json needs to include the messagesModify permission for this to work.

The new API will be in Thunderbird version 82, so if I understand the release process correctly it should be in stable version 88 (unless it is backported before that). You can try it already (v82 is the current EarlyBird).

Upvotes: 1

blue
blue

Reputation: 11

Documentation https://thunderbird-webextensions.readthedocs.io/en/68/tabs.html#getcurrent says:

May be undefined if called from a non-tab context (for example: a background page or popup view).

Since the background.js is not called from a tab context the tab is undefined.

Upvotes: 0

Related Questions