fix: use blob URL for Firefox downloads (data: URLs are access-denied)
build-extension / build (pull_request) Successful in 33s
build-extension / build (pull_request) Successful in 33s
Firefox rejects data: URLs in browser.downloads.download with "Access denied for URL data:...", breaking the PDF export. Build the download URL per-browser: a blob: object URL where URL.createObjectURL exists (Firefox background page), falling back to the base64 data: URL for Chrome MV3 service workers (no createObjectURL). Bump version to 1.3.0.
This commit is contained in:
+16
-8
@@ -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 || '');
|
||||
|
||||
Reference in New Issue
Block a user