Allow selective data export and import
This commit is contained in:
parent
6a85dbb2eb
commit
c7333482ce
3 changed files with 81 additions and 0 deletions
45
options/dataTransfer.js
Normal file
45
options/dataTransfer.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
"use strict";
|
||||
const storage = (globalThis.messenger ?? browser).storage;
|
||||
const KEY_GROUPS = {
|
||||
settings: [
|
||||
'endpoint',
|
||||
'templateName',
|
||||
'customTemplate',
|
||||
'customSystemPrompt',
|
||||
'aiParams',
|
||||
'debugLogging',
|
||||
'htmlToMarkdown',
|
||||
'stripUrlParams',
|
||||
'altTextImages',
|
||||
'collapseWhitespace'
|
||||
],
|
||||
rules: ['aiRules'],
|
||||
cache: ['aiCache']
|
||||
};
|
||||
|
||||
function collectKeys(categories = Object.keys(KEY_GROUPS)) {
|
||||
return categories.flatMap(cat => KEY_GROUPS[cat] || []);
|
||||
}
|
||||
|
||||
export async function exportData(categories) {
|
||||
const data = await storage.local.get(collectKeys(categories));
|
||||
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = 'sortana-export.json';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export async function importData(file, categories) {
|
||||
const text = await file.text();
|
||||
const parsed = JSON.parse(text);
|
||||
const data = {};
|
||||
for (const key of collectKeys(categories)) {
|
||||
if (key in parsed) data[key] = parsed[key];
|
||||
}
|
||||
await storage.local.set(data);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue