fix(chrome): replace URL.createObjectURL with base64 data URL for downloads (Chrome MV3 service worker has no createObjectURL)
build-extension / build (push) Failing after 15s

This commit is contained in:
2026-08-19 02:57:20 +08:00
parent cd418f513d
commit 40fa9cc9b5
+16 -4
View File
@@ -10,6 +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) {
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) => {
if (msg && msg.type === 'generate-pdf') {
generatePdf(msg.data)
@@ -32,8 +45,9 @@ function formatPeriod(period) {
async function generatePdf(data) {
const bytes = await ISMIStools.buildStudyLoadPdf(data);
const blob = new Blob([bytes], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
// 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);
const header = data.header || {};
const year = parseInt(data.year, 10);
const period = formatPeriod(data.period || header.semester || '');
@@ -55,12 +69,10 @@ async function generatePdf(data) {
const onChanged = (delta) => {
if (delta.id === id && delta.state && (delta.state.current === 'complete' || delta.state.current === 'interrupted')) {
browser.downloads.onChanged.removeListener(onChanged);
URL.revokeObjectURL(url);
}
};
browser.downloads.onChanged.addListener(onChanged);
} catch (err) {
URL.revokeObjectURL(url);
throw err;
}
}