Search code examples
javascriptxulthunderbirdthunderbird-addon

How to access the body of the email message being composed?


I created a toolbar button in message Compose Toolbar Palette.

<toolbarpalette id="MsgComposeToolbarPalette">
  <toolbarbutton id="myextension-button1" class="toolbarbutton-1"
      label="Cryption_compose" tooltiptext="Encrypts or decrypts your email"
      oncommand="Cryption.open_options();" />
</toolbarpalette>

OnClick opens the options.xul, where I created a XUL page that can handle text encryption and decryption when the user enters the text into the text box.

Now what I am trying is, once the user composes the email and clicks on the toolbar button, the textbox should be filled with the composed email text. Any idea how to do this?


Solution

  • When you are interested in how something is done in one of the Thunderbird windows, the way to figure out how it is implemented in the XUL DOM is to install the add-on DOM Inspector and use it to investigate what the contents of the DOM looks like. You probably also want, the Element Inspector add-on which is a very useful addition to the DOM Inspector (shift-right-click opens the DOM Inspector to the element clicked). You might also find Stacked Inspector helpful.

    In this case, a quick look with DOM Inspector shows that the edit window is implemented as a HTML document. The document is located under the element <editor id="content-frame">. You will want to access and change the contents of the <BODY>. You should be able to get the element by:

    let messageBody = document.getElementById("content-frame").contentDocument.body;
    

    If that does not work, then:

    let editor = document.getElementById("content-frame");
    let editorDocument = editor.contentDocument;
    let messageBody = editorDocument.getElementsByTagName("body")[0];
    

    Unfortunately, I don't have a basic Thunderbird add-on framework set up so I was not able to test this for you, but the second one at least should work.