From 42a869aaaf5eb9def5ed6b0cfc09a8f55b91065f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=9B?= Date: Mon, 27 Jul 2026 14:49:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20popup=20=E7=9B=B4=E6=8E=A5=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=EF=BC=8C=E4=B8=8D=E9=80=9A=E8=BF=87=20background?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-to-ppt/popup.js | 46 ++++++++++++--------------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/web-to-ppt/popup.js b/web-to-ppt/popup.js index 241314a..38868df 100644 --- a/web-to-ppt/popup.js +++ b/web-to-ppt/popup.js @@ -54,63 +54,42 @@ async function exportPptx() { const captureWidth = normalizeCaptureWidth(captureWidthInput.value, null); try { - // 1. 触发采集(background 会缓存数据) + // 1. 采集 setProgress(15, '正在采集页面...'); const captureResult = await chrome.runtime.sendMessage({ type: 'WEB_TO_PPT_CAPTURE_START', options: { captureWidth } }); + if (!captureResult || !captureResult.ok) throw new Error(captureResult?.error || '采集失败'); - if (!captureResult || !captureResult.ok) { - throw new Error(captureResult?.error || '采集失败'); - } - - // 2. 获取缓存数据 + // 2. 获取数据 setProgress(40, '采集完成,获取数据...'); - const dataResult = await chrome.runtime.sendMessage({ - type: 'WEB_TO_PPT_GET_DATA' - }); - - if (!dataResult || !dataResult.ok) { - throw new Error(dataResult?.error || '获取数据失败'); - } - - const jsonData = dataResult.data; + const dataResult = await chrome.runtime.sendMessage({ type: 'WEB_TO_PPT_GET_DATA' }); + if (!dataResult || !dataResult.ok) throw new Error(dataResult?.error || '获取数据失败'); // 3. 转化 setProgress(50, '正在生成 PPTX...'); - const schema = await WebToPPT.convertToPptx(jsonData, (msg) => { - setProgress(60, msg); - }); + const schema = await WebToPPT.convertToPptx(dataResult.data, (msg) => setProgress(60, msg)); - // 4. 生成 PPTX blob + // 4. 生成 PPTX setProgress(85, '正在打包...'); const blob = await WebToPPT.schemaToPptxBlob(schema); - // 5. 转 base64 + // 5. 下载(data URL,popup 直接调用) + 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 base64 = btoa(binary); + const dataUrl = 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,' + btoa(binary); - // 6. 让 background 下载 - setProgress(95, '正在下载...'); - const title = jsonData.source?.title || 'webpage'; + const title = dataResult.data.source?.title || 'webpage'; const safeTitle = title.replace(/[\\/:*?"<>|]+/g, '-').replace(/\s+/g, '-').slice(0, 64) || 'webpage'; const filename = `web-to-ppt/${safeTitle}-${Date.now()}.pptx`; - const dlResult = await chrome.runtime.sendMessage({ - type: 'WEB_TO_PPT_DOWNLOAD', - base64, - filename - }); - - if (!dlResult || !dlResult.ok) { - throw new Error(dlResult?.error || '下载失败'); - } + await chrome.downloads.download({ url: dataUrl, filename }); setProgress(100, '导出完成'); setResult('success', '导出完成'); @@ -126,7 +105,6 @@ async function exportPptx() { } } -// 初始化 document.addEventListener('DOMContentLoaded', async () => { const currentWidth = await getCurrentViewportWidth(); captureWidthInput.value = String(currentWidth || '');