From 27988f14b03b90f005598c58f4f3b920da72339b Mon Sep 17 00:00:00 2001 From: mic Date: Thu, 9 May 2024 21:38:35 +0200 Subject: [PATCH] Keeping the signature even if it's above the quote. Fixes #45 --- js/mzta-utils.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 24efbfdc..aa49e54f 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -90,15 +90,31 @@ export async function replaceBody(tabId, text){ const insertText = function (text, fullBody_string) { const parser = new DOMParser(); let fullBody = parser.parseFromString(fullBody_string, "text/html"); - const prefix = fullBody.getElementsByClassName("moz-cite-prefix"); - if (prefix.length > 0) { - const divider = prefix[0]; - let sibling = divider.previousSibling; + + // looking for the first quoted mail or the signature, which come first in case of "signature above the quote". + const prefix_quote = fullBody.getElementsByClassName("moz-cite-prefix"); + const prefix_sign = fullBody.getElementsByClassName("moz-signature"); + + let firstElement = null; + if (prefix_quote.length > 0 && prefix_sign.length > 0) { + firstElement = prefix_quote[0].compareDocumentPosition(prefix_sign[0]) & Node.DOCUMENT_POSITION_FOLLOWING ? prefix_quote[0] : prefix_sign[0]; + //console.log('>>>>>>>>>>>>>>> quote and signature found: ' + JSON.stringify(firstElement.innerHTML)) + //console.log('>>>>>>>>>>>>>>> DocPosition: ' + prefix_quote[0].compareDocumentPosition(prefix_sign[0])) + } else if (prefix_quote.length > 0) { + firstElement = prefix_quote[0]; + //console.log('>>>>>>>>>>>>>>> quote found') + } else if (prefix_sign.length > 0) { + firstElement = prefix_sign[0]; + //console.log('>>>>>>>>>>>>>>> signature found') + } + if (firstElement) { + let sibling = firstElement.previousSibling; while (sibling) { fullBody.body.removeChild(sibling); - sibling = divider.previousSibling; + sibling = firstElement.previousSibling; } } + let final_p = document.createElement("p"); final_p.innerHTML = "



"; fullBody.body.insertBefore(final_p, fullBody.body.firstChild);