diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 189a8e15..8186e579 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -58,4 +58,50 @@ function extractEmail(text) { const emailRegex = /[\w.-]+@[\w.-]+\.\w+/; const match = text.match(emailRegex); return match ? match[0] : ''; -} \ No newline at end of file +} + + +export async function replaceBody(tabId, text){ + let composeDetails = await messenger.compose.getComposeDetails(tabId); + let originalHtmlBody = composeDetails.body; + console.log('originalHtmlBody: ' + originalHtmlBody); + let fullBody = insertText(text, originalHtmlBody); + console.log('fullBody: ' + fullBody); + await messenger.compose.setComposeDetails(tabId, {body: fullBody}); +} + + +// The following methods are a modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js + +const insertText = function (text, fullBody_string) { + const parser = new DOMParser(); + let fullBody = parser.parseFromString(fullBody_string, "text/html"); + const prefix = fullBody.getElementsByClassName("moz-cite-prefix"); + if (prefix.length > 0) { + const divider = prefix[0]; + let sibling = divider.previousSibling; + while (sibling) { + fullBody.body.removeChild(sibling); + sibling = divider.previousSibling; + } + } + makeParagraphs(text, function (p) { + fullBody.body.insertBefore(p, fullBody.body.firstChild); + }); + return fullBody.body.innerHTML; +} + +const makeParagraphs = (text, func) => { + const chunks = text.split(/\n{2,}/); + if (chunks.length == 1) { + return func(document.createTextNode(text)); + } + const paragraphs = chunks.map((t) => { + const p = document.createElement("p"); + p.innerText = t; + return p; + }); + for (let i = paragraphs.length - 1; i >= 0; i--) { + func(paragraphs[i]); + } +}; \ No newline at end of file