Keeping the quoted text in the reply. Fixes #25.

This commit is contained in:
mic 2024-04-12 23:51:21 +02:00
parent b80ad8be3f
commit 7ec1529cf6
2 changed files with 43 additions and 12 deletions

View file

@ -1,5 +1,7 @@
// Modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js
console.log('mzta-compose-script.js');
const makeParagraphs = (text, func) => {
const chunks = text.split(/\n{2,}/);
if (chunks.length == 1) {
@ -31,6 +33,17 @@ const insert = function (text) {
});
}
const insertReply = function (text) {
const prefix = window.document.body.getElementsByClassName("moz-cite-prefix");
if (prefix.length > 0) {
const divider = prefix[0];
let sibling = divider.previousSibling;
return makeParagraphs(text, function (p) {
window.document.body.insertBefore(p, sibling);
});
}
}
browser.runtime.onMessage.addListener((message) => {
switch (message.command) {
@ -69,6 +82,10 @@ switch (message.command) {
case "getTextOnly":
return Promise.resolve(window.document.body.innerText);
case "insertText":
insertReply(message.text);
break;
default:
// do nothing
break;

View file

@ -46,15 +46,7 @@ messenger.runtime.onMessage.addListener(async (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);
const paragraphsHtmlString = message.text;
let prefs = await browser.storage.sync.get({reply_type: 'reply_all'});
console.log('reply_type: ' + prefs.reply_type);
let replyType = 'replyToAll';
@ -62,12 +54,34 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
replyType = 'replyToSender';
}
console.log('replyType: ' + replyType);
browser.messageDisplay.getDisplayedMessage(message.tabId).then((mailMessage) => {
browser.compose.beginReply(mailMessage.id, replyType, {
browser.messageDisplay.getDisplayedMessage(message.tabId).then(async (mailMessage) => {
let reply_tab = await browser.compose.beginReply(mailMessage.id, replyType, {
type: "reply",
body: paragraphsHtmlString,
//body: paragraphsHtmlString,
isPlainText: false,
})
// Wait for tab loaded.
await new Promise(resolve => {
const tabIsLoaded = tab => {
return tab.status == "complete" && tab.url != "about:blank";
};
const listener = (tabId, changeInfo, updatedTab) => {
if (tabIsLoaded(updatedTab)) {
browser.tabs.onUpdated.removeListener(listener);
resolve();
}
}
// Early exit if loaded already
if (tabIsLoaded(reply_tab)) {
resolve();
} else {
browser.tabs.onUpdated.addListener(listener);
}
});
// we need to wait for the compose windows to load the content script
setTimeout(() => browser.tabs.sendMessage(reply_tab.id, { command: "insertText", text: paragraphsHtmlString }), 500);
return true;
});
break;
default: