improved multipart html body decoding
This commit is contained in:
parent
b235005911
commit
8522f0b7f6
2 changed files with 31 additions and 17 deletions
|
|
@ -145,38 +145,52 @@ export async function getMailSubject(tab){
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractTextParts(fullMessage) {
|
function extractTextParts(fullMessage) {
|
||||||
const textParts = [];
|
const textParts = []
|
||||||
|
|
||||||
function walkParts(parts) {
|
function walkParts(parts) {
|
||||||
for (const part of parts) {
|
for (const part of parts) {
|
||||||
if (part.parts && part.parts.length > 0) {
|
if (part.parts && part.parts.length > 0) {
|
||||||
// Recursively walk through sub-parts
|
walkParts(part.parts)
|
||||||
walkParts(part.parts);
|
}
|
||||||
} else {
|
// console.log(">>>>>>>>>>>> extractTextParts: part.contentType: " + part.contentType + ", part.decryptionStatus: " + part.decryptionStatus + ", part.body: " + part.body);
|
||||||
// Check if contentType starts with "text/"
|
if (part.contentType && part.contentType.startsWith('text/')) {
|
||||||
if (part.contentType && part.contentType.startsWith("text/")) {
|
textParts.push(part)
|
||||||
textParts.push(part);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (fullMessage.parts && fullMessage.parts.length > 0) {
|
if (fullMessage.parts && fullMessage.parts.length > 0) {
|
||||||
walkParts(fullMessage.parts);
|
walkParts(fullMessage.parts)
|
||||||
}
|
}
|
||||||
|
return textParts
|
||||||
return textParts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMailBody(fullMessage){
|
function smartDecode(buf) {
|
||||||
|
try {
|
||||||
|
return new TextDecoder('utf-8', { fatal: true }).decode(buf);
|
||||||
|
} catch (e) {
|
||||||
|
return new TextDecoder('windows-1252').decode(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getMailBody(fullMessage, messageId) {
|
||||||
const textParts = extractTextParts(fullMessage);
|
const textParts = extractTextParts(fullMessage);
|
||||||
let text = "";
|
let text = "";
|
||||||
let html = "";
|
let html = "";
|
||||||
|
// console.log(">>>>>>>>>>>>>> getMailBody: textParts: " + JSON.stringify(textParts));
|
||||||
|
// console.log(">>>>>>>>>>>>>> getMailBody: fullMessage: " + JSON.stringify(fullMessage));
|
||||||
for (const part of textParts) {
|
for (const part of textParts) {
|
||||||
|
let body = part.body;
|
||||||
|
if ((body === undefined || body === "") && messageId && part.partName) {
|
||||||
|
const file = await browser.messages.getAttachmentFile(messageId, part.partName);
|
||||||
|
const buf = await file.arrayBuffer();
|
||||||
|
//const buf = new TextDecoder('utf-8').decode(buf);
|
||||||
|
body = smartDecode(buf);
|
||||||
|
}
|
||||||
if (part.contentType === "text/plain") {
|
if (part.contentType === "text/plain") {
|
||||||
text += part.body;
|
// console.log(">>>>>>>>>>>>>> getMailBody: part.body (TEXT): " + body);
|
||||||
|
text += body ?? "";
|
||||||
} else if (part.contentType === "text/html") {
|
} else if (part.contentType === "text/html") {
|
||||||
html += part.body;
|
// console.log(">>>>>>>>>>>>>> getMailBody: part.body (HTML): " + (body ? body.substring(0, 80) : body));
|
||||||
|
html += body ?? "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(html === "") {
|
if(html === "") {
|
||||||
|
|
|
||||||
|
|
@ -1113,7 +1113,7 @@ async function processEmails(args) {
|
||||||
|
|
||||||
if (addTagsAuto || spamFilter) {
|
if (addTagsAuto || spamFilter) {
|
||||||
curr_fullMessage = await browser.messages.getFull(message.id);
|
curr_fullMessage = await browser.messages.getFull(message.id);
|
||||||
msg_text = getMailBody(curr_fullMessage);
|
msg_text = await getMailBody(curr_fullMessage);
|
||||||
taLog.log("Starting from the HTML body if present and converting to plain text...");
|
taLog.log("Starting from the HTML body if present and converting to plain text...");
|
||||||
body_text = htmlBodyToPlainText(msg_text.html);
|
body_text = htmlBodyToPlainText(msg_text.html);
|
||||||
if( body_text.length == 0 ){
|
if( body_text.length == 0 ){
|
||||||
|
|
@ -1283,7 +1283,7 @@ async function processEmails(args) {
|
||||||
|
|
||||||
// extract body of current message as text
|
// extract body of current message as text
|
||||||
const curr_message_full = await browser.messages.getFull(curr_message.id);
|
const curr_message_full = await browser.messages.getFull(curr_message.id);
|
||||||
const curr_body_full_html = getMailBody(curr_message_full);
|
const curr_body_full_html = await getMailBody(curr_message_full);
|
||||||
let curr_body_full_text = htmlBodyToPlainText(curr_body_full_html.html);
|
let curr_body_full_text = htmlBodyToPlainText(curr_body_full_html.html);
|
||||||
if( curr_body_full_text.length === 0) {
|
if( curr_body_full_text.length === 0) {
|
||||||
taLog.log("No HTML found in the message body, using plain text...");
|
taLog.log("No HTML found in the message body, using plain text...");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue