From ace00a0f14b072eb8baf9db9cb677c5e049dc34f Mon Sep 17 00:00:00 2001 From: Gaspard Jankowiak Date: Fri, 5 Jun 2026 08:53:20 +0200 Subject: [PATCH] refactor parallel? --- content.js | 69 +++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/content.js b/content.js index 6f4bb2f..aa451f6 100644 --- a/content.js +++ b/content.js @@ -19,11 +19,7 @@ async function getXSRFToken() { return decodeURIComponent(tokenCookie.split("=").slice(1).join("=")) } -async function getApplicant(applicant, token) { - return getApplicantDetailsWithProgress(applicant, token) -} - -async function getApplicantDetailsWithProgress(applicant, token, onProgress = () => { }) { +async function fetchApplicantDetails(applicant, token) { return fetch(`https://personal.uni-graz.at/api/erec/job-applications/${applicant.id}`, { credentials: "same-origin", @@ -35,34 +31,40 @@ async function getApplicantDetailsWithProgress(applicant, token, onProgress = () } throw `Failed to get applicant ${applicant.id}` }) - .then(async (jsonData) => { - const files = jsonData.application_files ?? [] - let completedDownloads = 0 - let downloadedBytes = 0 +} - onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes }) +async function downloadApplicantFilesWithProgress(applicantDetails, token, onProgress = () => { }) { + const files = applicantDetails.application_files ?? [] + let completedDownloads = 0 + let downloadedBytes = 0 - await Promise.all(files.map(async (afile) => { - const response = await fetch( - `https://personal.uni-graz.at/api/erec/download-file/${afile.file_id}`, - { - credentials: "same-origin", - headers: { "X-XSRF-TOKEN": token } - } - ) + onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes }) - if (!response.ok) { - throw `Failed to download file ${afile.file_id} for applicant ${applicant.id}` - } + await Promise.all(files.map(async (afile) => { + const response = await fetch( + `https://personal.uni-graz.at/api/erec/download-file/${afile.file_id}`, + { + credentials: "same-origin", + headers: { "X-XSRF-TOKEN": token } + } + ) - afile.blob = await response.blob() - completedDownloads += 1 - downloadedBytes += afile.blob.size - onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes }) - })) + if (!response.ok) { + throw `Failed to download file ${afile.file_id} for applicant ${applicantDetails.id}` + } - return jsonData - }) + afile.blob = await response.blob() + completedDownloads += 1 + downloadedBytes += afile.blob.size + onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes }) + })) + + return applicantDetails +} + +async function getApplicant(applicant, token, onProgress = () => { }) { + const applicantDetails = await fetchApplicantDetails(applicant, token) + return downloadApplicantFilesWithProgress(applicantDetails, token, onProgress) } async function getApplicants() { @@ -137,8 +139,8 @@ function formatMegabytes(totalBytes) { function createProgressDialog() { const dialog = document.createElement("dialog") - const title = document.createElement("h2") - const status = document.createElement("p") + const title = document.createElement("h4") + const status = document.createElement("h2") const list = document.createElement("ul") const summary = document.createElement("div") const elapsed = document.createElement("p") @@ -153,9 +155,9 @@ function createProgressDialog() { let timerId = null dialog.classList.add("ripper-progress-dialog") - title.textContent = "Download progress" + title.textContent = "EPAS Ripper" status.classList.add("ripper-progress-status") - status.textContent = "Retrieving applicants list..." + status.textContent = "Retrieving applicants list (will take some time)..." list.classList.add("ripper-progress-list") summary.classList.add("ripper-progress-summary") elapsed.classList.add("ripper-progress-elapsed") @@ -281,7 +283,7 @@ function rip(event) { const applicantDetailsResults = await Promise.allSettled(applicants.map(async (applicant) => { try { - const applicantDetails = await getApplicantDetailsWithProgress(applicant, token, ({ downloaded, total, bytes }) => { + const applicantDetails = await getApplicant(applicant, token, ({ downloaded, total, bytes }) => { progressDialog.updateApplicant(applicant, downloaded, total, bytes) }) return applicantDetails @@ -300,7 +302,6 @@ function rip(event) { } const applicantDetails = result.value - // const dirName = `${applicantDetails.last_name}_${applicantDetails.first_name}` const dirName = `${sanitizeZipPathSegment(applicantDetails.last_name)}_${sanitizeZipPathSegment(applicantDetails.first_name)}` const applicantDir = zip.folder(dirName) const files = applicantDetails.application_files ?? []