selected_html placeholder added. see #368

This commit is contained in:
Mic 2025-05-22 00:51:00 +02:00
parent 5e1ddbf2e4
commit 1e8eb4a1d4
6 changed files with 33 additions and 5 deletions

View file

@ -619,6 +619,10 @@
"message": "Selected text",
"description": ""
},
"placeholder_selected_html": {
"message": "Selected html",
"description": ""
},
"placeholder_additional_text": {
"message": "Additional text",
"description": ""

View file

@ -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;

View file

@ -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':

View file

@ -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;

View file

@ -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();

View file

@ -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{