stripping printing headers from the html body berfore using it in a placeholder

This commit is contained in:
Mic 2026-03-27 00:24:00 +01:00
parent 43ffdd32e1
commit 7d2b811926
2 changed files with 36 additions and 2 deletions

View file

@ -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;
}

View file

@ -181,6 +181,11 @@ export function getMailBody(fullMessage){
}
if(html === "") {
html = text.replace(/\n/g, "<br>");
} 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(/&nbsp;/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')