From bc3dff856e9fc8c4094e1266f51a7eff5a0807d4 Mon Sep 17 00:00:00 2001 From: mic Date: Tue, 2 Apr 2024 00:50:30 +0200 Subject: [PATCH] COrrectly handling the carraige returns. Fixes #9 --- js/mzta-chatgpt.js | 7 +++++-- mzta-background.js | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/js/mzta-chatgpt.js b/js/mzta-chatgpt.js index 008a7355..a87aaa5c 100644 --- a/js/mzta-chatgpt.js +++ b/js/mzta-chatgpt.js @@ -35,9 +35,11 @@ async function chatgpt_getFromDOM(pos) { strPos = pos.toString().toLowerCase(); let response = ''; if (responseDivs.length) { - if (/last|final/.test(strPos)) // get last response + if (/last|final/.test(strPos)){ // get last response + responseDivs[responseDivs.length - 1].innerHTML = responseDivs[responseDivs.length - 1].innerHTML.replace(/<\\/p>/g, '

\\n\\n').replace(//g, '
\\n'); response = responseDivs[responseDivs.length - 1].textContent; - else { // get nth response + //console.log('chatgpt_getFromDOM: [HTML] ' + responseDivs[responseDivs.length - 1].innerHTML.replace(/]*>/gi, '').replace(/<\\/div>/gi, '').replace(/]*>/gi, '').replace(/<\\/svg>/gi, '').replace(/]*>/gi, '').replace(/<\\/path>/gi, '').replace(/]*>/gi, '').replace(/<\\/text>/gi, '').replace(/]*>/gi, '').replace(/<\\/span>/gi, '').replace(/]*>/gi, '').replace(/<\\/button>/gi, '')); + } else { // get nth response const nthOfResponse = ( // Calculate base number @@ -63,6 +65,7 @@ async function chatgpt_getFromDOM(pos) { response = responseDivs[nthOfResponse - 1].textContent; } response = response.replace(/^ChatGPTChatGPT/, ''); // strip sender name + //console.log('chatgpt_getFromDOM: ' + response); } return response; } diff --git a/mzta-background.js b/mzta-background.js index 733065e1..2e0ef7b9 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -45,10 +45,20 @@ messenger.runtime.onMessage.addListener((message, sender, sendResponse) => { browser.tabs.sendMessage(message.tabId, { command: "replaceSelectedText", text: message.text }); return true; case 'chatgpt_replyMessage': + //console.log('message.text: ' + message.text); + const chunks = message.text.split(/\n{2,}/); + const paragraphs = chunks.map((t) => { + const p = document.createElement("p"); + p.innerText = t; + return p; + }); + const paragraphsHtmlString = paragraphs.map(p => p.outerHTML).join(""); + //console.log('paragraphsHtmlString: ' + paragraphsHtmlString); browser.messageDisplay.getDisplayedMessage(message.tabId).then((mailMessage) => { - browser.compose.beginReply(mailMessage.id, { + browser.compose.beginReply(mailMessage.id, "replyToAll", { type: "reply", - body: message.text + body: paragraphsHtmlString, + isPlainText: false, }) }); break;