// this is a pointer to the mail TB window var mailWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService() .QueryInterface(Components.interfaces.nsIWindowMediator) .getMostRecentWindow("mail:3pane"); //this is a pointer to the currently selected message var messageDetails = mailWindow.GetLoadedMessage() // if the pointer is valid .. if (messageDetails) { // this is a pointer to the message header of the current message var messageHdr = mailWindow.messenger.messageServiceFromURI(messageDetails).messageURIToMsgHdr(messageDetails); // the date is in Microseconds ! var stdate = new Date(messageHdr.date / 1000); var dateService = Components.classes["@mozilla.org/intl/scriptabledateformat;1"].getService(Components.interfaces.nsIScriptableDateFormat); // format it according to the current locale's date spec var stdt = dateService.FormatDateTime("", dateService.dateFormatLong, dateService.timeFormatSeconds, stdate.getFullYear(), stdate.getMonth()+1, stdate.getDate(), stdate.getHours(), stdate.getMinutes(), stdate.getSeconds()); // this is where we start to create the new header var newheader = '-----Original Message-----\n'; newheader += 'From: ' + messageHdr.author + '\n'; newheader += 'To: ' + messageHdr.recipients + '\n' newheader += 'Sent: ' + stdt + '\n'; newheader += 'Subject: ' + messageHdr.subject; //the new header is done, now lets get the stock header // this is a pointer to the body portion of the compose window var fded = GetCurrentEditor() var fddom = fded.rootElement; // these childnodes are every one of the items already in the body ( the header, quoted text, etc) var fddomnodes = fddom.childNodes; // lets chew through all of the nodes looking for text that matches 'wrote, On' var a_node; for (i=0; i < fddomnodes.length ; i++ ) { a_node = fddomnodes[i]; // some nodes are NOT text (HTML BRs and SPANs) so we will put a try/catch block around the check try { if (a_node.nodeValue.indexOf('wrote, On') > 0) { // found the standard header text, lets change it now try { // lets create a span node to hold our new header var nht = fded.createElementWithDefaults("span"); // we need to change < chars (like from ) to ctrl chars var re = /) to ctrl chars var re = />/gi; newheader = newheader.replace(re,'>'); // lets change all \n (newline) control chars to
var re = /\n/gi; newheader = newheader.replace(re,'
'); // lets set the span node's inner html to the hew header text nht.innerHTML = newheader; // we do not want the caret's position to change while we are adding and deleting nodes fded.setShouldTxnSetSelection(false); fded.beginTransaction(); // this will ALWAYS insert the node just after the node we are going to delete fded.insertNode(nht,fddom,i+1); fded.endTransaction(); // THIS IS VERY IMPORTANT, if we do not do this, TB crashes fded.setShouldTxnSetSelection(true); // ok so we added the node, now lets delete the old header node fded.deleteNode(a_node); } catch(e) { alert(e); } // if the body text is big enough for the scroll bar to show, once we add the new header, // we don't see the caret anymore, this will make scroll the caret into view try { var selectionController = fded.selectionController; selectionController.scrollSelectionIntoView(selectionController.SELECTION_NORMAL, selectionController.SELECTION_ANCHOR_REGION, true); } catch (e) { alert(e) } // lets set i so high that we immediately exit the for loop i = 7000 } } catch (e) {} } } // Done !