From 2460e40bf7b8b7cd296c9bf38d1c0ea16857eca4 Mon Sep 17 00:00:00 2001 From: mic Date: Fri, 27 Mar 2026 23:00:33 +0100 Subject: [PATCH] fix removing html elements added by thunderai when extracting text or html from the email. see #710 --- js/mzta-compose-script.js | 32 +++++++++++++------------------- js/mzta-utils.js | 16 ++++++++-------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/js/mzta-compose-script.js b/js/mzta-compose-script.js index f86325e0..1eb3de51 100644 --- a/js/mzta-compose-script.js +++ b/js/mzta-compose-script.js @@ -26,28 +26,22 @@ 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() { +function getCleanBodyHtml() { const clone = document.body.cloneNode(true); for (const selector of MZTA_INJECTED_SELECTORS) { for (const el of clone.querySelectorAll(selector)) { el.remove(); } } - removeMozMainHeader(clone); + for (const table of clone.querySelectorAll('table.moz-main-header')) { + let sibling = table.previousElementSibling; + while (sibling && sibling.tagName === 'DIV') { + const toRemove = sibling; + sibling = sibling.previousElementSibling; + toRemove.remove(); + } + table.remove(); + } return clone; } @@ -174,7 +168,7 @@ switch (message.command) { case "getText": { let t = ''; - const children = getCleanBodyClone().childNodes; + const children = getCleanBodyHtml().childNodes; for (const node of children) { if (node instanceof Element) { if (node.classList.contains('moz-signature')) { @@ -187,11 +181,11 @@ switch (message.command) { } case "getTextOnly": { - return Promise.resolve(getCleanBodyClone().innerText); + return Promise.resolve(getCleanBodyHtml().innerText); } case "getFullHtml": { - return Promise.resolve(getCleanBodyClone().innerHTML); + return Promise.resolve(getCleanBodyHtml().innerHTML); } case "getOnlyTypedText": { diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 71f876b3..985b9d44 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -272,15 +272,15 @@ export function htmlBodyToPlainText(htmlString) { } 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(); + for (const table of root.querySelectorAll('table.moz-main-header')) { + let sibling = table.previousElementSibling; + while (sibling && sibling.tagName === 'DIV') { + const toRemove = sibling; + sibling = sibling.previousElementSibling; + toRemove.remove(); + } + table.remove(); } - table.remove(); } export function cleanupNewlines(text) {