From 4842b623d165bca0169a462a20e7800da5a6e9b5 Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 13 Oct 2024 22:30:13 +0200 Subject: [PATCH] first working substitution. see #146 --- js/mzta-menus.js | 59 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/js/mzta-menus.js b/js/mzta-menus.js index 64028c50..2f5cac7b 100644 --- a/js/mzta-menus.js +++ b/js/mzta-menus.js @@ -19,9 +19,10 @@ // Some original methods are derived from https://github.com/ali-raheem/Aify/blob/cfadf52f576b7be3720b5b73af7c8d3129c054da/plugin/html/actions.js import { getPrompts } from './mzta-prompts.js'; -import { getLanguageDisplayName, getMenuContextCompose, getMenuContextDisplay, i18nConditionalGet } from './mzta-utils.js' +import { getLanguageDisplayName, getMenuContextCompose, getMenuContextDisplay, i18nConditionalGet, getMailSubject } from './mzta-utils.js' import { taLogger } from './mzta-logger.js'; - +import { placeholdersUtils, getPlaceholders } from './mzta-placeholders.js'; + export class mzta_Menus { allPrompts = []; @@ -73,8 +74,8 @@ export class mzta_Menus { let curr_menu_entry = {id: curr_prompt.id, is_default: curr_prompt.is_default, name: curr_prompt.name}; - const getHighlight = async () => { - const tabs = await browser.tabs.query({ active: true, currentWindow: true }); + const getHighlight = async (tabs) => { + //const tabs = await browser.tabs.query({ active: true, currentWindow: true }); return {tabId: tabs[0].id, selection: await browser.tabs.sendMessage(tabs[0].id, { command: "getSelectedText" }), text: await browser.tabs.sendMessage(tabs[0].id, { command: "getTextOnly" }) @@ -82,23 +83,21 @@ export class mzta_Menus { }; curr_menu_entry.act = async () => { - const msg_text = await getHighlight(); + const tabs = await browser.tabs.query({ active: true, currentWindow: true }); + const msg_text = await getHighlight(tabs); //check if a selection is needed if(String(curr_prompt.need_selected) == "1" && (msg_text.selection==='')){ //A selection is needed, but nothing is selected! //alert(browser.i18n.getMessage('prompt_selection_needed')); - const tabs = await browser.tabs.query({ active: true, currentWindow: true }); browser.tabs.sendMessage(tabs[0].id, { command: "sendAlert", message : browser.i18n.getMessage('prompt_selection_needed') }); return; } - var body_text = ''; - if (msg_text.selection!=='') { - body_text = msg_text.selection.replace(/\s+/g, ' ').trim(); - } else { - body_text = msg_text.text.replace(/\s+/g, ' ').trim(); - } + let body_text = ''; + let selection_text = ''; + selection_text = msg_text.selection.replace(/\s+/g, ' ').trim(); + body_text = msg_text.text.replace(/\s+/g, ' ').trim(); //open chatgpt window //console.log("Click menu item..."); let chatgpt_lang = ''; @@ -112,9 +111,39 @@ export class mzta_Menus { chatgpt_lang = browser.i18n.getMessage("prompt_lang") + " " + chatgpt_lang + "."; } } + + // let mail_subject = await getMailSubject(tabs[0]); + // console.log(">>>>>>>>>>>>>>> mail_subject: " + mail_subject); - var fullPrompt = curr_prompt.text + (String(curr_prompt.need_signature) == "1" ? " " + await this.getDefaultSignature():"") + " " + chatgpt_lang + " \"" + body_text + "\" "; - + let fullPrompt = ''; + if(!placeholdersUtils.hasPlaceholder(curr_prompt.text)){ + // no placeholders, do as usual + fullPrompt = curr_prompt.text + (String(curr_prompt.need_signature) == "1" ? " " + await this.getDefaultSignature():"") + " " + chatgpt_lang + " \"" + (selection_text=='' ? body_text : selection_text) + "\" "; + }else{ + let mail_subject = await getMailSubject(tabs[0]); + // we have at least a placeholder, do the magic! + let currPHs = await placeholdersUtils.extractPlaceholders(curr_prompt.text); + // console.log(">>>>>>>>>> currPHs: " + JSON.stringify(currPHs)); + let finalSubs = {}; + for(let currPH of currPHs){ + switch(currPH.id){ + case 'mail_body': + finalSubs['mail_body'] = body_text; + break; + case 'mail_subject': + finalSubs['mail_subject'] = mail_subject; + break; + case 'selected_text': + finalSubs['selected_text'] = selection_text; + break; + default: // TODO Manage custom placeholders + break; + } + } + // console.log(">>>>>>>>>> finalSubs: " + JSON.stringify(finalSubs)); + fullPrompt = placeholdersUtils.replacePlaceholders(curr_prompt.text, finalSubs) + (String(curr_prompt.need_signature) == "1" ? " " + await this.getDefaultSignature():"") + " " + chatgpt_lang; + } + switch(curr_prompt.id){ case 'prompt_translate_this': let prefs2 = await browser.storage.sync.get({default_chatgpt_lang: getLanguageDisplayName(browser.i18n.getUILanguage())}); @@ -131,7 +160,7 @@ export class mzta_Menus { break; } - const tabs = await browser.tabs.query({ active: true, currentWindow: true }); + // const tabs = await browser.tabs.query({ active: true, currentWindow: true }); // add custom text if needed //browser.runtime.sendMessage({command: "chatgpt_open", prompt: fullPrompt, action: curr_prompt.action, tabId: tabs[0].id}); this.openChatGPT(fullPrompt, curr_prompt.action, tabs[0].id, curr_prompt.name, curr_prompt.need_custom_text);