From 3f7c9648d2e8ecf67bf80999cca1ebe6fe5c16a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=9B?= Date: Mon, 27 Jul 2026 14:43:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=8B=E8=BD=BD=E7=A7=BB=E5=88=B0=20b?= =?UTF-8?q?ackground.js=20=E6=89=A7=E8=A1=8C=EF=BC=8C=E5=92=8C=E5=8E=9F?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-to-ppt/background.js | 12 ++++++++++++ web-to-ppt/popup.js | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) 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', '导出完成');