diff --git a/_locales/de/messages.json b/_locales/de/messages.json index dabc7ec9..d46fed2e 100644 --- a/_locales/de/messages.json +++ b/_locales/de/messages.json @@ -386,5 +386,21 @@ "customprompts_form_label_define_response_lang": { "message": "Definieren Sie die Antwortsprache im Prompt.", "description": "" + }, + "prefs_Connection_type": { + "message": "Verbindungstyp", + "description": "" + }, + "prefs_Connection_type_ChatGTP_Web": { + "message": "ChatGTP Weboberfläche", + "description": "" + }, + "prefs_Connection_type_ChatGTP_API": { + "message": "ChatGTP OpenAI API", + "description": "" + }, + "prefs_prefs_ChatGPT_API_Key": { + "message": "ChatGPT API-Schlüssel", + "description": "" } } \ No newline at end of file diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 1d516cd8..38132dd1 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -386,5 +386,21 @@ "customprompts_form_label_define_response_lang": { "message": "Define the reponse languange in the prompt", "description": "" + }, + "prefs_Connection_type": { + "message": "Connection Type", + "description": "" + }, + "prefs_Connection_type_ChatGTP_Web": { + "message": "ChatGTP Web Interface", + "description": "" + }, + "prefs_Connection_type_ChatGTP_API": { + "message": "ChatGTP OpenAI API", + "description": "" + }, + "prefs_prefs_ChatGPT_API_Key": { + "message": "ChatGPT API Key", + "description": "" } } \ No newline at end of file diff --git a/_locales/fr/messages.json b/_locales/fr/messages.json index 6bd52602..5c168dbe 100644 --- a/_locales/fr/messages.json +++ b/_locales/fr/messages.json @@ -386,5 +386,21 @@ "customprompts_form_label_define_response_lang": { "message": "Définir la langue de réponse dans le prompt.", "description": "" + }, + "prefs_Connection_type": { + "message": "Type de connexion", + "description": "" + }, + "prefs_Connection_type_ChatGTP_Web": { + "message": "Interface Web ChatGTP", + "description": "" + }, + "prefs_Connection_type_ChatGTP_API": { + "message": "API OpenAI ChatGTP", + "description": "" + }, + "prefs_prefs_ChatGPT_API_Key": { + "message": "Clé API ChatGPT", + "description": "" } } \ No newline at end of file diff --git a/_locales/it/messages.json b/_locales/it/messages.json index 741b8af5..2c5fdebd 100644 --- a/_locales/it/messages.json +++ b/_locales/it/messages.json @@ -386,5 +386,21 @@ "customprompts_form_label_define_response_lang": { "message": "Definisci la lingua di risposta nel prompt.", "description": "" + }, + "prefs_Connection_type": { + "message": "Tipo di connessione", + "description": "" + }, + "prefs_Connection_type_ChatGTP_Web": { + "message": "Interfaccia Web ChatGTP", + "description": "" + }, + "prefs_Connection_type_ChatGTP_API": { + "message": "API OpenAI per ChatGTP", + "description": "" + }, + "prefs_prefs_ChatGPT_API_Key": { + "message": "Chiave API ChatGPT", + "description": "" } } \ No newline at end of file diff --git a/mzta-background.js b/mzta-background.js index 772e627d..3da7f525 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -147,7 +147,8 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_ return; } - if(!prefs.api_active){ + switch(prefs.connection_type){ + case 'chatgpt_web': // We are using the ChatGPT web interface let newWindow = await browser.windows.create({ url: "https://chatgpt.com", @@ -198,34 +199,35 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_ .catch(err => { console.error("[ThunderAI] ChatGPT web interface error injecting the script: ", err); }); - }else{ + break; // chatgpt_web + case 'chatgpt_api': // We are using the ChatGPT API - let newWindow = await browser.windows.create({ + let newWindow2 = await browser.windows.create({ url: browser.runtime.getURL('api_webchat/index.html'), type: "popup", width: prefs.chatgpt_win_width, height: prefs.chatgpt_win_height }); - createdWindowID = newWindow.id; - let createdTab = newWindow.tabs[0]; + createdWindowID = newWindow2.id; + let createdTab2 = newWindow2.tabs[0]; // Wait for tab loaded. // TODO This is needed in 128+, but doesn't work in 115 // await new Promise(resolve => { - // const tabIsLoaded = tab => { + // const tabIsLoaded2 = tab => { // return tab.status == "complete" && tab.url != "about:blank"; // }; - // const listener = (tabId, changeInfo, updatedTab) => { - // if (tabIsLoaded(updatedTab)) { - // browser.tabs.onUpdated.removeListener(listener); + // const listener2 = (tabId, changeInfo, updatedTab) => { + // if (tabIsLoaded2(updatedTab)) { + // browser.tabs.onUpdated.removeListener(listener2); // resolve(); // } // } // // Early exit if loaded already - // if (tabIsLoaded(createdTab)) { + // if (tabIsLoaded2(createdTab2)) { // resolve(); // } else { - // browser.tabs.onUpdated.addListener(listener); + // browser.tabs.onUpdated.addListener(listener2); // } // }); @@ -233,7 +235,8 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_ 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}); + browser.tabs.sendMessage(createdTab2.id, { command: "chatgpt_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId}); + break; // chatgpt_api } } diff --git a/options/mzta-options-default.js b/options/mzta-options-default.js index e3862035..bfcc95e1 100644 --- a/options/mzta-options-default.js +++ b/options/mzta-options-default.js @@ -22,6 +22,6 @@ export const prefs_default = { chatpgt_use_gpt4: false, chatgpt_keep_formatting: false, default_chatgpt_lang: '', - api_active: false, + connection_type: 'chatgpt_web', //Other values: 'chatgpt_api' api_key_chatgpt: '', } \ No newline at end of file diff --git a/options/mzta-options.html b/options/mzta-options.html index 0a8d8b15..66030506 100644 --- a/options/mzta-options.html +++ b/options/mzta-options.html @@ -6,7 +6,7 @@
__MSG_prefs_OptionText_release_notes__
-
__MSG_prefs_status_page__
+
__MSG_prefs_status_page__
@@ -60,6 +60,22 @@ + + + + + @@ -70,7 +86,7 @@ - + @@ -81,24 +97,13 @@ - + - - - - diff --git a/options/mzta-options.js b/options/mzta-options.js index e8c96862..c7fb38f8 100644 --- a/options/mzta-options.js +++ b/options/mzta-options.js @@ -45,9 +45,10 @@ function saveOptions(e) { browser.storage.sync.set(options); } -function restoreOptions() { +async function restoreOptions() { function setCurrentChoice(result) { document.querySelectorAll(".option-input").forEach(element => { + //console.log("[ThunderAI] Options restoring " + element.id + " = " + result[element.id]); switch (element.type) { case 'checkbox': element.checked = result[element.id] || false; @@ -67,6 +68,7 @@ function restoreOptions() { if (element.tagName === 'SELECT') { let default_select_value = ''; if(element.id == 'reply_type') default_select_value = 'reply_all'; + if(element.id == 'connection_type') default_select_value = 'chatgpt_web'; element.value = result[element.id] || default_select_value; if (element.value === '') { element.selectedIndex = -1; @@ -78,20 +80,47 @@ function restoreOptions() { }); } - function onError(error) { - console.log(`[ThunderAI] Error: ${error}`); - } - - let getting = browser.storage.sync.get(null); - getting.then(setCurrentChoice, onError); + let getting = await browser.storage.sync.get(prefs_default); + setCurrentChoice(getting); } -document.addEventListener('DOMContentLoaded', () => { - restoreOptions(); +function showConnectionOptions() { + let chatgpt_web_display = 'table-row'; + let chatgpt_api_display = 'none'; + if (document.getElementById("connection_type").value === "chatgpt_web") { + chatgpt_web_display = 'table-row'; + }else{ + chatgpt_web_display = 'none'; + } + if (document.getElementById("connection_type").value === "chatgpt_api") { + chatgpt_api_display = 'table-row'; + }else{ + chatgpt_api_display = 'none'; + } + document.querySelectorAll(".conntype_chatgpt_web").forEach(element => { + element.style.display = chatgpt_web_display; + }); + document.querySelectorAll(".conntype_chatgpt_api").forEach(element => { + element.style.display = chatgpt_api_display; + }); +} + +function warnAPIKeyEmpty() { + let apiKeyInput = document.getElementById('api_key_chatgpt'); + if(apiKeyInput.value === ''){ + apiKeyInput.style.border = '2px solid red'; + }else{ + apiKeyInput.style.border = 'none'; + } +} + +document.addEventListener('DOMContentLoaded', async () => { + await restoreOptions(); i18n.updateDocument(); document.querySelectorAll(".option-input").forEach(element => { element.addEventListener("change", saveOptions); }); + showConnectionOptions(); document.getElementById('btnManagePrompts').addEventListener('click', () => { // check if the tab is already there browser.tabs.query({url: browser.runtime.getURL('../customprompts/mzta-custom-prompts.html')}).then((tabs) => { @@ -104,4 +133,7 @@ document.addEventListener('DOMContentLoaded', () => { } }) }); + document.getElementById("connection_type").addEventListener("change", showConnectionOptions); + document.getElementById("connection_type").addEventListener("change", warnAPIKeyEmpty); + document.getElementById("api_key_chatgpt").addEventListener("change", warnAPIKeyEmpty); }, { once: true });
__MSG_prefs_OptionText_chatgpt_win_text__
+ + + +
-
-