diff --git a/api_webchat/controller.js b/api_webchat/controller.js index e0942e93..3666c5db 100644 --- a/api_webchat/controller.js +++ b/api_webchat/controller.js @@ -86,16 +86,21 @@ worker.onmessage = function(event) { // handling commands from the backgound page browser.runtime.onMessage.addListener((message, sender, sendResponse) => { //console.log(">>>>>>>>>>>>> controller.js onMessage: " + JSON.stringify(message)); - if (message.command === "chatgpt_send") { - //send the received prompt to the chatgpt api - if(message.do_custom_text=="1") { - let userInput = prompt(browser.i18n.getMessage("chatgpt_win_custom_text")); - if(userInput !== null) { - message.prompt += " " + userInput; + switch (message.command) { + case "chatgpt_send": + //send the received prompt to the chatgpt api + if(message.do_custom_text=="1") { + let userInput = prompt(browser.i18n.getMessage("chatgpt_win_custom_text")); + if(userInput !== null) { + message.prompt += " " + userInput; + } } - } - promptData = message; - messageInput._setMessageInputValue(message.prompt); - messageInput._handleNewChatMessage(); + promptData = message; + messageInput._setMessageInputValue(message.prompt); + messageInput._handleNewChatMessage(); + break; + case "api_error": + messagesArea.appendBotMessage(message.error,'error'); + break; } }); \ No newline at end of file diff --git a/mzta-background.js b/mzta-background.js index 7d35ecf8..14c095e5 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -152,18 +152,6 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_ return; } - if(prefs.connection_type === 'chatgpt_api'){ - // check if the API is present, otherwise open the web interface - if (prefs.chatgpt_api_key == '') { - showNotification(browser.i18n.getMessage('error'),browser.i18n.getMessage('chatgpt_empty_apikey')); - prefs.connection_type = 'chatgpt_web'; - } - if (prefs.chatgpt_model == '') { - showNotification(browser.i18n.getMessage('error'),browser.i18n.getMessage('chatgpt_empty_model')); - prefs.connection_type = 'chatgpt_web'; - } - } - switch(prefs.connection_type){ case 'chatgpt_web': // We are using the ChatGPT web interface @@ -253,6 +241,16 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_ let mailMessageId = -1; if(mailMessage) mailMessageId = mailMessage.id; + // check if the config is present, or give a message error + if (prefs.chatgpt_api_key == '') { + browser.tabs.sendMessage(createdTab2.id, { command: "api_error", error: browser.i18n.getMessage('chatgpt_empty_apikey')}); + return; + } + if (prefs.chatgpt_model == '') { + browser.tabs.sendMessage(createdTab2.id, { command: "api_error", error: browser.i18n.getMessage('chatgpt_empty_model')}); + return; + } + browser.tabs.sendMessage(createdTab2.id, { command: "chatgpt_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId, do_custom_text: do_custom_text}); break; // chatgpt_api }