import { uploadBulk } from "./upload.ts"; import { useStatusBar } from "../scrapbox-userscript-std/dom.ts"; import { upload } from "../scrapbox-file-uploader/mod.ts"; await (async () => { const URLs = [] as string[]; const { render, dispose } = useStatusBar(); const errorBar = useStatusBar(); const infoBar = useStatusBar(); try { const fileList = await upload({ accept: "image/jpeg, image/png", multiple: true }); if (!fileList) return; const compare = new Intl.Collator().compare; const files = Array.from(fileList) .sort((a, b) => compare(a.name, b.name)); let counter = 0; const errors = [] as number[]; for await (const result of uploadBulk( files, { title: "タイトル", noIndex: true, }, )) { counter++; if (!result.success) { if (!(result.reason instanceof Error)) throw result.reason; console.error(result.reason); errors.push(counter); errorBar.render( { type: "exclamation-triangle" }, { type: "text", text: `${result.reason.name} ${result.reason.message}`, }, ); continue; } else { const { permalink_url, name, index } = result.value; URLs[index] = permalink_url; infoBar.render( { type: "text", text: `${name} ${permalink_url}` }, ); } render( { type: "spinner" }, { type: "text", text: `${files.length} images, ${ counter - errors.length } uploaded, ${errors.length} failed`, }, ); } render( { type: "check-circle" }, { type: "text", text: `Finish uploading.` }, ); console.log(URLs); console.log(errors); const blob = new Blob([JSON.stringify(URLs)], { type: "application/json" }); window.open(URL.createObjectURL(blob)); } finally { setTimeout(() => { errorBar.dispose(); infoBar.dispose(); dispose(); }, 1000); } })();