dynamic placeholder methods added. see #527

This commit is contained in:
mic 2025-12-26 11:37:25 +01:00
parent 1311f56825
commit 7a6927ceec
2 changed files with 62 additions and 5 deletions

View file

@ -619,6 +619,10 @@
"message": "Folder path",
"description": ""
},
"placeholder_mail_headers": {
"message": "Mail headers",
"description": ""
},
"placeholder_selected_text": {
"message": "Selected text",
"description": ""

View file

@ -17,6 +17,7 @@
*/
import { prefs_default } from '../options/mzta-options-default.js';
import { getMailHeader } from './mzta-utils.js';
/* ================= PLACEHOLDERS PROPERTIES ========================================
@ -35,6 +36,10 @@ import { prefs_default } from '../options/mzta-options-default.js';
0: Custom placeholder
1: Default placeholder (not editable, cannot be deleted)
is_dynamic attribute:
0: it's a fixed placeholder
1: it's a dynamic placehoder (it means that it will have a : and then a value, like {%my_placeholder:test_value%})
================ USER PROPERTIES
enabled attribute:
0: Disabled
@ -53,6 +58,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -61,6 +67,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -69,6 +76,7 @@ const defaultPlaceholders = [
default_value: "",
type: 2,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -77,6 +85,7 @@ const defaultPlaceholders = [
default_value: "",
type: 2,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -85,6 +94,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -93,6 +103,7 @@ const defaultPlaceholders = [
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -101,6 +112,16 @@ const defaultPlaceholders = [
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
id: 'mail_headers',
name: "__MSG_placeholder_mail_headers__",
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "1",
enabled: 1,
},
{
@ -109,6 +130,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -117,6 +139,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -125,6 +148,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -133,6 +157,7 @@ const defaultPlaceholders = [
default_value: "0",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -141,6 +166,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -149,6 +175,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -157,6 +184,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -165,6 +193,7 @@ const defaultPlaceholders = [
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -173,6 +202,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -181,6 +211,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -189,6 +220,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -197,6 +229,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -205,6 +238,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -213,6 +247,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -221,6 +256,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -229,6 +265,7 @@ const defaultPlaceholders = [
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
}
];
@ -269,6 +306,7 @@ export async function setCustomPlaceholders(placeholders) {
placeholders.forEach(ph => {
ph.id = placeholdersUtils.validateCustomDataPH_ID(ph.id);
ph.is_default = "0";
ph.is_dynamic = "0";
});
await browser.storage.local.set({_custom_placeholder: placeholders});
}
@ -330,10 +368,21 @@ export const placeholdersUtils = {
// Use exec to find all matches
while ((match = regex.exec(text)) !== null) {
const foundPH = activePHs.find(ph => ph.id === match[1].trim());
if (foundPH) {
matches.push(foundPH);
}
console.log(">>>>>>>>>> extractPlaceholders match: " + JSON.stringify(match));
const foundPH = activePHs.find(ph => ph.id === match[1].trim() || (ph.is_dynamic == 1 && match[1].startsWith(ph.id + ':')));
if (foundPH) {
if (foundPH.is_dynamic == 1 && match[1].includes(':')) {
const [id, custom_value] = match[1].split(':', 2);
const dynamicPH = { ...foundPH }; // Create a copy to avoid modifying the original
dynamicPH.id = id.trim();
dynamicPH.custom_value = custom_value.trim();
matches.push(dynamicPH);
console.log(">>>>>>>>>> extractPlaceholders dynamicPH: " + JSON.stringify(dynamicPH));
} else {
matches.push(foundPH);
console.log(">>>>>>>>>> extractPlaceholders foundPH: " + JSON.stringify(foundPH));
}
}
}
return matches;
@ -424,6 +473,7 @@ export const placeholdersUtils = {
// console.log(">>>>>>>>>> curr_message: " + JSON.stringify(curr_message));
let finalSubs = {};
for(let currPH of currPHs){
console.log(">>>>>>>>>> currPH: " + JSON.stringify(currPH));
switch(currPH.id){
case 'mail_text_body':
finalSubs['mail_text_body'] = placeholdersUtils.failSafePlaceholders(body_text);
@ -446,8 +496,11 @@ export const placeholdersUtils = {
case 'mail_folder_path':
finalSubs['mail_folder_path'] = placeholdersUtils.failSafePlaceholders(curr_message.folder?.path);
break;
case 'mail_headers':
finalSubs['mail_headers'] = placeholdersUtils.failSafePlaceholders(getMailHeader(curr_message, currPH.original_value));
break;
case 'selected_text':
finalSubs['selected_text'] = placeholdersUtils.failSafePlaceholders(selection_text);
finalSubs['selected_text:'+currPH.original_value] = placeholdersUtils.failSafePlaceholders(selection_text);
break;
case 'selected_html':
finalSubs['selected_html'] = placeholdersUtils.failSafePlaceholders(selection_html);