5

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 messageDisplay@sample.extensions.thunderbird.net: undefined".

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

handle
  • 5,859
  • 3
  • 54
  • 82
  • Background.js runs in background merely to add the listener; your listener runs in foreground I think. So what you are looking to do is to replace the displayed message with your body variable? I see a getDisplayedMessage function but not a set, so it looks like that is not possible. Content_scripts is to load your scripts onto the page, so that should work - maybe there was a spelling mistake of the filename in your manifest. Since you have the tabID of the current tab, why not call get(tabID) rather than getCurrent(). – Peter Brand Mar 22 '20 at 14:17

2 Answers2

1

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).

Qeole
  • 8,284
  • 1
  • 24
  • 52
0

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.

blue
  • 11
  • 1