From 89fd587c21ab3be5560e16384761b0ddb7c7a21c Mon Sep 17 00:00:00 2001 From: Adrian Bonpin Date: Tue, 18 Aug 2026 20:12:20 +0000 Subject: [PATCH] fix: use blob URL for Firefox downloads (data: URLs access-denied) (#3) --- background.js | 24 ++++++++++++++++-------- manifest.json | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/background.js b/background.js index 826af83..0b5663f 100644 --- a/background.js +++ b/background.js @@ -10,11 +10,19 @@ function errMsg(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) { +// Build a download URL from PDF bytes. Firefox's background is a real page +// (URL.createObjectURL available) and supports blob: URLs in downloads. +// Chrome MV3 runs the background as a service worker where URL.createObjectURL +// is NOT available, but its downloads API accepts base64 data: URLs. +// Firefox rejects data: URLs in browser.downloads.download with +// "Access denied for URL data:...", so pick per-browser. +function buildDownloadUrl(bytes) { + if (typeof URL !== 'undefined' && typeof URL.createObjectURL === 'function') { + const blob = new Blob([bytes], { type: 'application/pdf' }); + return URL.createObjectURL(blob); + } + // Fallback: Chrome MV3 service worker. PDFs here are small (~150KB), well + // within data-URL length limits. let binary = ''; const chunk = 0x8000; for (let i = 0; i < bytes.length; i += chunk) { @@ -45,9 +53,9 @@ function formatPeriod(period) { async function generatePdf(data) { const bytes = await ISMIStools.buildStudyLoadPdf(data); - // Chrome MV3 background is a service worker (no URL.createObjectURL), so we - // use a base64 data URL that works in both browsers. - const url = pdfDataUrl(bytes); + // Firefox accepts blob: object URLs; Chrome MV3 service worker has no + // URL.createObjectURL, so it falls back to a base64 data: URL there. + const url = buildDownloadUrl(bytes); const header = data.header || {}; const year = parseInt(data.year, 10); const period = formatPeriod(data.period || header.semester || ''); diff --git a/manifest.json b/manifest.json index 919579a..e355e21 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "ISMIS Helper", - "version": "1.2.0", + "version": "1.3.0", "description": "Auto-login, auto-accept privacy policy, and Study Load PDF for the USC ISMIS portal.", "permissions": ["storage", "downloads"], "host_permissions": ["https://ismis.usc.edu.ph/*"],