06 plugin.js (4/4)
$('[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) };
});
});