From 6494a4f48f3218d03e390e16aac79a9aeffb8cfe Mon Sep 17 00:00:00 2001 From: mic Date: Fri, 18 Apr 2025 14:02:25 +0200 Subject: [PATCH] Refactor getMailBody function to work also with annidated multiparts. see #335 --- js/mzta-utils.js | 58 +++++++++++++++++++++++----------------------- mzta-background.js | 2 +- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/js/mzta-utils.js b/js/mzta-utils.js index e5a846a1..23142a85 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -108,41 +108,41 @@ export async function getMailSubject(tab){ } } -export async function getMailBody(fullMessage){ - let text = ''; - let html = ''; +function extractTextParts(fullMessage) { + const textParts = []; - // console.log(">>>>>>>>>> fullMessage.contentType.trim().toLowerCase(): " + fullMessage.contentType.trim().toLowerCase()); - // console.log(">>>>>>>>>> fullMessage.body: " + fullMessage.body); - - if (fullMessage.contentType.trim().toLowerCase() === "text/plain") { - text = fullMessage.body; - } - if (fullMessage.contentType.trim().toLowerCase() === "text/html") { - html = fullMessage.body; - } - - if((text == undefined || text == null || text == '') && (html == undefined || html == null || html == '')) { - for (let part of fullMessage.parts) { - if (part.contentType.trim().toLowerCase() === "text/plain") { - text = part.body; - } - if (part.contentType.trim().toLowerCase() === "text/html") { - html = part.body; - } - if((text == undefined || text == null || text == '') && (html == undefined || html == null || html == '')) { - for (let subpart of part.parts) { - if (subpart.contentType.trim().toLowerCase() === "text/plain") { - text = subpart.body; - } - if (subpart.contentType.trim().toLowerCase() === "text/html") { - html = subpart.body; - } + function walkParts(parts) { + for (const part of parts) { + if (part.parts && part.parts.length > 0) { + // Recursively walk through sub-parts + walkParts(part.parts); + } else { + // Check if contentType starts with "text/" + if (part.contentType && part.contentType.startsWith("text/")) { + textParts.push(part); } } } } + + if (fullMessage.parts && fullMessage.parts.length > 0) { + walkParts(fullMessage.parts); + } + + return textParts; +} +export function getMailBody(fullMessage){ + const textParts = extractTextParts(fullMessage); + let text = ""; + let html = ""; + for (const part of textParts) { + if (part.contentType === "text/plain") { + text += part.body; + } else if (part.contentType === "text/html") { + html += part.body; + } + } return {text, html}; } diff --git a/mzta-background.js b/mzta-background.js index 035de46d..aaeaa2fc 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -808,7 +808,7 @@ async function processEmails(messages, addTagsAuto, spamFilter) { if (addTagsAuto || spamFilter) { curr_fullMessage = await browser.messages.getFull(message.id); - msg_text = await getMailBody(curr_fullMessage); + msg_text = getMailBody(curr_fullMessage); body_text = msg_text.text.replace(/\s+/g, ' ').trim(); }