07 plugin.js (5/5)

  $('[data-act=scan]').onclick = async () => {
    $('[data-act=scan]').disabled = true;
    try {
      const books = await scan($('[data-root]').value.trim() || '/');
      report(books);
      $('[data-status]').textContent = 'Done.';
    } catch (e) {
      $('[data-status]').textContent = 'Error: ' + e.message;
    } finally {
      $('[data-act=scan]').disabled = false;
    }
  };

  $('[data-act=move]').onclick = async () => {
    if (!confirm(`Move ${duplicates.length} files to /Duplicates?`)) return;
    await fetch('/mkdir', { method: 'POST', body: form({ name: 'Duplicates', path: '/' }) });   // fails harmlessly if it exists
    let moved = 0;
    for (const path of duplicates) {
      const r = await fetch('/move', { method: 'POST', body: form({ path, dest: '/Duplicates' }) });
      if (r.ok) moved++;
      $('[data-status]').textContent = `Moved ${moved}/${duplicates.length}`;
    }
    $('[data-status]').textContent += '. Rescan to refresh.';
    $('[data-act=move]').disabled = true;
  };

  // Also usable from scripts (chapter 6): returns only counts, results must stay < 192 bytes.
  api.registerAction('stats', async (args) => {
    const books = await scan(args.root || '/');
    return { books: books.length, bytes: books.reduce((s, b) => s + b.size, 0) };
  });
});
‹ PreviousReader file (.txt)