02 The plugin (2/3)

  CrossPoint.registerPlugin((container, api) => {
    const b64 = (t) => { let s = ''; for (const c of new TextEncoder().encode(t)) s += String.fromCharCode(c); return btoa(s); };
    const INBOX = '/Books/Inbox';
  
    // A visible card on /plugins with a manual form, nice for testing.
    container.innerHTML = `
      <h2>Send to Reader</h2>
      <div class="setting-row"><div class="setting-name">Book URL</div>
        <div class="setting-control"><input type="text" data-url placeholder="https://.../book.epub"></div></div>
      <p><button class="btn-small btn-add">Download</button> <span data-out></span></p>`;
  
    async function save(args) {
      const url = String(args.url || '');
      if (!/^https?:\/\//.test(url)) throw new Error('url must start with http(s)://');
      // Name: explicit, or the last URL segment, made safe for FAT.
      let name = String(args.name || decodeURIComponent(new URL(url).pathname.split('/').pop() || 'book'));
      name = name.replace(/[\\/:*?"<>|]+/g, '_').slice(0, 80);
      if (!/\.(epub|txt|md|xtc|pdf)$/i.test(name)) name += '.epub';
      const dest = `${INBOX}/${name}`;
  
      const res = await api.fetchToSd(url, dest);           // follows redirects, resumes, creates /Books/Inbox
      await api.writeFile(dest + '.meta.json',               // sidecar: remember where it came from
        b64(JSON.stringify({ sendtoreader_source: url.slice(0, 500) })));
‹ PreviousReader file (.txt)Next ›