diff --git a/web-to-ppt/background.js b/web-to-ppt/background.js index 96fec62..fc8e77b 100644 --- a/web-to-ppt/background.js +++ b/web-to-ppt/background.js @@ -259,6 +259,18 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { return true; } + // 下载 PPTX(由 popup 调用,background 执行下载) + if (message?.type === "WEB_TO_PPT_DOWNLOAD") { + (async () => { + const dataUrl = 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,' + message.base64; + await chrome.downloads.download({ url: dataUrl, filename: message.filename }); + return { ok: true }; + })() + .then(sendResponse) + .catch(error => sendResponse({ ok: false, error: error.message || String(error) })); + return true; + } + return false; }); diff --git a/web-to-ppt/popup.js b/web-to-ppt/popup.js index 0a0f24f..008fcd0 100644 --- a/web-to-ppt/popup.js +++ b/web-to-ppt/popup.js @@ -101,25 +101,31 @@ async function exportPptx() { setProgress(50 + Math.min(40, Math.floor(Math.random() * 40)), msg); }); - // 4. 生成 PPTX + // 4. 生成 PPTX 并通过 background 下载 setProgress(90, '正在打包...'); const blob = await WebToPPT.schemaToPptxBlob(schema); - - // 5. 下载(用 data URL,和原插件一致) - setProgress(95, '正在下载...'); const buffer = await blob.arrayBuffer(); const bytes = new Uint8Array(buffer); let binary = ''; for (let i = 0; i < bytes.length; i += 0x8000) { binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000)); } - const dataUrl = 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,' + btoa(binary); + const base64 = btoa(binary); + // 5. 让 background 执行下载 + setProgress(95, '正在下载...'); const title = jsonData.source?.title || 'webpage'; const safeTitle = title.replace(/[\\/:*?"<>|]+/g, '-').replace(/\s+/g, '-').slice(0, 64) || 'webpage'; const filename = `web-to-ppt/${safeTitle}-${Date.now()}.pptx`; - await chrome.downloads.download({ url: dataUrl, filename }); + const dlResult = await chrome.runtime.sendMessage({ + type: 'WEB_TO_PPT_DOWNLOAD', + base64, + filename + }); + if (!dlResult || !dlResult.ok) { + throw new Error(dlResult?.error || '下载失败'); + } setProgress(100, `已导出:${filename}`); setResult('success', '导出完成');