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.