ISMIS Helper: cross-browser build, CI, PDF fixes, Chrome download fix #1

Merged
adrianbonpin merged 55 commits from feat/ismis-helper into prod 2026-08-18 19:13:50 +00:00
Showing only changes of commit 40fa9cc9b5 - Show all commits
+16 -4
View File
@@ -10,6 +10,19 @@ function errMsg(err) {
return String((err && err.message) || err); return String((err && err.message) || err);
} }
// Build a download URL from PDF bytes. Chrome MV3 runs the background as a
// service worker, where URL.createObjectURL is NOT available, so we use a
// base64 data URL (works in both Firefox and Chrome). PDFs here are small
// (~150KB), well within data-URL length limits.
function pdfDataUrl(bytes) {
let binary = '';
const chunk = 0x8000;
for (let i = 0; i < bytes.length; i += chunk) {
binary += String.fromCharCode.apply(null, bytes.subarray(i, i + chunk));
}
return 'data:application/pdf;base64,' + btoa(binary);
}
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => { browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg && msg.type === 'generate-pdf') { if (msg && msg.type === 'generate-pdf') {
generatePdf(msg.data) generatePdf(msg.data)
@@ -32,8 +45,9 @@ function formatPeriod(period) {
async function generatePdf(data) { async function generatePdf(data) {
const bytes = await ISMIStools.buildStudyLoadPdf(data); const bytes = await ISMIStools.buildStudyLoadPdf(data);
const blob = new Blob([bytes], { type: 'application/pdf' }); // Chrome MV3 background is a service worker (no URL.createObjectURL), so we
const url = URL.createObjectURL(blob); // use a base64 data URL that works in both browsers.
const url = pdfDataUrl(bytes);
const header = data.header || {}; const header = data.header || {};
const year = parseInt(data.year, 10); const year = parseInt(data.year, 10);
const period = formatPeriod(data.period || header.semester || ''); const period = formatPeriod(data.period || header.semester || '');
@@ -55,12 +69,10 @@ async function generatePdf(data) {
const onChanged = (delta) => { const onChanged = (delta) => {
if (delta.id === id && delta.state && (delta.state.current === 'complete' || delta.state.current === 'interrupted')) { if (delta.id === id && delta.state && (delta.state.current === 'complete' || delta.state.current === 'interrupted')) {
browser.downloads.onChanged.removeListener(onChanged); browser.downloads.onChanged.removeListener(onChanged);
URL.revokeObjectURL(url);
} }
}; };
browser.downloads.onChanged.addListener(onChanged); browser.downloads.onChanged.addListener(onChanged);
} catch (err) { } catch (err) {
URL.revokeObjectURL(url);
throw err; throw err;
} }
} }