diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 24ad1c4c..ad1d5da9 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -834,5 +834,13 @@ "addtags_exclude_tag": { "message": "Exclude tag", "description": "" + }, + "addtags_no_tags_received": { + "message": "No tags received from the AI.", + "description": "" + }, + "addtags_no_valid_tags": { + "message": "No valid tags found, after filtering with the exclusion list.", + "description": "" } } \ No newline at end of file diff --git a/js/mzta-compose-script.js b/js/mzta-compose-script.js index 1f67743f..517a9d6a 100644 --- a/js/mzta-compose-script.js +++ b/js/mzta-compose-script.js @@ -167,7 +167,7 @@ switch (message.command) { case "getTags": console.log(">>>>>>>>>>>>>> getTags: " + JSON.stringify(message.tags)); - // These methods are also defined in the file /js/mzta-addatags-exclusion-list.js + // ===== These methods are also defined in the file /js/mzta-addatags-exclusion-list.js async function addTags_getExclusionList() { let prefs_excluded_tags = await browser.storage.local.get({add_tags_exclusions: []}); console.log(">>>>>>>>>>>>>>> addTags_getExclusionList prefs_excluded_tags: " + JSON.stringify(prefs_excluded_tags)); @@ -177,7 +177,7 @@ switch (message.command) { function addTags_setExclusionList(add_tags_exclusions) { browser.storage.local.set({add_tags_exclusions: add_tags_exclusions}); } - // ================================================================================ + // =================================================================================== // Create and append the styles const style = document.createElement('style'); @@ -235,6 +235,10 @@ switch (message.command) { font-size: 14px; } + .mzta_dialog_message2{ + margin: 20px auto; + } + .div_btns{ width: 90%; display: flex; @@ -286,32 +290,47 @@ switch (message.command) { const content = document.createElement('div'); content.className = 'mzta_dialog_content'; + inputString = ''; //TEST + let no_submit = false; + // Parse the input string into labels - const words = inputString.split(',').map(word => word.trim()); + const words = inputString.split(',').map(word => word.trim()).filter(word => word !== ''); console.log(">>>>>>>>>>>>> words: " + JSON.stringify(words)); + if(words.length == 0){ + const message = document.createElement('div'); + message.className = 'mzta_dialog_message2'; + message.textContent = browser.i18n.getMessage("addtags_no_tags_received"); + content.appendChild(message); + no_submit = true; + } + let prefs_tags = await browser.storage.sync.get({add_tags_hide_exclusions: false}); let add_tags_exclusions_list = await addTags_getExclusionList(); console.log(">>>>>>>>>>>>> add_tags_exclusions_list: " + JSON.stringify(add_tags_exclusions_list)); - const words_final = words.map(word => { - const isExcluded = add_tags_exclusions_list.some(exclusion => - word.includes(exclusion) - ); - - return { - tag: word, - excluded: isExcluded ? 1 : 0 - }; - }); + const words_final = words + .filter(word => word !== '') + .map(word => { + const isExcluded = add_tags_exclusions_list.some(exclusion => + word.includes(exclusion) + ); + + return { + tag: word, + excluded: isExcluded ? 1 : 0 + }; + }); console.log(">>>>>>>>>>>>> words_final: " + JSON.stringify(words_final)); // Create the form const form = document.createElement('form'); + let tags_shown = 0; + words_final.forEach(word => { const label = document.createElement('label'); label.style.display = 'block'; @@ -346,6 +365,7 @@ switch (message.command) { label.appendChild(img); label.appendChild(document.createTextNode(` ${word.tag}`)); if(!word.excluded || (word.excluded && !prefs_tags.add_tags_hide_exclusions)){ + tags_shown++; form.appendChild(label); } if(word.excluded){ @@ -354,6 +374,14 @@ switch (message.command) { } }); + if((!no_submit) && (tags_shown == 0)){ + const message = document.createElement('div'); + message.className = 'mzta_dialog_message2'; + message.textContent = browser.i18n.getMessage("addtags_no_valid_tags"); + content.appendChild(message); + no_submit = true; + } + // Add a div for buttons const buttonsDiv = document.createElement('div'); buttonsDiv.className = 'div_btns'; @@ -367,18 +395,20 @@ switch (message.command) { closeButton.addEventListener('click', closeDialog); buttonsDiv.appendChild(closeButton); - // Add the submit button - const submitButton = document.createElement('button'); - submitButton.type = 'button'; - submitButton.id = 'mzta_dialog_submit'; - submitButton.textContent = 'Submit'; - submitButton.className = 'mzta_dialog_btn'; - submitButton.addEventListener('click', () => { - const selected = Array.from(form.querySelectorAll('input[type=checkbox]:checked')).map(cb => cb.value); - onSubmit(selected); - closeDialog(); - }); - buttonsDiv.appendChild(submitButton); + if(!no_submit){ + // Add the submit button + const submitButton = document.createElement('button'); + submitButton.type = 'button'; + submitButton.id = 'mzta_dialog_submit'; + submitButton.textContent = 'Submit'; + submitButton.className = 'mzta_dialog_btn'; + submitButton.addEventListener('click', () => { + const selected = Array.from(form.querySelectorAll('input[type=checkbox]:checked')).map(cb => cb.value); + onSubmit(selected); + closeDialog(); + }); + buttonsDiv.appendChild(submitButton); + } content.appendChild(form); tags_dialog.appendChild(content); diff --git a/js/mzta-menus.js b/js/mzta-menus.js index c16efee5..3fa3e0ee 100644 --- a/js/mzta-menus.js +++ b/js/mzta-menus.js @@ -206,23 +206,29 @@ export class mzta_Menus { switch(curr_prompt.id){ case 'prompt_add_tags': // Add tags to the email let mail_tags = ''; - let prefs_maxt = await browser.storage.sync.get({add_tags_maxnum: 3}); - let add_tags_maxnum = prefs_maxt.add_tags_maxnum; + let prefs_at = await browser.storage.sync.get({add_tags_maxnum: 3, connection_type: ''}); + if((prefs_at.connection_type === '')||(prefs_at.connection_type === null)||(prefs_at.connection_type === undefined)||(prefs_at.connection_type === 'chatgpt_web')){ + console.error("[ThunderAI | AddTags] Invalid connection type: " + prefs_at.connection_type); + return {ok:'0'}; + } + let add_tags_maxnum = prefs_at.add_tags_maxnum; if(add_tags_maxnum > 0){ fullPrompt += " " + browser.i18n.getMessage("prompt_add_tags_maxnum") + " " + add_tags_maxnum +"."; } this.logger.log("fullPrompt: " + fullPrompt); - // TODO: use the current API, abrto if using chatgpt web + // TODO: use the current API, abort if using chatgpt web // COMMENTED TO DO TESTS - mail_tags = "recipients, test, home, work, car, light"; - // let cmd_addTags = new mzta_specialCommand_AddTags(fullPrompt,"chatgpt_api",true); - // await cmd_addTags.initWorker(); - // try{ - // mail_tags = await cmd_addTags.sendPrompt(); - // // console.log(">>>>>>>>>>> mail_tags: " + mail_tags); - // }catch(err){ - // console.error("[ThunderAI] Error getting tags: ", err); - // } + // mail_tags = "recipients, test, home, work, car, light"; + let cmd_addTags = new mzta_specialCommand_AddTags(fullPrompt,prefs_at.connection_type,true); + await cmd_addTags.initWorker(); + try{ + mail_tags = await cmd_addTags.sendPrompt(); + // console.log(">>>>>>>>>>> mail_tags: " + mail_tags); + }catch(err){ + console.error("[ThunderAI] Error getting tags: ", err); + browser.tabs.sendMessage(tabs[0].id, { command: "sendAlert", curr_tab_type: tabs[0].type, message: "Error getting tags: " + err }); + return {ok:'0'}; + } this.logger.log("mail_tags: " + mail_tags); browser.tabs.sendMessage(tabs[0].id, {command: "getTags", tags: mail_tags }); return {ok:'1'};