From fc77b286b7c347a482db079465b0dc745e28beaf Mon Sep 17 00:00:00 2001 From: gapato Date: Mon, 8 Jun 2026 19:59:55 +0200 Subject: [PATCH] small refactor --- content.js | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/content.js b/content.js index b3e06f7..6391d9b 100644 --- a/content.js +++ b/content.js @@ -55,7 +55,7 @@ async function downloadApplicantFilesWithProgress(applicantDetails, token, onPro throw `Failed to download file ${afile.file_id} for applicant ${applicantDetails.id}` } - afile.bytes = new Uint8Array(await response.arrayBuffer()) + afile.bytes = await response.bytes() completedDownloads += 1 downloadedBytes += afile.bytes.length onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes }) @@ -120,7 +120,7 @@ async function getApplicants() { return { aid, token, - applicants: jsonData//.slice(0, 5) + applicants: jsonData } // return { aid, token, applicants: jsonData } }) @@ -287,24 +287,46 @@ function getFflate() { } async function createZipBlob(archiveEntries) { - - console.log(archiveEntries) + const { Zip, ZipPassThrough } = getFflate() return new Promise((resolve, reject) => { - fflate.zip(archiveEntries, { mem: 9, level: 0, consume: true }, (error, data) => { + const chunks = [] + const zip = new Zip() + let settled = false + + zip.ondata = (error, chunk, final) => { + if (settled) { + return + } + if (error != null) { + settled = true reject(error) return } - resolve(data) - }) - // resolve(fflate.zipSync(archiveEntries, { level: 0, consume: true })) + chunks.push(chunk) + + if (final) { + settled = true + resolve(new Blob(chunks, { type: "application/zip" })) + } + } + + try { + Object.entries(archiveEntries).forEach(([path, bytes]) => { + const file = new ZipPassThrough(path) + + zip.add(file) + file.push(bytes, true) + }) + + zip.end() + } catch (error) { + settled = true + reject(error) + } }) - .then((archiveBytes) => { - console.log("producing blob") - return new Blob([archiveBytes], { type: "application/zip" }) - }) } function downloadBlob(blob, fileName) { @@ -508,7 +530,7 @@ function rip(event) { archiveFiles.forEach((archiveFile, index) => { const afile = applicantDetails.application_files[index] - archiveEntries[archiveFile.relativePath] = afile.bytes + archiveEntries[archiveFile.relativePath] = new Uint8Array(afile.bytes) }) }) @@ -528,7 +550,7 @@ function rip(event) { console.log("Generating zip archive...") - createZipBlob(archiveEntries) + return createZipBlob(archiveEntries) .then((zipBlob) => { console.log("Zip archive is ready.") progressDialog.setStatus("Download ready.")