03 plugin.js (1/3)

  CrossPoint.registerPlugin(async (container, api) => {
    // ---------- helpers ----------
    const $ = (s) => container.querySelector(s);
    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) {
      // config.json must stay FLAT (strings/whole numbers) and under 2 KB,
      // because the device reads it as {cfg.KEY} values.
      const json = JSON.stringify(cfg);
      if (json.length > 2000) throw new Error('config too large');
      await api.writeFile(api.dir + '/' + CONFIG, b64(json));
    }
  
    // ---------- UI ----------
    const row = (label, input) => `
      <div class="setting-row">
        <div class="setting-name">${label}</div>
        <div class="setting-control">${input}</div>
      </div>`;
  
    container.innerHTML = `
      <h2>My Server</h2>
      ${row('Server URL', '<input type="text" data-k="server" placeholder="https://books.example.com">')}
      ${row('Username',   '<input type="text" data-k="username" autocomplete="off">')}
      ${row('Password',   '<input type="password" data-k="password" autocomplete="new-password">')}
‹ PreviousReader file (.txt)Next ›