Merge pull request #474 from micz/3.6.1_issue_469

3.6.1 issue #469
This commit is contained in:
Mic 2025-08-20 21:13:28 +02:00 committed by GitHub
commit 9ac4675345
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 17 deletions

View file

@ -198,19 +198,26 @@ export function stripHtmlKeepLines(htmlString) {
.replace(/<[^>]*>/g, '') // removes any other HTML tags
.trim(); // removes leading/trailing whitespace
}
export function htmlBodyToPlainText(html) {
return html
.replace(/<head[\s\S]*?<\/head>/gi, '')
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<\/p\s*>/gi, '\n')
.replace(/<p\s*>/gi, '')
.replace(/<\/(div|section|article|li|ul|ol|table|tr|td|th)>/gi, '\n') // newline for block tags
.replace(/<[^>]*>/g, '') // remove all other tags with no extra spaces
.replace(/[ \t]+\n/g, '\n')
.replace(/\n{2,}/g, '\n')
.replace(/[ \t]+/g, ' ')
.trim();
export function htmlBodyToPlainText(htmlString) {
// Create a new DOMParser instance
const parser = new DOMParser();
// Parse the HTML string
const doc = parser.parseFromString(htmlString, 'text/html');
// 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()
doc.querySelectorAll('style').forEach(e => e.remove());//.querySelector('html').children.not(':visible').remove()
// Extract text content
const textContent = doc.body.textContent || "";
// Trim whitespace
return textContent
.replace(/[ \t]+\n/g, '\n')
.replace(/\n{2,}/g, '\n')
.replace(/[ \t]+/g, ' ')
.replace(/&nbsp;/gi,"")
.trim();
}
export function convertNewlinesToBr(text) {

View file

@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "ThunderAI",
"description": "__MSG_extensionDescription__",
"version": "3.6.1",
"version": "3.6.1_issue_469",
"author": "Mic (m@micz.it)",
"homepage_url": "https://micz.it/thunderbird-addon-thunderai/",
"browser_specific_settings": {

View file

@ -976,10 +976,16 @@ async function processEmails(messages, addTagsAuto, spamFilter) {
if (addTagsAuto || spamFilter) {
curr_fullMessage = await browser.messages.getFull(message.id);
msg_text = getMailBody(curr_fullMessage);
body_text = msg_text.text.replace(/\s+/g, ' ').trim();
// body_text = msg_text.text.replace(/\s+/g, ' ').trim();
// if( body_text.length == 0 ){
// taLog.log("No text found in the message body, trying to convert HTML to plain text...");
// body_text = htmlBodyToPlainText(msg_text.html);
// }
taLog.log("Startin from the HTML body if present and converting to plain text...");
body_text = htmlBodyToPlainText(msg_text.html);
if( body_text.length == 0 ){
taLog.log("No text found in the message body, trying to convert HTML to plain text...");
body_text = htmlBodyToPlainText(msg_text.html);
taLog.log("No HTML found in the message body, using plain text...");
body_text = msg_text.text.replace(/\s+/g, ' ').trim();
}
}