stripping printing headers from the html body berfore using it in a placeholder
This commit is contained in:
parent
942c5a6143
commit
61eb858f5a
2 changed files with 36 additions and 2 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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(/ /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')
|
||||
|
|
|
|||
Loading…
Reference in a new issue