correctly handling the modification of the body. see #30 #34

This commit is contained in:
mic 2024-05-01 00:33:58 +02:00
parent 1ec6a610e6
commit 1547409669
2 changed files with 32 additions and 6 deletions

View file

@ -16,16 +16,33 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// 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) => { browser.runtime.onMessage.addListener(async (message) => {
switch (message.command) { switch (message.command) {
case "getSelectedText": case "getSelectedText":
return Promise.resolve(window.getSelection().toString()); return Promise.resolve(window.getSelection().toString());
case "replaceSelectedText": // TODO case "replaceSelectedText":
const selectedText = window.getSelection().toString(); const selectedText = window.getSelection().toString();
if (selectedText === '') { if (selectedText === '') {
let fullBody = insert(message.text); return;
await browser.compose.setComposeDetails(message.tabId, {body: fullBody.body.innerHTML});
} }
const sel = window.getSelection(); const sel = window.getSelection();
if (!sel || sel.type !== "Range" || !sel.rangeCount) { if (!sel || sel.type !== "Range" || !sel.rangeCount) {
@ -36,6 +53,7 @@ switch (message.command) {
makeParagraphs(message.text, function (p) { makeParagraphs(message.text, function (p) {
r.insertNode(p); r.insertNode(p);
}); });
browser.runtime.sendMessage({command: "compose_reloadBody", tabId: message.tabId});
break; break;
case "getText": case "getText":

View file

@ -19,9 +19,11 @@
import { mzta_script } from './js/mzta-chatgpt.js'; import { mzta_script } from './js/mzta-chatgpt.js';
import { prefs_default } from './options/mzta-options-default.js'; import { prefs_default } from './options/mzta-options-default.js';
import { mzta_Menus } from './js/mzta-menus.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 createdWindowID = null;
var original_html = '';
var modified_html = '';
browser.composeScripts.register({ browser.composeScripts.register({
js: [{file: "/js/mzta-compose-script.js"}] js: [{file: "/js/mzta-compose-script.js"}]
@ -62,7 +64,8 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
break; break;
case 'chatgpt_replaceSelectedText': case 'chatgpt_replaceSelectedText':
//console.log('chatgpt_replaceSelectedText: [' + message.tabId +'] ' + message.text) //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; return true;
case 'chatgpt_replyMessage': case 'chatgpt_replyMessage':
const paragraphsHtmlString = message.text; 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 // 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(() => 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; return true;
}); });
break; break;
case 'compose_reloadBody':
modified_html = await getOriginalBody(message.tabId);
await setBody(message.tabId, original_html);
await setBody(message.tabId, modified_html);
break;
default: default:
break; break;
} }