diff --git a/js/mzta-compose-script.js b/js/mzta-compose-script.js index 8f379ce0..b7ad0295 100644 --- a/js/mzta-compose-script.js +++ b/js/mzta-compose-script.js @@ -16,16 +16,33 @@ * along with this program. If not, see . */ +// Some original methods are a modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js + +const makeParagraphs = (text, func) => { + const chunks = text.split(/\n{2,}/); + if (chunks.length == 1) { + return func(document.createTextNode(text)); + } + const paragraphs = chunks.map((t) => { + const p = document.createElement("p"); + p.innerText = t; + return p; + }); + for (let i = paragraphs.length - 1; i >= 0; i--) { + func(paragraphs[i]); + } +}; + + browser.runtime.onMessage.addListener(async (message) => { switch (message.command) { case "getSelectedText": return Promise.resolve(window.getSelection().toString()); - case "replaceSelectedText": // TODO + case "replaceSelectedText": const selectedText = window.getSelection().toString(); if (selectedText === '') { - let fullBody = insert(message.text); - await browser.compose.setComposeDetails(message.tabId, {body: fullBody.body.innerHTML}); + return; } const sel = window.getSelection(); if (!sel || sel.type !== "Range" || !sel.rangeCount) { @@ -36,6 +53,7 @@ switch (message.command) { makeParagraphs(message.text, function (p) { r.insertNode(p); }); + browser.runtime.sendMessage({command: "compose_reloadBody", tabId: message.tabId}); break; case "getText": diff --git a/mzta-background.js b/mzta-background.js index 96ba2690..ec981952 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -19,9 +19,11 @@ import { mzta_script } from './js/mzta-chatgpt.js'; import { prefs_default } from './options/mzta-options-default.js'; import { mzta_Menus } from './js/mzta-menus.js'; -import { getCurrentIdentity, replaceBody } from './js/mzta-utils.js'; +import { getCurrentIdentity, getOriginalBody, reloadBody, replaceBody, setBody } from './js/mzta-utils.js'; var createdWindowID = null; +var original_html = ''; +var modified_html = ''; browser.composeScripts.register({ js: [{file: "/js/mzta-compose-script.js"}] @@ -62,7 +64,8 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) => break; case 'chatgpt_replaceSelectedText': //console.log('chatgpt_replaceSelectedText: [' + message.tabId +'] ' + message.text) - browser.tabs.sendMessage(message.tabId, { command: "replaceSelectedText", text: message.text, tabId: message.tabId }); + original_html = await getOriginalBody(message.tabId); + await browser.tabs.sendMessage(message.tabId, { command: "replaceSelectedText", text: message.text, tabId: message.tabId }); return true; case 'chatgpt_replyMessage': const paragraphsHtmlString = message.text; @@ -101,10 +104,15 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) => }); // we need to wait for the compose windows to load the content script //setTimeout(() => browser.tabs.sendMessage(reply_tab.id, { command: "insertText", text: paragraphsHtmlString, tabId: reply_tab.id }), 500); - setTimeout(() => replaceBody(reply_tab.id, paragraphsHtmlString), 500); + setTimeout(async () => await replaceBody(reply_tab.id, paragraphsHtmlString), 500); return true; }); break; + case 'compose_reloadBody': + modified_html = await getOriginalBody(message.tabId); + await setBody(message.tabId, original_html); + await setBody(message.tabId, modified_html); + break; default: break; }