266 lines
No EOL
11 KiB
JavaScript
266 lines
No EOL
11 KiB
JavaScript
/*
|
|
* ThunderAI [https://micz.it/thunderbird-addon-thunderai/]
|
|
* Copyright (C) 2024 - 2026 Mic (m@micz.it)
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import {
|
|
prefs_default,
|
|
integration_options_config
|
|
} from '../../options/mzta-options-default.js';
|
|
import { taLogger } from '../../js/mzta-logger.js';
|
|
import {
|
|
getSpecialPrompts,
|
|
setSpecialPrompts
|
|
} from "../../js/mzta-prompts.js";
|
|
import {
|
|
getPlaceholders,
|
|
mapPlaceholderToSuggestion
|
|
} from "../../js/mzta-placeholders.js";
|
|
import { textareaAutocomplete } from "../../js/mzta-placeholders-autocomplete.js";
|
|
import {
|
|
isAPIKeyValue,
|
|
setTomSelectBorder
|
|
} from "../../js/mzta-utils.js";
|
|
import {
|
|
initializeSpecificIntegrationUI
|
|
} from "../_lib/connection-ui.js";
|
|
|
|
let autocompleteSuggestions = [];
|
|
let taLog = new taLogger("mzta-get-calendar-event-page",true);
|
|
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
|
|
let specialPrompts = await getSpecialPrompts();
|
|
let get_calendar_event_prompt = specialPrompts.find(prompt => prompt.id === 'prompt_get_calendar_event');
|
|
|
|
if (get_calendar_event_prompt && get_calendar_event_prompt.api_type && get_calendar_event_prompt.api_type !== '') {
|
|
let update_prefs = {};
|
|
update_prefs['get_calendar_event_connection_type'] = get_calendar_event_prompt.api_type;
|
|
|
|
let integration = get_calendar_event_prompt.api_type.replace('_api', '');
|
|
if (integration_options_config && integration_options_config[integration]) {
|
|
for (const key of Object.keys(integration_options_config[integration])) {
|
|
if (get_calendar_event_prompt[key] !== undefined) {
|
|
update_prefs[`get_calendar_event_${integration}_${key}`] = get_calendar_event_prompt[key];
|
|
}
|
|
}
|
|
}
|
|
await browser.storage.sync.set(update_prefs);
|
|
}
|
|
|
|
await initializeSpecificIntegrationUI({
|
|
prefix: 'get_calendar_event',
|
|
promptId: 'prompt_get_calendar_event',
|
|
taLog: taLog,
|
|
restoreOptionsCallback: restoreOptions
|
|
});
|
|
|
|
i18n.updateDocument();
|
|
|
|
document.querySelectorAll(".option-input").forEach(element => {
|
|
element.addEventListener("change", saveOptions);
|
|
});
|
|
|
|
let get_calendar_event_textarea = document.getElementById('get_calendar_event_prompt_text');
|
|
let get_calendar_event_save_btn = document.getElementById('btn_save_prompt');
|
|
let get_calendar_event_reset_btn = document.getElementById('btn_reset_prompt');
|
|
let get_calendar_event_use_specific_integration = document.getElementById('get_calendar_event_use_specific_integration');
|
|
let get_calendar_event_no_selection = document.getElementById('calendar_no_selection');
|
|
let get_calendar_event_from_clipboard = document.getElementById('get_calendar_event_from_clipboard');
|
|
|
|
get_calendar_event_textarea.addEventListener('input', (event) => {
|
|
get_calendar_event_reset_btn.disabled = (event.target.value === browser.i18n.getMessage('prompt_get_calendar_event_full_text'));
|
|
get_calendar_event_save_btn.disabled = (event.target.value === get_calendar_event_prompt.text);
|
|
if(get_calendar_event_save_btn.disabled){
|
|
document.getElementById('get_calendar_event_prompt_unsaved').classList.add('hidden');
|
|
} else {
|
|
document.getElementById('get_calendar_event_prompt_unsaved').classList.remove('hidden');
|
|
}
|
|
});
|
|
|
|
get_calendar_event_use_specific_integration.addEventListener('change', (event) => {
|
|
if (!event.target.checked) {
|
|
browser.storage.sync.set({ get_calendar_event_connection_type: '' });
|
|
}
|
|
});
|
|
|
|
get_calendar_event_no_selection.addEventListener('change', async (event) => {
|
|
specialPrompts.find(prompt => prompt.id === 'prompt_get_calendar_event').need_selected = event.target.checked ? '0' : '1';
|
|
await setSpecialPrompts(specialPrompts);
|
|
browser.runtime.sendMessage({command: "reload_menus"});
|
|
});
|
|
|
|
get_calendar_event_from_clipboard.addEventListener('change', async (event) => {
|
|
if (event.target.checked) {
|
|
// Request clipboardRead permission when enabling the feature
|
|
try {
|
|
const granted = await browser.permissions.request({
|
|
permissions: ["clipboardRead"]
|
|
});
|
|
|
|
if (granted) {
|
|
// Permission granted, enable the feature
|
|
await browser.storage.sync.set({ get_calendar_event_from_clipboard: true });
|
|
browser.runtime.sendMessage({command: "reload_menus"});
|
|
} else {
|
|
// Permission denied, uncheck the checkbox
|
|
event.target.checked = false;
|
|
alert(browser.i18n.getMessage("clipboard_permission_denied"));
|
|
}
|
|
} catch (err) {
|
|
taLog.error("Error requesting clipboard permission:", err);
|
|
event.target.checked = false;
|
|
alert(browser.i18n.getMessage("clipboard_permission_error"));
|
|
}
|
|
} else {
|
|
// Disabling the feature
|
|
await browser.storage.sync.set({ get_calendar_event_from_clipboard: false });
|
|
browser.runtime.sendMessage({command: "reload_menus"});
|
|
}
|
|
});
|
|
|
|
get_calendar_event_reset_btn.addEventListener('click', () => {
|
|
get_calendar_event_textarea.value = browser.i18n.getMessage('prompt_get_calendar_event_full_text');
|
|
get_calendar_event_reset_btn.disabled = true;
|
|
let event = new Event('input', { bubbles: true, cancelable: true });
|
|
get_calendar_event_textarea.dispatchEvent(event);
|
|
});
|
|
|
|
get_calendar_event_save_btn.addEventListener('click', async () => {
|
|
specialPrompts.find(prompt => prompt.id === 'prompt_get_calendar_event').text = get_calendar_event_textarea.value;
|
|
specialPrompts.find(prompt => prompt.id === 'prompt_get_calendar_event_from_clipboard').text = get_calendar_event_textarea.value;
|
|
await setSpecialPrompts(specialPrompts);
|
|
get_calendar_event_save_btn.disabled = true;
|
|
document.getElementById('get_calendar_event_prompt_unsaved').classList.add('hidden');
|
|
browser.runtime.sendMessage({command: "reload_menus"});
|
|
});
|
|
|
|
if(get_calendar_event_prompt.text === 'prompt_get_calendar_event_full_text'){
|
|
get_calendar_event_prompt.text = browser.i18n.getMessage(get_calendar_event_prompt.text);
|
|
}
|
|
get_calendar_event_textarea.value = get_calendar_event_prompt.text;
|
|
get_calendar_event_reset_btn.disabled = (get_calendar_event_textarea.value === browser.i18n.getMessage('prompt_get_calendar_event_full_text'));
|
|
|
|
autocompleteSuggestions = (await getPlaceholders(true)).filter(p => !(p.id === 'additional_text')).map(mapPlaceholderToSuggestion);
|
|
textareaAutocomplete(get_calendar_event_textarea, autocompleteSuggestions, 1); // type_value = 1, only when reading an email
|
|
|
|
});
|
|
|
|
|
|
|
|
// Methods to manage options, derived from: /options/mzta-options.js
|
|
|
|
function saveOptions(e) {
|
|
e.preventDefault();
|
|
let options = {};
|
|
let element = e.target;
|
|
|
|
switch (element.type) {
|
|
case 'checkbox':
|
|
options[element.id] = element.checked;
|
|
break;
|
|
case 'number':
|
|
options[element.id] = element.valueAsNumber;
|
|
break;
|
|
case 'text':
|
|
case 'password':
|
|
options[element.id] = element.value.trim();
|
|
break;
|
|
case 'select-one':
|
|
options[element.id] = element.value;
|
|
break;
|
|
default:
|
|
console.error("[ThunderAI] Unhandled input type:", element.type);
|
|
}
|
|
|
|
browser.storage.sync.set(options);
|
|
}
|
|
|
|
async function restoreOptions() {
|
|
function setCurrentChoice(result) {
|
|
document.querySelectorAll(".option-input").forEach(element => {
|
|
if(!element.id) return;
|
|
taLog.log("Options restoring " + element.id + " = " + (isAPIKeyValue(element.id) ? "****************" : result[element.id]));
|
|
switch (element.type) {
|
|
case 'checkbox':
|
|
element.checked = result[element.id] || false;
|
|
break;
|
|
case 'number':
|
|
let default_number_value = 0;
|
|
if(element.id == 'chatgpt_win_height') default_number_value = prefs_default.chatgpt_win_height;
|
|
if(element.id == 'chatgpt_win_width') default_number_value = prefs_default.chatgpt_win_width;
|
|
element.value = result[element.id] ?? default_number_value;
|
|
break;
|
|
case 'text':
|
|
case 'password':
|
|
let default_text_value = '';
|
|
if(element.id == 'default_chatgpt_lang') default_text_value = prefs_default.default_chatgpt_lang;
|
|
element.value = result[element.id] || default_text_value;
|
|
break;
|
|
case 'textarea':
|
|
break;
|
|
default:
|
|
if (element.tagName === 'SELECT') {
|
|
let default_select_value = '';
|
|
const restoreValue = result[element.id] || default_select_value;
|
|
// Check if option exists
|
|
let optionExists = Array.from(element.options).some(opt => opt.value === restoreValue);
|
|
// If it doesn't exist and restoreValue is not empty, create it
|
|
if (!optionExists && restoreValue !== '') {
|
|
let newOption = new Option(restoreValue, restoreValue);
|
|
element.add(newOption);
|
|
}
|
|
// Set value
|
|
element.value = restoreValue;
|
|
if (element.value === '') {
|
|
element.selectedIndex = -1;
|
|
}
|
|
if (element.tomselect) {
|
|
element.tomselect.setValue(element.value, true);
|
|
setTomSelectBorder(element.tomselect);
|
|
}
|
|
}else{
|
|
console.error("[ThunderAI] Unhandled input type:", element.type);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
let getting = await browser.storage.sync.get(prefs_default);
|
|
|
|
let specialPrompts = await getSpecialPrompts();
|
|
let get_calendar_event_prompt = specialPrompts.find(prompt => prompt.id === 'prompt_get_calendar_event');
|
|
|
|
if (get_calendar_event_prompt) {
|
|
if (get_calendar_event_prompt.api_type && get_calendar_event_prompt.api_type !== '') {
|
|
getting['get_calendar_event_connection_type'] = get_calendar_event_prompt.api_type;
|
|
} else {
|
|
getting['get_calendar_event_connection_type'] = getting['connection_type'];
|
|
}
|
|
for (const [integration, options] of Object.entries(integration_options_config)) {
|
|
for (const key of Object.keys(options)) {
|
|
const propName = `${integration}_${key}`;
|
|
if (get_calendar_event_prompt[propName] !== undefined && get_calendar_event_prompt[propName] !== '') {
|
|
getting[`get_calendar_event_${propName}`] = get_calendar_event_prompt[propName];
|
|
} else {
|
|
getting[`get_calendar_event_${propName}`] = getting[propName];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
setCurrentChoice(getting);
|
|
} |