implemented a loop on exception to send a message to the tab
This commit is contained in:
parent
8d381455b6
commit
43c6c68238
1 changed files with 103 additions and 70 deletions
|
|
@ -167,23 +167,23 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
|
||||||
let createdTab = newWindow.tabs[0];
|
let createdTab = newWindow.tabs[0];
|
||||||
|
|
||||||
// Wait for tab loaded.
|
// Wait for tab loaded.
|
||||||
await new Promise(resolve => {
|
// await new Promise(resolve => {
|
||||||
const tabIsLoaded = tab => {
|
// const tabIsLoaded = tab => {
|
||||||
return tab.status == "complete" && tab.url != "about:blank";
|
// return tab.status == "complete" && tab.url != "about:blank";
|
||||||
};
|
// };
|
||||||
const listener = (tabId, changeInfo, updatedTab) => {
|
// const listener = (tabId, changeInfo, updatedTab) => {
|
||||||
if (tabIsLoaded(updatedTab)) {
|
// if (tabIsLoaded(updatedTab)) {
|
||||||
browser.tabs.onUpdated.removeListener(listener);
|
// browser.tabs.onUpdated.removeListener(listener);
|
||||||
resolve();
|
// resolve();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// Early exit if loaded already
|
// // Early exit if loaded already
|
||||||
if (tabIsLoaded(createdTab)) {
|
// if (tabIsLoaded(createdTab)) {
|
||||||
resolve();
|
// resolve();
|
||||||
} else {
|
// } else {
|
||||||
browser.tabs.onUpdated.addListener(listener);
|
// browser.tabs.onUpdated.addListener(listener);
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
let pre_script = `let mztaStatusPageDesc="`+ browser.i18n.getMessage("prefs_status_page") +`";
|
let pre_script = `let mztaStatusPageDesc="`+ browser.i18n.getMessage("prefs_status_page") +`";
|
||||||
let mztaForceCompletionDesc="`+ browser.i18n.getMessage("chatgpt_force_completion") +`";
|
let mztaForceCompletionDesc="`+ browser.i18n.getMessage("chatgpt_force_completion") +`";
|
||||||
|
|
@ -192,17 +192,28 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
|
||||||
let mztaPromptName="[`+ i18nConditionalGet(prompt_name) +`]";
|
let mztaPromptName="[`+ i18nConditionalGet(prompt_name) +`]";
|
||||||
`;
|
`;
|
||||||
|
|
||||||
browser.tabs.executeScript(createdTab.id, { code: pre_script + mzta_script, matchAboutBlank: false })
|
let done1 = false;
|
||||||
.then(async () => {
|
|
||||||
taLog.log("[ThunderAI] ChatGPT web interface script injected successfully");
|
while(!done1){
|
||||||
let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
|
try {
|
||||||
let mailMessageId = -1;
|
browser.tabs.executeScript(createdTab.id, { code: pre_script + mzta_script, matchAboutBlank: false })
|
||||||
if(mailMessage) mailMessageId = mailMessage.id;
|
.then(async () => {
|
||||||
browser.tabs.sendMessage(createdTab.id, { command: "chatgpt_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId});
|
taLog.log("[ThunderAI] ChatGPT web interface script injected successfully");
|
||||||
})
|
let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
|
||||||
.catch(err => {
|
let mailMessageId = -1;
|
||||||
console.error("[ThunderAI] ChatGPT web interface error injecting the script: ", err);
|
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] ChatGPT web interface error injecting the script: ", err);
|
||||||
|
});
|
||||||
|
done1 = true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[ThunderAI] Error sending message to ChatGPT Web Interface: ', error);
|
||||||
|
done1 = false;
|
||||||
|
}
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
}
|
||||||
break; // chatgpt_web
|
break; // chatgpt_web
|
||||||
|
|
||||||
case 'chatgpt_api':
|
case 'chatgpt_api':
|
||||||
|
|
@ -217,26 +228,26 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
|
||||||
createdWindowID = newWindow2.id;
|
createdWindowID = newWindow2.id;
|
||||||
let createdTab2 = newWindow2.tabs[0];
|
let createdTab2 = newWindow2.tabs[0];
|
||||||
|
|
||||||
if(await isThunderbird128OrGreater()){
|
// if(await isThunderbird128OrGreater()){
|
||||||
// Wait for tab loaded.
|
// // Wait for tab loaded.
|
||||||
await new Promise(resolve => {
|
// await new Promise(resolve => {
|
||||||
const tabIsLoaded2 = tab => {
|
// const tabIsLoaded2 = tab => {
|
||||||
return tab.status == "complete" && tab.url != "about:blank";
|
// return tab.status == "complete" && tab.url != "about:blank";
|
||||||
};
|
// };
|
||||||
const listener2 = (tabId, changeInfo, updatedTab) => {
|
// const listener2 = (tabId, changeInfo, updatedTab) => {
|
||||||
if (tabIsLoaded2(updatedTab)) {
|
// if (tabIsLoaded2(updatedTab)) {
|
||||||
browser.tabs.onUpdated.removeListener(listener2);
|
// browser.tabs.onUpdated.removeListener(listener2);
|
||||||
resolve();
|
// resolve();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// Early exit if loaded already
|
// // Early exit if loaded already
|
||||||
if (tabIsLoaded2(createdTab2)) {
|
// if (tabIsLoaded2(createdTab2)) {
|
||||||
resolve();
|
// resolve();
|
||||||
} else {
|
// } else {
|
||||||
browser.tabs.onUpdated.addListener(listener2);
|
// browser.tabs.onUpdated.addListener(listener2);
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
||||||
let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
|
let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
|
||||||
let mailMessageId2 = -1;
|
let mailMessageId2 = -1;
|
||||||
|
|
@ -252,7 +263,18 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.tabs.sendMessage(createdTab2.id, { command: "api_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId2, do_custom_text: do_custom_text});
|
let done2 = false;
|
||||||
|
|
||||||
|
while(!done2){
|
||||||
|
try {
|
||||||
|
browser.tabs.sendMessage(createdTab2.id, { command: "api_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId2, do_custom_text: do_custom_text});
|
||||||
|
done2 = true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[ThunderAI] Error sending message to OpenAI ChatGPI API: ', error);
|
||||||
|
done2 = false;
|
||||||
|
}
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
}
|
||||||
break; // chatgpt_api
|
break; // chatgpt_api
|
||||||
|
|
||||||
case 'ollama_api':
|
case 'ollama_api':
|
||||||
|
|
@ -267,26 +289,26 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
|
||||||
createdWindowID = newWindow3.id;
|
createdWindowID = newWindow3.id;
|
||||||
let createdTab3 = newWindow3.tabs[0];
|
let createdTab3 = newWindow3.tabs[0];
|
||||||
|
|
||||||
if(await isThunderbird128OrGreater()){
|
// if(await isThunderbird128OrGreater()){
|
||||||
// Wait for tab loaded.
|
// Wait for tab loaded.
|
||||||
await new Promise(resolve => {
|
// await new Promise(resolve => {
|
||||||
const tabIsLoaded3 = tab => {
|
// const tabIsLoaded3 = tab => {
|
||||||
return tab.status == "complete" && tab.url != "about:blank";
|
// return tab.status == "complete" && tab.url != "about:blank";
|
||||||
};
|
// };
|
||||||
const listener3 = (tabId, changeInfo, updatedTab) => {
|
// const listener3 = (tabId, changeInfo, updatedTab) => {
|
||||||
if (tabIsLoaded3(updatedTab)) {
|
// if (tabIsLoaded3(updatedTab)) {
|
||||||
browser.tabs.onUpdated.removeListener(listener3);
|
// browser.tabs.onUpdated.removeListener(listener3);
|
||||||
resolve();
|
// resolve();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// Early exit if loaded already
|
// // Early exit if loaded already
|
||||||
if (tabIsLoaded3(createdTab3)) {
|
// if (tabIsLoaded3(createdTab3)) {
|
||||||
resolve();
|
// resolve();
|
||||||
} else {
|
// } else {
|
||||||
browser.tabs.onUpdated.addListener(listener3);
|
// browser.tabs.onUpdated.addListener(listener3);
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}
|
//}
|
||||||
|
|
||||||
let mailMessage3 = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
|
let mailMessage3 = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
|
||||||
let mailMessageId3 = -1;
|
let mailMessageId3 = -1;
|
||||||
|
|
@ -302,7 +324,18 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.tabs.sendMessage(createdTab3.id, { command: "api_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId3, do_custom_text: do_custom_text});
|
let done3 = false;
|
||||||
|
|
||||||
|
while(!done3){
|
||||||
|
try {
|
||||||
|
await browser.tabs.sendMessage(createdTab3.id, { command: "api_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId3, do_custom_text: do_custom_text});
|
||||||
|
done3 = true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[ThunderAI] Error sending message to Ollama API: ', error);
|
||||||
|
done3 = false;
|
||||||
|
}
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
}
|
||||||
break; // ollama_api
|
break; // ollama_api
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue