04 plugin.js (2/5)
CrossPoint.registerPlugin(async (container, api) => {
const $ = (sel) => container.querySelector(sel);
const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
const b64 = (text) => {
let bin = '';
for (const byte of new TextEncoder().encode(text)) bin += String.fromCharCode(byte);
return btoa(bin);
};
const CONFIG = 'config.json';
async function readConfig() {
try {
const r = await fetch(api.pluginFile(CONFIG)); // 404 before the first save
return r.ok ? await r.json() : {};
} catch (e) {
return {};
}
}
async function writeConfig(cfg) {
const json = JSON.stringify(cfg);
if (json.length > 2000) throw new Error('config.json would exceed 2 KB');
// writeFile rejects empty bodies; "{}" is the smallest valid config.
await api.writeFile(api.dir + '/' + CONFIG, b64(json));
}