selected_html placeholder added. see #368
This commit is contained in:
parent
5e1ddbf2e4
commit
1e8eb4a1d4
6 changed files with 33 additions and 5 deletions
|
|
@ -619,6 +619,10 @@
|
||||||
"message": "Selected text",
|
"message": "Selected text",
|
||||||
"description": ""
|
"description": ""
|
||||||
},
|
},
|
||||||
|
"placeholder_selected_html": {
|
||||||
|
"message": "Selected html",
|
||||||
|
"description": ""
|
||||||
|
},
|
||||||
"placeholder_additional_text": {
|
"placeholder_additional_text": {
|
||||||
"message": "Additional text",
|
"message": "Additional text",
|
||||||
"description": ""
|
"description": ""
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,17 @@ switch (message.command) {
|
||||||
return Promise.resolve(window.getSelection().toString());
|
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": {
|
case "replaceSelectedText": {
|
||||||
const selectedText = window.getSelection().toString();
|
const selectedText = window.getSelection().toString();
|
||||||
let force_insert = false;
|
let force_insert = false;
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,7 @@ export class mzta_Menus {
|
||||||
//const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
//const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||||
return {tabId: tabs[0].id,
|
return {tabId: tabs[0].id,
|
||||||
selection: await browser.tabs.sendMessage(tabs[0].id, { command: "getSelectedText" }),
|
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" }),
|
text: await browser.tabs.sendMessage(tabs[0].id, { command: "getTextOnly" }),
|
||||||
html: await browser.tabs.sendMessage(tabs[0].id, { command: "getFullHtml" }),
|
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 }),
|
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 body_text = '';
|
||||||
let selection_text = '';
|
let selection_text = '';
|
||||||
|
let selection_html = msg_text.selection_html;
|
||||||
let only_typed_text = '';
|
let only_typed_text = '';
|
||||||
let only_quoted_text = '';
|
let only_quoted_text = '';
|
||||||
only_typed_text = msg_text.only_typed_text.replace(/\s+/g, ' ').trim();
|
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();
|
only_quoted_text = msg_text.only_quoted_text.replace(/\s+/g, ' ').trim();
|
||||||
curr_prompt.selection_text = selection_text;
|
curr_prompt.selection_text = selection_text;
|
||||||
|
curr_prompt.selection_html = selection_html;
|
||||||
body_text = msg_text.text.replace(/\s+/g, ' ').trim();
|
body_text = msg_text.text.replace(/\s+/g, ' ').trim();
|
||||||
curr_prompt.body_text = body_text;
|
curr_prompt.body_text = body_text;
|
||||||
//open chatgpt window
|
//open chatgpt window
|
||||||
|
|
@ -141,7 +144,7 @@ export class mzta_Menus {
|
||||||
break;
|
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){
|
switch(curr_prompt.id){
|
||||||
case 'prompt_translate_this':
|
case 'prompt_translate_this':
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,13 @@ const defaultPlaceholders = [
|
||||||
type: 0,
|
type: 0,
|
||||||
is_default: "1",
|
is_default: "1",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'selected_html',
|
||||||
|
name: "__MSG_placeholder_selected_html__",
|
||||||
|
default_value: "",
|
||||||
|
type: 0,
|
||||||
|
is_default: "1",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
id: 'additional_text',
|
id: 'additional_text',
|
||||||
name: "__MSG_placeholder_additional_text__",
|
name: "__MSG_placeholder_additional_text__",
|
||||||
|
|
@ -301,7 +308,7 @@ export const placeholdersUtils = {
|
||||||
return regex.test(text);
|
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);
|
let currPHs = await placeholdersUtils.extractPlaceholders(prompt_text);
|
||||||
// console.log(">>>>>>>>>> currPHs: " + JSON.stringify(currPHs));
|
// console.log(">>>>>>>>>> currPHs: " + JSON.stringify(currPHs));
|
||||||
let finalSubs = {};
|
let finalSubs = {};
|
||||||
|
|
@ -331,6 +338,9 @@ export const placeholdersUtils = {
|
||||||
case 'selected_text':
|
case 'selected_text':
|
||||||
finalSubs['selected_text'] = placeholdersUtils.failSafePlaceholders(selection_text);
|
finalSubs['selected_text'] = placeholdersUtils.failSafePlaceholders(selection_text);
|
||||||
break;
|
break;
|
||||||
|
case 'selected_html':
|
||||||
|
finalSubs['selected_html'] = placeholdersUtils.failSafePlaceholders(selection_html);
|
||||||
|
break;
|
||||||
case 'author':
|
case 'author':
|
||||||
finalSubs['author'] = placeholdersUtils.failSafePlaceholders(curr_message.author);
|
finalSubs['author'] = placeholdersUtils.failSafePlaceholders(curr_message.author);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -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 = '';
|
let fullPrompt = '';
|
||||||
|
|
||||||
if(!placeholdersUtils.hasPlaceholder(curr_prompt.text)){
|
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) + "\" ";
|
fullPrompt = curr_prompt.text + (String(curr_prompt.need_signature) == "1" ? " " + await taPromptUtils.getDefaultSignature():"") + " " + chatgpt_lang + " \"" + (selection_text=='' ? body_text : selection_text) + "\" ";
|
||||||
}else{
|
}else{
|
||||||
// we have at least a placeholder, do the magic!
|
// 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));
|
// console.log(">>>>>>>>>> finalSubs: " + JSON.stringify(finalSubs));
|
||||||
let prefs_ph = await browser.storage.sync.get({placeholders_use_default_value: false});
|
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();
|
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();
|
||||||
|
|
|
||||||
|
|
@ -966,7 +966,7 @@ function checkPromptsConfigForPlaceholders(textarea){
|
||||||
|
|
||||||
let tr_ancestor2 = textarea.closest('tr');
|
let tr_ancestor2 = textarea.closest('tr');
|
||||||
let selected_text_element = tr_ancestor2.querySelector('.need_selected') || tr_ancestor2.querySelector('.need_selected_new');
|
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){
|
if(!selected_text_element.checked){
|
||||||
selected_text_element.closest('.need_selected_span').style.border = '2px solid red';
|
selected_text_element.closest('.need_selected_span').style.border = '2px solid red';
|
||||||
}else{
|
}else{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue