import/export data placeholders added. see #156

This commit is contained in:
Mic 2025-06-15 23:37:00 +02:00
parent f0d2e12c7b
commit f4c1ab1018
2 changed files with 21 additions and 27 deletions

View file

@ -267,7 +267,7 @@ export async function prepareCustomDataPHsForImport(placeholders){
// console.log(">>>>>>>>>>> prepareCustomDataPHsForImport prompts: " + JSON.stringify(prompts)); // console.log(">>>>>>>>>>> prepareCustomDataPHsForImport prompts: " + JSON.stringify(prompts));
const output = await getCustomPlaceholders(); const output = await getCustomPlaceholders();
// console.log(">>>>>>>>>>> prepareCustomDataPHsForImport output: " + JSON.stringify(output)); // console.log(">>>>>>>>>>> prepareCustomDataPHsForImport output: " + JSON.stringify(output));
prompts.forEach(placeholder => { placeholders.forEach(placeholder => {
if(output.some(p => p.id == placeholder.id)){ if(output.some(p => p.id == placeholder.id)){
Object.keys(placeholder).forEach(key => { Object.keys(placeholder).forEach(key => {
output.find(p => p.id == placeholder.id)[key] = placeholder[key]; output.find(p => p.id == placeholder.id)[key] = placeholder[key];

View file

@ -156,14 +156,14 @@ document.addEventListener('DOMContentLoaded', async () => {
const btnExportAll = document.getElementById('btnExportAll'); const btnExportAll = document.getElementById('btnExportAll');
btnExportAll.addEventListener('click', (e) => { btnExportAll.addEventListener('click', (e) => {
e.preventDefault(); e.preventDefault();
exportPrompts(); exportCustomDataPHs();
}); });
async function exportPrompts() { async function exportCustomDataPHs() {
const manifest = browser.runtime.getManifest(); const manifest = browser.runtime.getManifest();
const addonVersion = manifest.version; const addonVersion = manifest.version;
const outputPrompts = preparePromptsForExport(await getPrompts()); const outputCustomDataPHs = prepareCustomDataPHsForExport(await getCustomPlaceholders());
let outputObj = {id: 'thunderai-prompts', addon_version: addonVersion, prompts: outputPrompts}; let outputObj = {id: 'thunderai-custom-data-placeholders', addon_version: addonVersion, customdataplaceholders: outputCustomDataPHs};
const blob = new Blob([JSON.stringify(outputObj, null, 2)], { const blob = new Blob([JSON.stringify(outputObj, null, 2)], {
type: "application/json", type: "application/json",
}); });
@ -171,7 +171,7 @@ document.addEventListener('DOMContentLoaded', async () => {
const time_stamp = `${currentDate.getFullYear()}${String(currentDate.getMonth() + 1).padStart(2, '0')}${String(currentDate.getDate()).padStart(2, '0')}${String(currentDate.getHours()).padStart(2, '0')}${String(currentDate.getMinutes()).padStart(2, '0')}${String(currentDate.getSeconds()).padStart(2, '0')}`; const time_stamp = `${currentDate.getFullYear()}${String(currentDate.getMonth() + 1).padStart(2, '0')}${String(currentDate.getDate()).padStart(2, '0')}${String(currentDate.getHours()).padStart(2, '0')}${String(currentDate.getMinutes()).padStart(2, '0')}${String(currentDate.getSeconds()).padStart(2, '0')}`;
messenger.downloads.download({ messenger.downloads.download({
url: URL.createObjectURL(blob), url: URL.createObjectURL(blob),
filename: `thunderai-prompts-${time_stamp}.json`, filename: `thunderai-custom-data-placeholders-${time_stamp}.json`,
saveAs: true, saveAs: true,
}); });
} }
@ -179,57 +179,51 @@ document.addEventListener('DOMContentLoaded', async () => {
const btnImport = document.getElementById('btnImport'); const btnImport = document.getElementById('btnImport');
btnImport.addEventListener('click', (e) => { btnImport.addEventListener('click', (e) => {
e.preventDefault(); e.preventDefault();
importPrompts(); importCustomDataPHs();
}); });
function importPrompts() { function importCustomDataPHs() {
if(confirm(browser.i18n.getMessage("importPrompts_confirmText") + '\n' + browser.i18n.getMessage("customPrompts_managePrompts_info_default_2") + '\n' + browser.i18n.getMessage("customPrompts_managePrompts_info_default_3"))) { if(confirm(browser.i18n.getMessage("importCustomDataPH_confirmText") + '\n' + browser.i18n.getMessage("customDataPH_manageDataPH_info_default") + '\n' + browser.i18n.getMessage("customPrompts_managePrompts_info_default_3"))) {
//ask the user to choose a JSON file, and then read it, check if the serialized JSON is valid as generated from exportPrompts(), and if so, add it to the list //ask the user to choose a JSON file, and then read it, check if the serialized JSON is valid as generated from importCustomDataPHs(), and if so, add it to the list
const input = document.createElement('input'); const input = document.createElement('input');
input.type = 'file'; input.type = 'file';
input.accept = '.json'; input.accept = '.json';
input.click(); input.click();
input.onchange = async () => { input.onchange = async () => {
setMessage(browser.i18n.getMessage('customPrompts_start_import')); setMessage(browser.i18n.getMessage('importCustomDataPH_start_import'));
const file = input.files[0]; const file = input.files[0];
const reader = new FileReader(); const reader = new FileReader();
reader.onload = async () => { reader.onload = async () => {
const json = reader.result; const json = reader.result;
try { try {
const obj = JSON.parse(json); const obj = JSON.parse(json);
if(obj.id !== 'thunderai-prompts') { if(obj.id !== 'thunderai-custom-data-placeholders') {
alert(browser.i18n.getMessage("importPrompts_invalidFile")); alert(browser.i18n.getMessage("importCustomDataPH_invalidFile"));
setMessage(browser.i18n.getMessage('importPrompts_invalidFile'),'red'); setMessage(browser.i18n.getMessage('importCustomDataPH_invalidFile'),'red');
return; return;
} }
// if(obj.addon_version !== manifest.version) { // if(obj.addon_version !== manifest.version) {
// alert(browser.i18n.getMessage("importPrompts_invalidVersion")); // alert(browser.i18n.getMessage("importPrompts_invalidVersion"));
// return; // return;
// } // }
if(!Array.isArray(obj.prompts)) { if(!Array.isArray(obj.customdataplaceholders)) {
alert(browser.i18n.getMessage("importPrompts_invalidPrompts")); alert(browser.i18n.getMessage("importCustomDataPH_invalidDataPHs"));
setMessage(browser.i18n.getMessage('customPrompts_invalidPrompts'),'red'); setMessage(browser.i18n.getMessage('importCustomDataPH_invalidDataPHs'),'red');
return; return;
} }
//setCustomPrompts(obj.prompts);
customDataPHsList.clear(); customDataPHsList.clear();
loadCustomDataPHsList(await preparePromptsForImport(obj.prompts)); loadCustomDataPHsList(await prepareCustomDataPHsForImport(obj.customdataplaceholders));
setSomethingChanged(); setSomethingChanged();
i18n.updateDocument(); i18n.updateDocument();
// browser.runtime.sendMessage({command: "reload_menus"}); setMessage(browser.i18n.getMessage('importCustomDataPH_import_completed'), 'orange');
setMessage(browser.i18n.getMessage('customPrompts_import_completed'), 'orange');
// msgTimeout = setTimeout(() => {
// clearMessage();
// }, 10000);
} catch(err) { } catch(err) {
alert(browser.i18n.getMessage("importPrompts_invalidFile") + ' ' + err); alert(browser.i18n.getMessage("importCustomDataPH_invalidFile") + ' ' + err);
setMessage(browser.i18n.getMessage('importPrompts_invalidFile'),'red'); setMessage(browser.i18n.getMessage('importCustomDataPH_invalidFile'),'red');
return; return;
} }
}; };
reader.readAsText(file); reader.readAsText(file);
}; };
}; };
} }