From 61eb858f5a6e86eea3ca1b6a9ba6f54656775b25 Mon Sep 17 00:00:00 2001 From: Mic Date: Fri, 27 Mar 2026 00:24:00 +0100 Subject: [PATCH] stripping printing headers from the html body berfore using it in a placeholder --- js/mzta-compose-script.js | 15 +++++++++++++++ js/mzta-utils.js | 23 +++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/js/mzta-compose-script.js b/js/mzta-compose-script.js index 34298ba3..a5c796f3 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')