From 5ecc13ad206840cc1edf7f906a7fc30d66d02c09 Mon Sep 17 00:00:00 2001 From: mic Date: Tue, 7 May 2024 22:36:44 +0200 Subject: [PATCH] custom prompts: added base methods. see #12 --- js/mzta-menus.js | 18 ++++++++---- js/mzta-prompts.js | 71 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 8 deletions(-) diff --git a/js/mzta-menus.js b/js/mzta-menus.js index 3ea432f8..a06b02f5 100644 --- a/js/mzta-menus.js +++ b/js/mzta-menus.js @@ -18,11 +18,12 @@ // Some original methods are derived from https://github.com/ali-raheem/Aify/blob/cfadf52f576b7be3720b5b73af7c8d3129c054da/plugin/html/actions.js -import { defaultPrompts } from './mzta-prompts.js'; +import { getPrompts } from './mzta-prompts.js'; import { getLanguageDisplayName } from './mzta-utils.js' export class mzta_Menus { + allPrompts = []; openChatGPT = null; menu_context_compose = 'compose_action_menu'; menu_context_display = 'message_display_action_menu'; @@ -34,7 +35,13 @@ export class mzta_Menus { constructor(openChatGPT) { this.openChatGPT = openChatGPT; - defaultPrompts.forEach((prompt) => { + this.allPrompts = []; + } + + + async initialize() { + this.allPrompts = await getPrompts(); + this.allPrompts.forEach((prompt) => { this.addAction(prompt) }); //this.addMenu(this.rootMenu); @@ -105,7 +112,8 @@ export class mzta_Menus { } } - loadMenus() { + async loadMenus() { + await this.initialize(); this.addMenu(this.rootMenu); let listeners = this.menu_listeners; browser.menus.onClicked.addListener((info, tab) => { @@ -136,7 +144,7 @@ export class mzta_Menus { }; getContexts(id){ - const curr_prompt = defaultPrompts.find(p => p.id === id); + const curr_prompt = this.allPrompts.find(p => p.id === id); if (!curr_prompt) { return []; } @@ -154,7 +162,7 @@ export class mzta_Menus { } getCustomTextAttribute(id){ - const curr_prompt = defaultPrompts.find(p => p.id === id); + const curr_prompt = this.allPrompts.find(p => p.id === id); if (!curr_prompt) { return ""; } diff --git a/js/mzta-prompts.js b/js/mzta-prompts.js index 22b81d8d..818c2910 100644 --- a/js/mzta-prompts.js +++ b/js/mzta-prompts.js @@ -16,9 +16,13 @@ * along with this program. If not, see . */ -// Modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/html/globals.js +// Modified prompts text derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/html/globals.js -/* Types (type attribute): +/* ================= PROMPTS PROPERTIES ======================================== + + ================ BASE PROPERTIES + + Types (type attribute): 0: always show (when composing a part of the text must be selected if need_selected = 1) 1: show when reading an email 2: show when composing a mail (a part of the text must be selected if need_selected = 1) @@ -39,9 +43,18 @@ Custom Text (need_custom_text attribute): 0: No custom text needed 1: Custom text needed + + ================ USER PROPERTIES + Enabled (enabled attribute): + 0: Disabled + 1: Enabled + + Position (position attribute): + : position number + */ -export const defaultPrompts = [ +const defaultPrompts = [ { id: 'prompt_reply', name: "__MSG_prompt_reply__", @@ -113,3 +126,55 @@ export const defaultPrompts = [ need_custom_text: 0, }, ]; + + +export async function getPrompts(){ + //return await Promise.all([getDefaultPrompts_withProps(), getCustomPrompts()]).then(([defaultPrompts, customPrompts]) => [...defaultPrompts, ...customPrompts]); + const defaultPrompts = await getDefaultPrompts_withProps(); + const customPrompts = await getCustomPrompts(); + return defaultPrompts.concat(customPrompts); +} + + +async function getDefaultPrompts_withProps() { + let prefs = await browser.storage.sync.get({_default_prompts_properties: null}); + let defaultPrompts_prop = [...defaultPrompts]; + if(prefs._default_prompts_properties === null){ // no default prompts properties saved + let pos = 1; + defaultPrompts_prop.forEach((prompt) => { + prompt.position = pos; + prompt.enabled = 1; + pos++; + }) + } else { // we have saved the default prompts properties + defaultPrompts_prop.forEach((prompt) => { + prompt.position = prefs._default_prompts_properties[prompt.id].position; + prompt.enabled = prefs._default_prompts_properties[prompt.id].enabled; + }) + } + return defaultPrompts_prop; +} + + +async function getCustomPrompts() { + let prefs = await browser.storage.sync.get({_custom_prompt: null}); + if(prefs._custom_prompt === null){ + return []; + } else { + return prefs._custom_prompt; + } +} + +export async function setDefaultPromptsProperties(prompts) { + let default_prompts_properties = {}; + prompts.forEach((prompt) => { + default_prompts_properties[prompt.id] = {position: prompt.position, enabled: prompt.enabled}; + }); + await browser.storage.sync.set({_default_prompts_properties: default_prompts_properties}); +} + + +export async function setCustomPrompts(prompts) { + await browser.storage.sync.set({_custom_prompt: prompts}); +} +