/* Service worker: performs downloads via the extension's own downloads API. * * A content script (page context) may auto-download only ONE file before Chrome * blocks the rest behind a "multiple downloads" guard. Routing each batch * through chrome.downloads here sidesteps that: every part downloads * immediately and appears in Chrome's download tray. * * Service workers cannot call URL.createObjectURL, so the content script makes * the blob URL and sends it here. A data: URL is accepted as a fallback for the * same reason, though large ones are less reliable — hence blob first. */ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { if (!msg || msg.type !== 'download' || !msg.url) return; try { chrome.downloads.download( { url: msg.url, filename: msg.filename, saveAs: false, conflictAction: 'uniquify' }, function (id) { var err = chrome.runtime.lastError; sendResponse({ ok: !err && id != null, error: err && err.message }); } ); } catch (e) { sendResponse({ ok: false, error: String(e) }); } return true; // keep the message channel open for the async sendResponse });