From aeb75c746cd35146872ba9b532f2b60142ea714b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=9B?= Date: Mon, 27 Jul 2026 14:45:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20popup=20=E6=B5=81=E7=A8=8B=E7=AE=80?= =?UTF-8?q?=E5=8C=96=20=E2=80=94=20=E9=87=87=E9=9B=86=E2=86=92=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E2=86=92=E8=BD=AC=E5=8C=96=E2=86=92background?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-to-ppt/popup.js | 56 ++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/web-to-ppt/popup.js b/web-to-ppt/popup.js index 008fcd0..241314a 100644 --- a/web-to-ppt/popup.js +++ b/web-to-ppt/popup.js @@ -1,4 +1,4 @@ -const SETTINGS_KEY = 'webToPPTSettings'; +const SETTINGS_KEY = 'webToPixsoSettings'; const DEFAULT_SETTINGS = { useProxy: false, concurrency: '8', captureMode: 'mixed', captureWidth: null }; const MIN_CAPTURE_WIDTH = 320; const MAX_CAPTURE_WIDTH = 3840; @@ -16,19 +16,6 @@ function normalizeCaptureWidth(value, fallback = null) { return Math.max(MIN_CAPTURE_WIDTH, Math.min(MAX_CAPTURE_WIDTH, number)); } -function normalizeSettings(value = {}) { - return { - useProxy: Boolean(value.useProxy), - captureMode: 'mixed', - captureWidth: normalizeCaptureWidth(value.captureWidth, null) - }; -} - -async function getSettings() { - const result = await chrome.storage.local.get({ [SETTINGS_KEY]: DEFAULT_SETTINGS }); - return normalizeSettings(result[SETTINGS_KEY]); -} - async function getCurrentViewportWidth() { try { const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); @@ -65,45 +52,42 @@ async function exportPptx() { setProgress(5, '准备采集...'); const captureWidth = normalizeCaptureWidth(captureWidthInput.value, null); - const settings = { ...DEFAULT_SETTINGS, captureWidth }; try { - // 1. 抓取 DOM + // 1. 触发采集(background 会缓存数据) setProgress(15, '正在采集页面...'); - const response = await chrome.runtime.sendMessage({ + const captureResult = await chrome.runtime.sendMessage({ type: 'WEB_TO_PPT_CAPTURE_START', - options: settings + options: { captureWidth } }); - if (!response || !response.ok) { - throw new Error(response?.error || '采集失败'); + if (!captureResult || !captureResult.ok) { + throw new Error(captureResult?.error || '采集失败'); } - // 2. 获取 JSON 数据(从 background 返回的文件路径读取不了,直接从 background 拿数据) - // 需要让 background 返回 JSON 数据而不是文件 - setProgress(40, '采集完成,正在转化...'); - - // background 已经下载了 JSON 文件,但我们需要数据来转化 - // 用另一个消息获取数据 - const dataResponse = await chrome.runtime.sendMessage({ + // 2. 获取缓存数据 + setProgress(40, '采集完成,获取数据...'); + const dataResult = await chrome.runtime.sendMessage({ type: 'WEB_TO_PPT_GET_DATA' }); - if (!dataResponse || !dataResponse.ok) { - throw new Error(dataResponse?.error || '获取数据失败'); + if (!dataResult || !dataResult.ok) { + throw new Error(dataResult?.error || '获取数据失败'); } - const jsonData = dataResponse.data; + const jsonData = dataResult.data; // 3. 转化 setProgress(50, '正在生成 PPTX...'); const schema = await WebToPPT.convertToPptx(jsonData, (msg) => { - setProgress(50 + Math.min(40, Math.floor(Math.random() * 40)), msg); + setProgress(60, msg); }); - // 4. 生成 PPTX 并通过 background 下载 - setProgress(90, '正在打包...'); + // 4. 生成 PPTX blob + setProgress(85, '正在打包...'); const blob = await WebToPPT.schemaToPptxBlob(schema); + + // 5. 转 base64 const buffer = await blob.arrayBuffer(); const bytes = new Uint8Array(buffer); let binary = ''; @@ -112,7 +96,7 @@ async function exportPptx() { } const base64 = btoa(binary); - // 5. 让 background 执行下载 + // 6. 让 background 下载 setProgress(95, '正在下载...'); const title = jsonData.source?.title || 'webpage'; const safeTitle = title.replace(/[\\/:*?"<>|]+/g, '-').replace(/\s+/g, '-').slice(0, 64) || 'webpage'; @@ -123,13 +107,13 @@ async function exportPptx() { base64, filename }); + if (!dlResult || !dlResult.ok) { throw new Error(dlResult?.error || '下载失败'); } - setProgress(100, `已导出:${filename}`); + setProgress(100, '导出完成'); setResult('success', '导出完成'); - // 不自动关闭 popup,等用户选完保存位置 } catch (error) { setProgress(0, error.message || String(error));