replying to the right message. fixes #71

This commit is contained in:
mic 2024-06-18 21:08:03 +02:00
parent 2e6fa9f86a
commit 639fe495a7
2 changed files with 25 additions and 13 deletions

View file

@ -119,7 +119,7 @@ function chatpgt_scrollToBottom () {
catch (err) { console.error('[ThunderAI] ', err); }
}
function addCustomDiv(prompt_action,tabId) {
function addCustomDiv(prompt_action,tabId,mailMessageId) {
// Create <style> element for the CSS
var style = document.createElement('style');
style.textContent = ".mzta-header-fixed {position: fixed;bottom: 0;left: 0;height:100px;width: 100%;background-color: #333;color: white;text-align: center;padding: 10px 0;z-index: 1000;border-top: 3px solid white;}"
@ -205,7 +205,7 @@ function addCustomDiv(prompt_action,tabId) {
btn_ok.textContent = browser.i18n.getMessage("chatgpt_win_get_answer");
btn_ok.onclick = async function() {
const response = await chatgpt_getFromDOM('last');
browser.runtime.sendMessage({command: "chatgpt_replyMessage", text: response, tabId: tabId});
browser.runtime.sendMessage({command: "chatgpt_replyMessage", text: response, tabId: tabId, mailMessageId: mailMessageId});
//console.log(response);
browser.runtime.sendMessage({command: "chatgpt_close"});
};
@ -215,7 +215,7 @@ function addCustomDiv(prompt_action,tabId) {
btn_ok.onclick = async function() {
const response = await chatgpt_getFromDOM('last');
//console.log('replace text: '+tabId)
browser.runtime.sendMessage({command: "chatgpt_replaceSelectedText", text: response, tabId: tabId});
browser.runtime.sendMessage({command: "chatgpt_replaceSelectedText", text: response, tabId: tabId, mailMessageId: mailMessageId});
//console.log(response);
browser.runtime.sendMessage({command: "chatgpt_close"});
};
@ -344,7 +344,7 @@ browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
// User not logged in
alert(browser.i18n.getMessage("chatgpt_user_not_logged_in"));
}else{
addCustomDiv(message.action,message.tabId);
addCustomDiv(message.action,message.tabId,message.mailMessageId);
(async () => {
if(mztaDoCustomText === 1){
current_message = message;

View file

@ -76,14 +76,22 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
replyType = 'replyToSender';
}
//console.log('replyType: ' + replyType);
browser.messageDisplay.getDisplayedMessage(message.tabId).then(async (mailMessage) => {
let reply_tab = await browser.compose.beginReply(mailMessage.id, replyType, {
type: "reply",
//body: paragraphsHtmlString,
isPlainText: false,
identityId: await getCurrentIdentity(mailMessage),
})
// browser.messageDisplay.getDisplayedMessage(message.tabId).then(async (mailMessage) => {
// let reply_tab = await browser.compose.beginReply(mailMessage.id, replyType, {
// type: "reply",
// //body: paragraphsHtmlString,
// isPlainText: false,
// identityId: await getCurrentIdentity(mailMessage),
// })
//console.log(">>>>>>>>>>>> message.mailMessageId: " + message.mailMessageId);
browser.messages.get(message.mailMessageId).then(async (mailMessage) => {
let curr_idn = await getCurrentIdentity(mailMessage)
let reply_tab = await browser.compose.beginReply(mailMessage.id, replyType, {
type: "reply",
//body: paragraphsHtmlString,
isPlainText: false,
identityId: curr_idn,
})
// Wait for tab loaded.
await new Promise(resolve => {
const tabIsLoaded = tab => {
@ -92,6 +100,7 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
const listener = (tabId, changeInfo, updatedTab) => {
if (tabIsLoaded(updatedTab)) {
browser.tabs.onUpdated.removeListener(listener);
console.log(">>>>>>>>>>>> reply_tab: " + tabId);
resolve();
}
}
@ -174,7 +183,10 @@ async function openChatGPT(promptText, action, curr_tabId, do_custom_text = 0) {
browser.tabs.executeScript(createdTab.id, { code: pre_script + mzta_script, matchAboutBlank: false })
.then(async () => {
console.log("[ThunderAI] Script injected successfully");
browser.tabs.sendMessage(createdTab.id, { command: "chatgpt_send", prompt: promptText, action: action, tabId: curr_tabId });
let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
let mailMessageId = -1;
if(mailMessage) mailMessageId = mailMessage.id;
browser.tabs.sendMessage(createdTab.id, { command: "chatgpt_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId});
})
.catch(err => {
console.error("[ThunderAI] Error injecting the script: ", err);