04 plugin.js (2/3)

      ${row('Save books to', '<input type="text" data-k="dest_dir" placeholder="/Books/My Server">')}
      <p>
        <button class="btn-small btn-add" data-act="save">Save</button>
        <button class="btn-small" data-act="test">Test connection</button>
      </p>
      <p data-out style="color:var(--muted-color)"></p>
      <p style="color:var(--muted-color);font-size:.9em">
        Stored unencrypted on the SD card in ${api.dir}/${CONFIG}.
      </p>`;
  
    const out = (msg, ok = true) => {
      $('[data-out]').textContent = msg;
      $('[data-out]').style.color = ok ? 'var(--success-color)' : 'var(--danger-color)';
    };
    const inputs = [...container.querySelectorAll('[data-k]')];
  
    // ---------- load ----------
    const cfg = await readConfig();
    for (const el of inputs) el.value = cfg[el.dataset.k] ?? '';
  
    // ---------- save ----------
    $('[data-act=save]').onclick = async () => {
      for (const el of inputs) {
        const v = el.value.trim();
        if (v) cfg[el.dataset.k] = v; else delete cfg[el.dataset.k];
      }
      if (cfg.server) cfg.server = cfg.server.replace(/\/+$/, '');   // no trailing slash
      if (cfg.dest_dir && !cfg.dest_dir.startsWith('/')) cfg.dest_dir = '/' + cfg.dest_dir;
      try {
        await writeConfig(cfg);
        out('Saved.');
      } catch (e) {
        out('Save failed: ' + e.message, false);
      }
    };
  
    // ---------- test through the reader (no CORS!) ----------
‹ PreviousReader file (.txt)Next ›