From 1e8eb4a1d4331d82938afca0b7849a97329b3a2b Mon Sep 17 00:00:00 2001 From: Mic Date: Thu, 22 May 2025 00:51:00 +0200 Subject: [PATCH] selected_html placeholder added. see #368 --- _locales/en/messages.json | 4 ++++ js/mzta-compose-script.js | 11 +++++++++++ js/mzta-menus.js | 5 ++++- js/mzta-placeholders.js | 12 +++++++++++- js/mzta-utils-prompt.js | 4 ++-- pages/customprompts/mzta-custom-prompts.js | 2 +- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 82e2f576..ba43e9a6 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -619,6 +619,10 @@ "message": "Selected text", "description": "" }, + "placeholder_selected_html": { + "message": "Selected html", + "description": "" + }, "placeholder_additional_text": { "message": "Additional text", "description": "" diff --git a/js/mzta-compose-script.js b/js/mzta-compose-script.js index cf0e4a94..1f462054 100644 --- a/js/mzta-compose-script.js +++ b/js/mzta-compose-script.js @@ -22,6 +22,17 @@ switch (message.command) { return Promise.resolve(window.getSelection().toString()); } + case "getSelectedHtml": { + const selection = window.getSelection(); + if (!selection || selection.rangeCount === 0) { + return Promise.resolve(''); + } + const range = selection.getRangeAt(0); + const div = document.createElement('div'); + div.appendChild(range.cloneContents()); + return Promise.resolve(div.innerHTML); + } + case "replaceSelectedText": { const selectedText = window.getSelection().toString(); let force_insert = false; diff --git a/js/mzta-menus.js b/js/mzta-menus.js index e93d0327..0ea6213c 100644 --- a/js/mzta-menus.js +++ b/js/mzta-menus.js @@ -82,6 +82,7 @@ export class mzta_Menus { //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" }), + selection_html: await browser.tabs.sendMessage(tabs[0].id, { command: "getSelectedHtml" }), text: await browser.tabs.sendMessage(tabs[0].id, { command: "getTextOnly" }), html: await browser.tabs.sendMessage(tabs[0].id, { command: "getFullHtml" }), only_typed_text: await browser.tabs.sendMessage(tabs[0].id, { command: "getOnlyTypedText", do_autoselect: do_autoselect }), @@ -105,6 +106,7 @@ export class mzta_Menus { let body_text = ''; let selection_text = ''; + let selection_html = msg_text.selection_html; let only_typed_text = ''; let only_quoted_text = ''; only_typed_text = msg_text.only_typed_text.replace(/\s+/g, ' ').trim(); @@ -116,6 +118,7 @@ export class mzta_Menus { } only_quoted_text = msg_text.only_quoted_text.replace(/\s+/g, ' ').trim(); curr_prompt.selection_text = selection_text; + curr_prompt.selection_html = selection_html; body_text = msg_text.text.replace(/\s+/g, ' ').trim(); curr_prompt.body_text = body_text; //open chatgpt window @@ -141,7 +144,7 @@ export class mzta_Menus { break; } - fullPrompt = await taPromptUtils.preparePrompt(curr_prompt, curr_message, chatgpt_lang, selection_text, body_text, await getMailSubject(tabs[0]), msg_text, only_typed_text, only_quoted_text, tags_full_list); + fullPrompt = await taPromptUtils.preparePrompt(curr_prompt, curr_message, chatgpt_lang, selection_text, selection_html, body_text, await getMailSubject(tabs[0]), msg_text, only_typed_text, only_quoted_text, tags_full_list); switch(curr_prompt.id){ case 'prompt_translate_this': diff --git a/js/mzta-placeholders.js b/js/mzta-placeholders.js index 08bbd1d7..c89dd813 100644 --- a/js/mzta-placeholders.js +++ b/js/mzta-placeholders.js @@ -91,6 +91,13 @@ const defaultPlaceholders = [ type: 0, is_default: "1", }, + { + id: 'selected_html', + name: "__MSG_placeholder_selected_html__", + default_value: "", + type: 0, + is_default: "1", + }, { id: 'additional_text', name: "__MSG_placeholder_additional_text__", @@ -301,7 +308,7 @@ export const placeholdersUtils = { return regex.test(text); }, - async getPlaceholdersValues(prompt_text, curr_message, mail_subject, body_text, msg_text, only_typed_text, only_quoted_text, selection_text, tags_full_list) { + async getPlaceholdersValues(prompt_text, curr_message, mail_subject, body_text, msg_text, only_typed_text, only_quoted_text, selection_text, selection_html, tags_full_list) { let currPHs = await placeholdersUtils.extractPlaceholders(prompt_text); // console.log(">>>>>>>>>> currPHs: " + JSON.stringify(currPHs)); let finalSubs = {}; @@ -331,6 +338,9 @@ export const placeholdersUtils = { case 'selected_text': finalSubs['selected_text'] = placeholdersUtils.failSafePlaceholders(selection_text); break; + case 'selected_html': + finalSubs['selected_html'] = placeholdersUtils.failSafePlaceholders(selection_html); + break; case 'author': finalSubs['author'] = placeholdersUtils.failSafePlaceholders(curr_message.author); break; diff --git a/js/mzta-utils-prompt.js b/js/mzta-utils-prompt.js index 8cd0d624..c57cd895 100644 --- a/js/mzta-utils-prompt.js +++ b/js/mzta-utils-prompt.js @@ -29,7 +29,7 @@ export const taPromptUtils = { } }, - async preparePrompt(curr_prompt, curr_message, chatgpt_lang, selection_text, body_text, subject_text, msg_text, only_typed_text, only_quoted_text, tags_full_list){ + async preparePrompt(curr_prompt, curr_message, chatgpt_lang, selection_text, selection_html, body_text, subject_text, msg_text, only_typed_text, only_quoted_text, tags_full_list){ let fullPrompt = ''; if(!placeholdersUtils.hasPlaceholder(curr_prompt.text)){ @@ -37,7 +37,7 @@ export const taPromptUtils = { fullPrompt = curr_prompt.text + (String(curr_prompt.need_signature) == "1" ? " " + await taPromptUtils.getDefaultSignature():"") + " " + chatgpt_lang + " \"" + (selection_text=='' ? body_text : selection_text) + "\" "; }else{ // we have at least a placeholder, do the magic! - let finalSubs = await placeholdersUtils.getPlaceholdersValues(curr_prompt.text, curr_message, subject_text, body_text, msg_text, only_typed_text, only_quoted_text, selection_text, tags_full_list); + let finalSubs = await placeholdersUtils.getPlaceholdersValues(curr_prompt.text, curr_message, subject_text, body_text, msg_text, only_typed_text, only_quoted_text, selection_text, selection_html, tags_full_list); // console.log(">>>>>>>>>> finalSubs: " + JSON.stringify(finalSubs)); let prefs_ph = await browser.storage.sync.get({placeholders_use_default_value: false}); fullPrompt = (placeholdersUtils.replacePlaceholders(curr_prompt.text, finalSubs, prefs_ph.placeholders_use_default_value, true) + (String(curr_prompt.need_signature) == "1" ? " " + await taPromptUtils.getDefaultSignature():"") + " " + chatgpt_lang).trim(); diff --git a/pages/customprompts/mzta-custom-prompts.js b/pages/customprompts/mzta-custom-prompts.js index 802f9e7b..3b3bbeb2 100644 --- a/pages/customprompts/mzta-custom-prompts.js +++ b/pages/customprompts/mzta-custom-prompts.js @@ -966,7 +966,7 @@ function checkPromptsConfigForPlaceholders(textarea){ let tr_ancestor2 = textarea.closest('tr'); let selected_text_element = tr_ancestor2.querySelector('.need_selected') || tr_ancestor2.querySelector('.need_selected_new'); - if(String(textarea.value).indexOf('{%selected_text%}') != -1){ + if((String(textarea.value).indexOf('{%selected_text%}') != -1)||(String(textarea.value).indexOf('{%selected_html%}') != -1)){ if(!selected_text_element.checked){ selected_text_element.closest('.need_selected_span').style.border = '2px solid red'; }else{