From 2fa4064c7cef28310702ae113889b69612454602 Mon Sep 17 00:00:00 2001 From: adrianbonpin Date: Wed, 19 Aug 2026 00:57:27 +0800 Subject: [PATCH] feat: format download filename as ID - Program - S.Y. YYYY-YYYY - Semester - Study Load --- background.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/background.js b/background.js index 0116108..d1a28c3 100644 --- a/background.js +++ b/background.js @@ -13,14 +13,39 @@ browser.runtime.onMessage.addListener((msg, sender, sendResponse) => { } }); +function formatPeriod(period) { + if (!period) return ''; + return String(period) + .replace(/\b1ST\b/g, '1st') + .replace(/\b2ND\b/g, '2nd') + .replace(/\b3RD\b/g, '3rd') + .replace(/\b(\d+)TH\b/g, '$1th') + .toLowerCase() + .replace(/^./, (c) => c.toUpperCase()); +} + async function generatePdf(data) { const bytes = await ISMIStools.buildStudyLoadPdf(data); const blob = new Blob([bytes], { type: 'application/pdf' }); const url = URL.createObjectURL(blob); - const name = (data.header && data.header.fullName) || 'Student'; - const filename = 'Study Load - ' + name + '.pdf'; + const header = data.header || {}; + const year = parseInt(data.year, 10); + const period = formatPeriod(data.period || header.semester || ''); + const compactProgram = ISMIStools.compactProgram(header.program || ''); + const schoolYear = Number.isFinite(year) ? `${year}-${year + 1}` : ''; + const parts = [ + header.studentId || (header.fullName ? header.fullName.replace(/\s+/g, ' ') : ''), + compactProgram, + schoolYear ? 'S.Y. ' + schoolYear : '', + period, + 'Study Load' + ].filter(Boolean); + const filename = parts.join(' - ') + '.pdf'; + const fallback = 'Study Load - ' + (header.fullName || 'Student') + '.pdf'; + const safeFilename = filename || fallback; + try { - const id = await browser.downloads.download({ url, filename, saveAs: true }); + const id = await browser.downloads.download({ url, filename: safeFilename, saveAs: true }); const onChanged = (delta) => { if (delta.id === id && delta.state && (delta.state.current === 'complete' || delta.state.current === 'interrupted')) { browser.downloads.onChanged.removeListener(onChanged);