MozillaZine

Thunderbird 60 add-on to replace blockquote with div in body

Talk about add-ons and extension development.
protogen
 
Posts: 2
Joined: October 28th, 2019, 10:00 pm

Post Posted October 28th, 2019, 11:24 pm

I've written a simple add-on for Thunderbird 60.9.0 that, upon sending an email, performs a global replace of all html blockquote tags for div tags in the email body.

I did this because I'm sick of the intending that occurs due to the use of blockquote tags. My emails have no indenting when I view them myself (due to me using the ReplyWithHeader add-on which hides the indenting) however all my friends/colleagues who use Outlook still see the indenting, and worse, when they reply to me the indenting reappears at my end (i.e. it is no longer hidden). It drives me nuts!

Anyway, my add-on is working well (first add-on I've written). The code I used is below. I just wanted to get some feedback from the community as to whether what I've done is the best and/or most sensible way to achieve this result.

Thank you all.

Code: Select all
function send_event_handler(event) {

   var msg_compose_window = document.getElementById("msgcomposeWindow");
   var msg_type = msg_compose_window.getAttribute("msgtype");
   if (! (msg_type == nsIMsgCompDeliverMode.Now || msg_type == nsIMsgCompDeliverMode.Later)) return;

   try {
      var editor_type = GetCurrentEditorType();
      if (! (editor_type == "html" || editor_type == "htmlmail")) return;

      var editor = GetCurrentEditor();
      editor.beginTransaction();

      var body = editor.outputToString('text/html', 2);
      body = body.replace("<blockquote", "<div", "g");
      body = body.replace("blockquote>", "div>", "g");
      editor.rebuildDocumentFromSource(body);

      editor.endTransaction();

   } catch (e) {

      Components.utils.reportError(e);
      return false;
   }
}

window.addEventListener("compose-send-message", send_event_handler, true);

protogen
 
Posts: 2
Joined: October 28th, 2019, 10:00 pm

Post Posted October 29th, 2019, 10:07 pm

I discovered today I was using the wrong syntax on the string replace function. Subsequently, it was ignoring the "g" (global) modifier. I've updated to the code below.

Code: Select all
function send_event_handler(event) {

   var msg_compose_window = document.getElementById("msgcomposeWindow");
   var msg_type = msg_compose_window.getAttribute("msgtype");
   if (! (msg_type == nsIMsgCompDeliverMode.Now || msg_type == nsIMsgCompDeliverMode.Later)) return;

   try {
      var editor_type = GetCurrentEditorType();
      if (! (editor_type == "html" || editor_type == "htmlmail")) return;

      var editor = GetCurrentEditor();
      editor.beginTransaction();

      var body = editor.outputToString('text/html', 2);
      body = body.replace(/<blockquote/gi, "<div");
      body = body.replace(/blockquote>/gi, "div>");
      editor.rebuildDocumentFromSource(body);

      editor.endTransaction();

   } catch (e) {

      Components.utils.reportError(e);
      return false;
   }
}

window.addEventListener("compose-send-message", send_event_handler, true);

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 2 guests