/*
* ThunderAI [https://micz.it/thunderbird-addon-thunderai/]
* Copyright (C) 2024 - 2025 Mic (m@micz.it)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
"/, '
').replace(/"<\/p>$/, '
'); // strip quotation marks //console.log(">>>>>>>>>>>> fullTextHTMLAtAssignment: " + fullTextHTMLAtAssignment); actionButton.addEventListener('click', async () => { switch(promptData.action) { case "1": // do reply // console.log("[ThunderAI] (do reply) fullTextHTMLAtAssignment: " + fullTextHTMLAtAssignment); await browser.runtime.sendMessage({command: "chatgpt_replyMessage", text: fullTextHTMLAtAssignment, tabId: promptData.tabId, mailMessageId: promptData.mailMessageId}); browser.runtime.sendMessage({command: "chatgpt_close", window_id: (await browser.windows.getCurrent()).id}); break; case "2": // replace text // console.log("[ThunderAI] (replace text) fullTextHTMLAtAssignment: " + fullTextHTMLAtAssignment); await browser.runtime.sendMessage({command: "chatgpt_replaceSelectedText", text: fullTextHTMLAtAssignment, tabId: promptData.tabId, mailMessageId: promptData.mailMessageId}); browser.runtime.sendMessage({command: "chatgpt_close", window_id: (await browser.windows.getCurrent()).id}); break; } }); const closeButton = document.createElement('button'); closeButton.textContent = browser.i18n.getMessage("chatgpt_win_close"); closeButton.addEventListener('click', async () => { // console.log("[ThunderAI] (close) fullTextHTMLAtAssignment: " + fullTextHTMLAtAssignment); browser.runtime.sendMessage({command: "chatgpt_close", window_id: (await browser.windows.getCurrent()).id}); // close window }); if(promptData.action != 0) { actionButtons.appendChild(actionButton); } // diff viewer button if(promptData.prompt_info?.use_diff_viewer == "1") { const diffvButton = document.createElement('button'); diffvButton.textContent = 'Show differences'; diffvButton.addEventListener('click', async () => { let strippedText = fullTextHTMLAtAssignment.replace(/<\/?[^>]+(>|$)/g, ""); let originalText = promptData.prompt_info?.selection_text; if((originalText == null) || (originalText == "")) { originalText = promptData.prompt_info?.body_text; } this.appendDiffViewer(originalText, strippedText); diffvButton.disabled = true; }); actionButtons.appendChild(diffvButton); } actionButtons.appendChild(closeButton); this.messages.appendChild(actionButtons); this.scrollToBottom(); } addDivider() { const divider = document.createElement('hr'); this.messages.appendChild(divider); this.scrollToBottom(); } appendDiffViewer(originalText, newText) { const wordDiff = Diff.diffWords(originalText, newText); const messageElement = document.createElement('div'); messageElement.classList.add('message', 'bot'); // Iterate over each part of the diff to create the HTML output wordDiff.forEach(part => { const diffElement = document.createElement("span"); // Apply a different class depending on whether the word is added, removed, or unchanged if (part.added) { diffElement.className = "added"; diffElement.textContent = part.value; } else if (part.removed) { diffElement.className = "removed"; diffElement.textContent = part.value; } else { diffElement.textContent = part.value; } // Add the element to the container messageElement.appendChild(diffElement); }); const header = document.createElement('h2'); header.textContent = "Diff viewer"; this.messages.appendChild(header); this.messages.appendChild(messageElement); this.addDivider(); this.scrollToBottom(); } flushAccumulatingMessage() { if (this.accumulatingMessageEl) { // Collect all tokens in a full text let fullText = ''; this.accumulatingMessageEl.querySelectorAll('.token').forEach(tokenEl => { fullText += tokenEl.textContent; }); // Convert Markdown to DOM nodes using the markdown-it library const md = window.markdownit(); const html = md.render(fullText); this.fullTextHTML += html; // Create a new DOM parser const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); // Remove existing tokens while (this.accumulatingMessageEl.firstChild) { this.accumulatingMessageEl.removeChild(this.accumulatingMessageEl.firstChild); } // Append new nodes Array.from(doc.body.childNodes).forEach(node => { this.accumulatingMessageEl.appendChild(node); }); this.accumulatingMessageEl = null; } } } customElements.define('messages-area', MessagesArea);