diff --git a/CHANGELOG.md b/CHANGELOG.md
index 61672de6..18410985 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,12 @@
+
Version 2.1.2 - 04/09/2024
+
+ - [ChatGPT API] The URL in the prompt sending error message is now correctly clickable [#123].
+ - [ChatGPT API] If the web interface loads slowly, a button is now shown to retry sending the prompt [#122].
+ - [ChatGPT API] Improved the methods to open the ChatGPT window and interact with it.
+
Version 2.1.1 - 28/08/2024
- [ChatGPT API] Fixed a bug in handling the chat window.
diff --git a/_locales/de/messages.json b/_locales/de/messages.json
index 83223218..3b4b343a 100644
--- a/_locales/de/messages.json
+++ b/_locales/de/messages.json
@@ -220,7 +220,17 @@
"description": ""
},
"chatgpt_textarea_not_found_error": {
- "message": "Es gibt einen Fehler beim Senden der Eingabeaufforderung an ChatGPT. Bitte versuchen Sie es erneut. Wenn das Problem weiterhin besteht, überprüfen Sie bitte den Dienststatus.",
+ "message": "Es scheint, dass die ChatGPT-Seite zu lange zum Laden braucht. Wenn sie fertig geladen ist, klicken Sie auf den Button rechts. Wenn das Problem weiterhin besteht, überprüfen Sie bitte den Dienststatus.",
+ "description": "",
+ "placeholders": {
+ "url": {
+ "content": "$1",
+ "example": "https://micz.it/thunderbird-addon-thunderai/status/"
+ }
+ }
+ },
+ "chatgpt_btn_retry": {
+ "message": "Wiederholen",
"description": ""
},
"chatgpt_sendbutton_not_found_error": {
diff --git a/_locales/en/messages.json b/_locales/en/messages.json
index 020f9da9..a1c66406 100644
--- a/_locales/en/messages.json
+++ b/_locales/en/messages.json
@@ -220,7 +220,17 @@
"description": ""
},
"chatgpt_textarea_not_found_error": {
- "message": "There is an error sending the prompt to ChatGPT, please retry. If the problem persists, please check the service status.",
+ "message": "It seems that the ChatGPT page is taking too long to load. If it finishes loading, click the button on the right. If the problem persists, please check the service status.",
+ "description": "",
+ "placeholders": {
+ "url": {
+ "content": "$1",
+ "example": "https://micz.it/thunderbird-addon-thunderai/status/"
+ }
+ }
+ },
+ "chatgpt_btn_retry": {
+ "message": "Retry",
"description": ""
},
"chatgpt_sendbutton_not_found_error": {
diff --git a/_locales/fr/messages.json b/_locales/fr/messages.json
index 0cfdaa7b..749c17e4 100644
--- a/_locales/fr/messages.json
+++ b/_locales/fr/messages.json
@@ -220,7 +220,17 @@
"description": ""
},
"chatgpt_textarea_not_found_error": {
- "message": "Il y a une erreur lors de l'envoi de la proposition à ChatGPT, veuillez réessayer. Si le problème persiste, veuillez vérifier le statut du service.",
+ "message": "Il semble que la page ChatGPT mette trop de temps à charger. Si elle finit de charger, cliquez sur le bouton à droite. Si le problème persiste, veuillez vérifier le statut du service.",
+ "description": "",
+ "placeholders": {
+ "url": {
+ "content": "$1",
+ "example": "https://micz.it/thunderbird-addon-thunderai/status/"
+ }
+ }
+ },
+ "chatgpt_btn_retry": {
+ "message": "Réessayer",
"description": ""
},
"chatgpt_sendbutton_not_found_error": {
diff --git a/_locales/it/messages.json b/_locales/it/messages.json
index 66ae3769..ac8d9028 100644
--- a/_locales/it/messages.json
+++ b/_locales/it/messages.json
@@ -220,7 +220,17 @@
"description": ""
},
"chatgpt_textarea_not_found_error": {
- "message": "C'è stato un errore nell'invio del prompt a ChatGPT, per favore riprova. Se il problema persiste, per favore controlla lo stato del servizio.",
+ "message": "Sembra che la pagina di ChatGPT impieghi troppo tempo a caricarsi. Se finisce di caricarsi, clicca sul pulsante a destra. Se il problema persiste, per favore controlla lo stato del servizio.",
+ "description": "",
+ "placeholders": {
+ "url": {
+ "content": "$1",
+ "example": "https://micz.it/thunderbird-addon-thunderai/status/"
+ }
+ }
+ },
+ "chatgpt_btn_retry": {
+ "message": "Riprova",
"description": ""
},
"chatgpt_sendbutton_not_found_error": {
diff --git a/js/mzta-chatgpt-loader.js b/js/mzta-chatgpt-loader.js
new file mode 100644
index 00000000..ce674db9
--- /dev/null
+++ b/js/mzta-chatgpt-loader.js
@@ -0,0 +1,27 @@
+/*
+ * ThunderAI [https://micz.it/thunderbird-addon-thunderai/]
+ * Copyright (C) 2024 Mic (m@micz.it)
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+const urlParams = new URLSearchParams(window.location.search);
+const call_id = urlParams.get('call_id');
+
+async function page_ready(call_id){
+ await browser.runtime.sendMessage({command: "chatgpt_web_ready_" + call_id});
+}
+
+page_ready(call_id);
+//console.log(">>>>>>>>>>> [ThunderAI] call_id: " + call_id)
\ No newline at end of file
diff --git a/js/mzta-chatgpt.js b/js/mzta-chatgpt.js
index e94fba21..30e30c4f 100644
--- a/js/mzta-chatgpt.js
+++ b/js/mzta-chatgpt.js
@@ -22,8 +22,11 @@
export const mzta_script = `
let force_go = false;
+let do_force_completion = false;
let current_message = null;
let current_action = null;
+let current_tabId = null;
+let current_mailMessageId = null;
let selectionChangeTimeout = null;
let isDragging = false;
@@ -59,19 +62,22 @@ async function chatgpt_sendMsg(msg, method ='') { // return -1 send button
async function chatgpt_isIdle() {
return new Promise(resolve => {
const intervalId = setInterval(() => {
- if (chatgpt_getRegenerateButton()) {
+ if (chatgpt_getRegenerateButton() || do_force_completion) {
clearInterval(intervalId); resolve(true);
}}, 100);});}
function chatgpt_getRegenerateButton() {
for (const mainSVG of document.querySelectorAll('main svg')) {
- if (mainSVG.querySelector('path[d*="M3.07 10.876C3.623"]')) // regen icon found
+ if (mainSVG.querySelector('path[d^="M3.06957"]')) // regen icon found
return mainSVG.parentNode.parentNode;
}
}
function chatpgt_scrollToBottom () {
- try { document.querySelector('button[class*="cursor"][class*="bottom"]').click(); }
+ try {
+ //document.querySelector('button[class*="cursor"][class*="bottom"]').click();
+ document.querySelector('path[d^="M12 21C11.7348"]').parentNode.parentNode.click();
+ }
catch (err) { console.error('[ThunderAI] ', err); }
}
@@ -139,7 +145,7 @@ function addCustomDiv(prompt_action,tabId,mailMessageId) {
force_completion_div.textContent = mztaForceCompletionDesc;
force_completion_div.title = mztaForceCompletionTitle;
force_completion_div.addEventListener('click', function() {
- operation_done();
+ do_force_completion = true;
});
fixedDiv.appendChild(force_completion_div);
@@ -288,7 +294,7 @@ async function doProceed(message, customText = ''){
}
//await chatgpt.isLoaded();
let send_result = await chatgpt_sendMsg(message.prompt+' '+customText,'click');
- console.log(">>>>>>>>>>> send_result: " + send_result);
+ //console.log(">>>>>>>>>>> send_result: " + send_result);
switch(send_result){
case -1: // send button not found
let curr_msg = document.getElementById('mzta-curr_msg');
@@ -297,16 +303,38 @@ async function doProceed(message, customText = ''){
break;
case -2: // textarea not found
let curr_model_warn = document.getElementById('mzta-model_warn');
- curr_model_warn.textContent = browser.i18n.getMessage("chatgpt_textarea_not_found_error");
+ let message = browser.i18n.getMessage("chatgpt_textarea_not_found_error","https://micz.it/thunderbird-addon-thunderai/status/");
+ let parts = message.split(/([^<]+)<\\/a>/);
+ curr_model_warn.textContent = parts[0];
+ let link = document.createElement('a');
+ link.href = parts[1];
+ link.textContent = parts[2];
+ curr_model_warn.appendChild(link);
+ curr_model_warn.appendChild(document.createTextNode(parts[3]));
curr_model_warn.style.display = 'inline-block';
document.getElementById('mzta-curr_msg').textContent = "";
document.getElementById('mzta-loading').style.display = 'none';
+ let btn_retry = document.createElement('button');
+ btn_retry.id="mzta-btn_retry";
+ btn_retry.classList.add('mzta-btn');
+ btn_retry.style.position = 'absolute';
+ btn_retry.style.top = '5px';
+ btn_retry.style.right = '5px';
+ btn_retry.textContent = browser.i18n.getMessage("chatgpt_btn_retry");
+ btn_retry.addEventListener('click', function() {
+ doRetry();
+ });
+ curr_model_warn.insertAdjacentElement('afterend', btn_retry);
break;
}
await chatgpt_isIdle();
operation_done();
}
+function doRetry(){
+ doProceed(current_message);
+}
+
function removeTagsAndReturnHTML(rootElement, removeTags, preserveTags) {
const fragment = document.createDocumentFragment();
@@ -470,30 +498,50 @@ function selectContentOnClick(event) {
}
}
+function run(){
+ if(!checkLoggedIn()){
+ // User not logged in
+ alert(browser.i18n.getMessage("chatgpt_user_not_logged_in"));
+ }else{
+ addCustomDiv(current_action,current_tabId,current_mailMessageId);
+ (async () => {
+ if(mztaDoCustomText === 1){
+ showCustomTextField();
+ } else {
+ await doProceed(current_message);
+ }
+ // Add an event listener to the document to detect clicks
+ document.addEventListener('mousedown', selectContentOnMouseDown);
+ document.addEventListener('mousemove', selectContentOnMouseMove);
+ document.addEventListener('mouseup', selectContentOnMouseUp);
+ })();
+ }
+}
// In the content script
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
- if (message.command === "chatgpt_send") {
- if(!checkLoggedIn()){
- // User not logged in
- alert(browser.i18n.getMessage("chatgpt_user_not_logged_in"));
- }else{
+ //console.log(">>>>>>>>>>>>> content.js onMessage: " + JSON.stringify(message));
+ switch(message.command) {
+ case "chatgpt_send":
current_action = message.action;
- addCustomDiv(message.action,message.tabId,message.mailMessageId);
- (async () => {
- if(mztaDoCustomText === 1){
- current_message = message;
- showCustomTextField();
- } else {
- await doProceed(message);
- }
- // Add an event listener to the document to detect clicks
- document.addEventListener('mousedown', selectContentOnMouseDown);
- document.addEventListener('mousemove', selectContentOnMouseMove);
- document.addEventListener('mouseup', selectContentOnMouseUp);
- })();
- }
+ current_message = message;
+ current_tabId = message.tabId;
+ current_mailMessageId = message.mailMessageId;
+ run();
+ break;
+ case "chatgpt_alive":
+ sendResponse({isAlive: true});
+ break;
}
});
+let checkTab = setInterval(() => {
+ let customDiv = document.getElementById('mzta-custom_text');
+ if(customDiv){
+ clearInterval(checkTab);
+ }else{
+ run();
+ }
+}, 1000);
+
`
diff --git a/js/mzta-utils.js b/js/mzta-utils.js
index b3d81dfb..9b39307c 100644
--- a/js/mzta-utils.js
+++ b/js/mzta-utils.js
@@ -123,7 +123,7 @@ function compareThunderbirdVersions(v1, v2) {
return 0;
}
-export function generateCallID(length) {
+export function generateCallID(length = 10) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
const charactersLength = characters.length;
diff --git a/manifest.json b/manifest.json
index 31ac877b..184a62cf 100644
--- a/manifest.json
+++ b/manifest.json
@@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "ThunderAI",
"description": "__MSG_extensionDescription__",
- "version": "2.1.1",
+ "version": "2.1.2",
"author": "Mic (m@micz.it)",
"homepage_url": "https://micz.it/thunderbird-addon-thunderai/",
"browser_specific_settings": {
diff --git a/mzta-background.js b/mzta-background.js
index 0820dcc8..42eb0ed6 100644
--- a/mzta-background.js
+++ b/mzta-background.js
@@ -56,6 +56,12 @@ for (let messageTab of messageTabs) {
}
}
+browser.contentScripts.register({
+ matches: ["https://*.chatgpt.com/*"],
+ js: [{file: "js/mzta-chatgpt-loader.js"}],
+ runAt: "document_idle"
+ });
+
messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
// Check what type of message we have received and invoke the appropriate
// handler function.
@@ -161,30 +167,38 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
switch(prefs.connection_type){
case 'chatgpt_web':
+
+ let rand_call_id = '_chatgptweb_' + generateCallID();
+
// We are using the ChatGPT web interface
let newWindow = await browser.windows.create({
- url: "https://chatgpt.com",
+ url: "https://chatgpt.com?call_id=" + rand_call_id,
type: "popup",
width: prefs.chatgpt_win_width,
height: prefs.chatgpt_win_height
});
- taLog.log("[ThunderAI] ChatGPT web interface script started...");
- let createdTab = newWindow.tabs[0];
let newWindowId = newWindow.id;
+ let createdTab = newWindow.tabs[0];
- let pre_script = `let mztaWinId = `+ newWindowId +`;
- let mztaStatusPageDesc="`+ browser.i18n.getMessage("prefs_status_page") +`";
- let mztaForceCompletionDesc="`+ browser.i18n.getMessage("chatgpt_force_completion") +`";
- let mztaForceCompletionTitle="`+ browser.i18n.getMessage("chatgpt_force_completion_title") +`";
- let mztaDoCustomText=`+ do_custom_text +`;
- let mztaPromptName="[`+ i18nConditionalGet(prompt_name) +`]";
- `;
+ const listener = async (message, sender, sendResponse) => {
- let done1 = false;
+ if (message.command === "chatgpt_web_ready_" + rand_call_id) {
- while(!done1){
- try {
+ taLog.log("[ThunderAI] ChatGPT web interface script started...");
+
+ let pre_script = `let mztaWinId = `+ newWindowId +`;
+ let mztaStatusPageDesc="`+ browser.i18n.getMessage("prefs_status_page") +`";
+ let mztaForceCompletionDesc="`+ browser.i18n.getMessage("chatgpt_force_completion") +`";
+ let mztaForceCompletionTitle="`+ browser.i18n.getMessage("chatgpt_force_completion_title") +`";
+ let mztaDoCustomText=`+ do_custom_text +`;
+ let mztaPromptName="[`+ i18nConditionalGet(prompt_name) +`]";
+ `;
+
+ taLog.log("Waiting 1 sec");
+ await new Promise(resolve => setTimeout(resolve, 1000));
+ taLog.log("Waiting 1 sec done");
+
await browser.tabs.executeScript(createdTab.id, { code: pre_script + mzta_script, matchAboutBlank: false });
let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
let mailMessageId = -1;
@@ -192,23 +206,20 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
browser.tabs.sendMessage(createdTab.id, { command: "chatgpt_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId});
taLog.log('[ChatGPT Web] Connection succeded!');
taLog.log("[ThunderAI] ChatGPT Web script injected successfully");
- done1 = true;
- } catch (error) {
- taLog.warn('[ChatGPT Web] Error connecting to the window chat: ', error);
- taLog.warn('[ChatGPT Web] Trying again...');
- done1 = false;
+ browser.runtime.onMessage.removeListener(listener);
}
- await new Promise(resolve => setTimeout(resolve, 500));
}
- break; // chatgpt_web
+
+ browser.runtime.onMessage.addListener(listener);
+ break; // chatgpt_web
case 'chatgpt_api':
// We are using the ChatGPT API
- let rand_call_id = '_openai_' + generateCallID();
+ let rand_call_id2 = '_openai_' + generateCallID();
await browser.windows.create({
- url: browser.runtime.getURL('api_webchat/index.html?llm='+prefs.connection_type+'&call_id='+rand_call_id),
+ url: browser.runtime.getURL('api_webchat/index.html?llm='+prefs.connection_type+'&call_id='+rand_call_id2),
type: "popup",
width: prefs.chatgpt_win_width,
height: prefs.chatgpt_win_height
@@ -216,7 +227,7 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
const listener2 = async (message, sender, sendResponse) => {
- if (message.command === "openai_api_ready_"+rand_call_id) {
+ if (message.command === "openai_api_ready_"+rand_call_id2) {
let newWindow2 = await browser.windows.get(message.window_id, {populate: true});
let createdTab2 = newWindow2.tabs[0];
@@ -250,10 +261,10 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
taLog.log("Ollama API window opening...");
- let rand_call_id2 = '_ollama_' + generateCallID();
+ let rand_call_id3 = '_ollama_' + generateCallID();
await browser.windows.create({
- url: browser.runtime.getURL('api_webchat/index.html?llm='+prefs.connection_type+'&call_id='+rand_call_id2),
+ url: browser.runtime.getURL('api_webchat/index.html?llm='+prefs.connection_type+'&call_id='+rand_call_id3),
type: "popup",
width: prefs.chatgpt_win_width,
height: prefs.chatgpt_win_height
@@ -261,7 +272,7 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
const listener3 = async (message, sender, sendResponse) => {
- if (message.command === "ollama_api_ready_"+rand_call_id2) {
+ if (message.command === "ollama_api_ready_"+rand_call_id3) {
taLog.log("Ollama API window ready.");
taLog.log("message.window_id: " + message.window_id)
let newWindow3 = await browser.windows.get(message.window_id, {populate: true});
diff --git a/options/mzta-release-notes.html b/options/mzta-release-notes.html
index 8e0c81dd..ca115ff4 100644
--- a/options/mzta-release-notes.html
+++ b/options/mzta-release-notes.html
@@ -7,6 +7,12 @@
ThunderAI Release Notes
+
Version 2.1.2 - 04/09/2024
+
+ - [ChatGPT API] The URL in the prompt sending error message is now correctly clickable [#123].
+ - [ChatGPT API] If the web interface loads slowly, a button is now shown to retry sending the prompt [#122].
+ - [ChatGPT API] Improved the methods to open the ChatGPT window and interact with it.
+
Version 2.1.1 - 28/08/2024
- [ChatGPT API] Fixed a bug in handling the chat window.