ThunderBird Event for Viewing a Message
-
19-06-2021 - |
题
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?
解决方案
If what you want is something similar to a Greasemonkey script that would run on every message, you should:
- Wait for the
load
event of thewindow
. - Retrieve the message pane object with
document.getElementById("messagepane")
. - Bind your handler to the message pane's
DOMContentLoaded
event, or similar events likeload
depending on when exactly you want your handler to be called.DOMContentLoaded
will give you a Greasemonkey-style behavior. - 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.
其他提示
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.
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.
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 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 />
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
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.