ThunderBird Event for Viewing a Message
Asked Answered
M

4

8

I'm trying to modify a message before it is displayed in the main window in Thunderbird. I can't seem to find 1) An event that will be triggered when a new message is opened/viewed 2) A way to modify the displayed contents of a message.

I believe I need the chrome://messenger/content/messenger.xul overlay, and can use a listener such as:

window.addEventListener( "SOME MAGIC HERE", modify_message_handler, true );

But what that event is, I am unsure, along with what object I will get (a message header?) and how easily I can modify what is displayed.

So the questions are:

  • Do I have the correct overlay?
  • Can this be done with events? If not, how?
  • If so, what event is needed and what object does it pass?
Myramyrah answered 6/7, 2012 at 23:27 Comment(2)
have you found developer.mozilla.org/en/DOM/DOM_event_reference ?Patentee
@Patentee I hadn't seen that, thank you.Myramyrah
C
10

If what you want is something similar to a Greasemonkey script that would run on every message, you should:

  1. Wait for the load event of the window.
  2. Retrieve the message pane object with document.getElementById("messagepane").
  3. Bind your handler to the message pane's DOMContentLoaded event, or similar events like load depending on when exactly you want your handler to be called. DOMContentLoaded will give you a Greasemonkey-style behavior.
  4. In the event handler, event.originalTarget is the document corresponding to the displayed message. You can apply all the usual DOM modification techniques here.

For more details, see this example from the documentation.

Cleaver answered 10/7, 2012 at 19:19 Comment(1)
That appears to be the entry point I need - I'll give it a go (in due time) and report back. Thank you.Myramyrah
S
5

It's not clear where you've tried looking yet, but I'll offer a suggestion: this outdated documentation suggests there is an OnItemPropertyChanged event that you could tap into to listen for the "open" property, which does appear to be defined in the source.

Whether this only applies to folders or both folders and messages, though, is unclear; and you'll have to dig deeper to find out if the properties of the item object passed in will allow you to make changes.

Hope that sheds some light; if you've already been all over that, please let us know what you've learned.

Silicon answered 10/7, 2012 at 18:44 Comment(2)
Thanks for the pointers. I will report back but it might not be for some days - I'm juggling some tasks right now.Myramyrah
See my answer #11371403 for what I found.Myramyrah
O
3

I'll primarily address the thunderbird-extension Tag in your Question.

Thunderbird Extension author Paolo "Kaosmos" has many ready to use tools for your consideration.


HeaderToolsLite:

The editing features (changing headings only or the entire source code of the message) are accessible from the menu "Message" -> "HeaderToolsLite" or from the context menu in message panel.

The edit function of the entire source code should be used with caution, because a wrong change can cause problems in displaying all the messages in the folder.



Attach Extra Tools:

attach easily messages selected in the main window<br />
attach attachments from other messages<br />
attach files from 5 favourite directories<br />
attach files and directories in zipped format<br />



DSN OPTIONS GUI:

an extension that adds a graphical interface to handle the preferences, both global and per account, about "Delivery Status Notification" (DSN).



Useful Links:
Notable Reference 1: Common use cases to modify messages
Notable Reference 2: Thunderbird API for Message Window and Message Header
Notable Reference 3: Building a Thunderbird Extension
Notable Reference 4: Using XUL Thunderbird UI in your Extension

Occlusion answered 10/7, 2012 at 23:5 Comment(0)
M
2

Generic Human set me down the right path with the badly needed link. First thing was to add a load event. I'll paste some of the code with my very primitive understanding:

window.addEventListener("load", function load(event) {
        window.removeEventListener("load", load, false);
        myExtension.init();
}, false);

When this script's overlay loads, run init.

var myExtension = {
  init: function() {
        var appcontent = document.getElementById("appcontent"); // browser app content
        if (appcontent) {
                appcontent.addEventListener("OMContentLoaded", myExtension.onPageLoad, true);
        }
        var messagepane = document.getElementById("messagepane"); // tunderbird message pane
        if(messagepane) {
                messagepane.addEventListener("load", function(event) { myExtension.onPageLoad(event); }, true);
        }
  },

In init I add a listener that calls onPageLoad every time a message is displayed. I don't actually use the appcontent case afaik.

  onPageLoad: function(aEvent) {
                var doc = aEvent.originalTarget;  // doc is document that triggered "onload" event
                // we can now morph the loaded page
                // doc.location is a 'Location' object
                var newDat = mutateBody(doc.body.innerHTML);
                doc.body.innerHTML = newDat;
                aEvent.originalTarget.defaultView.addEventListener("unload", function(event) { myExtension.onPageUnload(event); }, true);
  },

  onPageUnload: function(aEvent) {
    // No action necessary yet
  }
};

The doc.body.textContents seemed an obvious element but it actually didn't maintain formatting (darn HTML) - using innerHTML works much better for my needs.

Myramyrah answered 16/7, 2012 at 23:52 Comment(1)
Mozilla documentation for this can be found here: developer.mozilla.org/en-US/docs/Archive/Add-ons/Code_snippets/…Georginegeorglana

© 2022 - 2024 — McMap. All rights reserved.