From bf3b4548b0ecab9d2c1fce34f1b2065d0cb38512 Mon Sep 17 00:00:00 2001 From: ddshi <8811906+ddshi@user.noreply.gitee.com> Date: Fri, 23 Jan 2026 16:55:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E9=80=9A=E8=BF=87background=20scrip?= =?UTF-8?q?t=E7=9B=B4=E6=8E=A5=E4=B8=8B=E8=BD=BD=E5=88=B0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 下载改由background script处理,使用chrome.downloads.download API - 添加chrome.runtime.sendMessage类型定义 - 修复SVG图标路径错误 - 使用Data URL方式确保下载可靠性 Co-Authored-By: Claude --- src/background/index.ts | 29 +++++++++++++---------------- src/popup/Popup.tsx | 15 +++++++-------- src/types/chrome.d.ts | 1 + 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/background/index.ts b/src/background/index.ts index bdc3688..c9d9f3c 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,34 +1,31 @@ // Background Service Worker // 处理下载等后台任务 -import type { ExtractedContent } from '../types'; -import { generateMarkdown } from '../utils/markdown'; -import { downloadMarkdown, generateFilename } from '../utils/download'; - // 监听来自popup的消息 chrome.runtime.onMessage.addListener((message: any, _sender: any, sendResponse: (response?: any) => void) => { if (message.action === 'download') { - handleDownload(message.data) + handleDownload(message.filename, message.content) .then(() => sendResponse({ success: true })) .catch(error => sendResponse({ success: false, error: error?.message || 'Download failed' })); return true; // 异步响应 } - if (message.action === 'copy') { - sendResponse({ success: false, error: 'Copy should be handled in popup' }); - return false; - } - return false; }); -// 处理下载 -async function handleDownload(data: { content: ExtractedContent; options: { includeTitle: boolean; includeUrl: boolean } }): Promise { - const { content, options } = data; - const markdown = generateMarkdown(content, options); - const filename = generateFilename(content.title); +// 处理下载 - 直接下载到默认目录 +async function handleDownload(filename: string, content: string): Promise { + // 使用 Data URL 方式(更可靠) + const dataUrl = `data:text/markdown;charset=utf-8,${encodeURIComponent(content)}`; - await downloadMarkdown(markdown, filename); + // 使用 chrome.downloads.download 直接下载到默认目录 + const downloadId = await chrome.downloads.download({ + url: dataUrl, + filename: filename, + saveAs: false // 直接保存到默认下载目录 + }); + + console.log('ReadMD: Download initiated', downloadId, filename); } // 下载完成后清理 diff --git a/src/popup/Popup.tsx b/src/popup/Popup.tsx index b4cbc7d..f92bb47 100644 --- a/src/popup/Popup.tsx +++ b/src/popup/Popup.tsx @@ -76,7 +76,7 @@ export function Popup() { } }; - // 下载文件 + // 下载文件 - 通过 background script 直接下载到默认目录 const handleDownload = async () => { if (!extractedContent) return; @@ -86,16 +86,15 @@ export function Popup() { includeUrl: settings.includeUrl }); - const blob = new Blob([markdown], { type: 'text/markdown;charset=utf-8' }); - const url = URL.createObjectURL(blob); const filename = `${extractedContent.title.replace(/[<>:"/\\|?*]/g, '_').substring(0, 100)}.md`; - await chrome.downloads.download({ - url: url, + + // 发送消息给 background script 处理下载 + await chrome.runtime.sendMessage({ + action: 'download', filename: filename, - saveAs: false + content: markdown }); - URL.revokeObjectURL(url); showStatus('success', '开始下载...'); setTimeout(() => { window.close(); @@ -200,7 +199,7 @@ export function Popup() { > - + 设置 diff --git a/src/types/chrome.d.ts b/src/types/chrome.d.ts index defad3d..0e4ec50 100644 --- a/src/types/chrome.d.ts +++ b/src/types/chrome.d.ts @@ -8,6 +8,7 @@ declare const chrome: { onMessage: { addListener: (callback: (message: any, sender: any, sendResponse: (response?: any) => void) => boolean | void) => void; }; + sendMessage: (message: any) => Promise; openOptionsPage: () => Promise; }; downloads: {