diff --git a/js/mzta-compose-script.js b/js/mzta-compose-script.js index 6b1d7fab..b4091b1e 100644 --- a/js/mzta-compose-script.js +++ b/js/mzta-compose-script.js @@ -26,6 +26,20 @@ const MZTA_INJECTED_SELECTORS = [ '.mzta_dialog', ]; +// Mirrors removeMozMainHeader() from mzta-utils.js. +// Removes the Thunderbird-injected header table and any preceding divs. +function removeMozMainHeader(root) { + const table = root.querySelector('table.moz-main-header'); + if (!table) return; + let sibling = table.previousElementSibling; + while (sibling && sibling.tagName === 'DIV') { + const toRemove = sibling; + sibling = sibling.previousElementSibling; + toRemove.remove(); + } + table.remove(); +} + function getCleanBodyClone() { const clone = document.body.cloneNode(true); for (const selector of MZTA_INJECTED_SELECTORS) { @@ -33,6 +47,7 @@ function getCleanBodyClone() { el.remove(); } } + removeMozMainHeader(clone); return clone; } diff --git a/js/mzta-utils.js b/js/mzta-utils.js index b3048c30..71f876b3 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -181,6 +181,11 @@ export function getMailBody(fullMessage){ } if(html === "") { html = text.replace(/\n/g, "
"); + } else { + const parser = new DOMParser(); + const doc = parser.parseFromString(html, 'text/html'); + removeMozMainHeader(doc.body); + html = doc.body.innerHTML; } return {text, html}; } @@ -246,7 +251,9 @@ export function htmlBodyToPlainText(htmlString) { const parser = new DOMParser(); // Parse the HTML string const doc = parser.parseFromString(htmlString, 'text/html'); - + + removeMozMainHeader(doc.body); + // remove invisible elements https://stackoverflow.com/questions/39813081/queryselector-where-display-is-not-none // return doc; doc.querySelectorAll('[style*="display:none"]').forEach(e => e.remove());//.querySelector('html').children.not(':visible').remove() @@ -263,7 +270,19 @@ export function htmlBodyToPlainText(htmlString) { .replace(/ /gi,"") .trim(); } - + +export function removeMozMainHeader(root) { + const table = root.querySelector('table.moz-main-header'); + if (!table) return; + let sibling = table.previousElementSibling; + while (sibling && sibling.tagName === 'DIV') { + const toRemove = sibling; + sibling = sibling.previousElementSibling; + toRemove.remove(); + } + table.remove(); +} + export function cleanupNewlines(text) { return text .replace(/\r\n/g, '\n')