From db4621009697dea3c8df77d8f68744d9096e683e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=A8=E9=9C=96=E9=93=83?= Date: Mon, 12 Jan 2026 22:14:01 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/background.js | 25 +++++++++++++++++++++---- public/manifest.json | 3 ++- src/components/CopyButton.js | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/public/background.js b/public/background.js index 5d9fa12..4f7ad17 100644 --- a/public/background.js +++ b/public/background.js @@ -1,8 +1,25 @@ +console.log('Background script loaded'); + chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { - if (request.action === 'start') { + console.log('收到消息:', request); + + if (request.action === 'copy') { + console.log('开始复制文本:', request.text); + navigator.clipboard.writeText(request.text) - .then(() => sendResponse({success: true})) - .catch((err) => console.log(err)); - return true; + .then(() => { + console.log('复制成功'); + sendResponse({success: true}); + }) + .catch((err) => { + console.error('复制失败:', err); + sendResponse({success: false, error: err.message}); + }); + + return true; // 保持消息端口开放以支持异步响应 } + + // 对于未知的操作,也返回响应 + sendResponse({success: false, error: '未知操作'}); + return false; }); \ No newline at end of file diff --git a/public/manifest.json b/public/manifest.json index 36068e2..23b5043 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -4,7 +4,8 @@ "version": "1.0", "description": "A Chrome Extension with React Router: User List + Actions Pages", "permissions": [ - "storage" + "storage", + "clipboardWrite" ], "background": { "service_worker": "background.js" diff --git a/src/components/CopyButton.js b/src/components/CopyButton.js index e8638a5..254b457 100644 --- a/src/components/CopyButton.js +++ b/src/components/CopyButton.js @@ -36,22 +36,52 @@ const CopyButton = ({ const handleCopy = async () => { try { + console.log('开始复制:', text); + + // 首先尝试直接使用navigator.clipboard(在popup页面中可能可用) + try { + console.log('尝试直接使用navigator.clipboard复制'); + await navigator.clipboard.writeText(text.toString()); + console.log('直接复制成功'); + handleCopySuccess(); + return; + } catch (directError) { + console.log('直接复制失败,尝试使用Chrome扩展API:', directError); + } + + // 如果直接复制失败,尝试使用Chrome扩展API if (chrome && chrome.runtime && chrome.runtime.sendMessage) { + console.log('使用Chrome扩展API复制'); + + // 使用Chrome扩展API复制 chrome.runtime.sendMessage({ action: 'copy', text: text }, (response) => { + console.log('收到background响应:', response, 'lastError:', chrome.runtime.lastError); + + // 检查是否有运行时错误 + if (chrome.runtime.lastError) { + console.error('Chrome运行时错误:', chrome.runtime.lastError.message); + handleCopyError(); + return; + } + + // 检查响应 if (response && response.success) { + console.log('通过background复制成功'); handleCopySuccess(); } else { + console.error('通过background复制失败:', response?.error); handleCopyError(); } }); } else { - await navigator.clipboard.writeText(text.toString()); - handleCopySuccess(); + console.error('没有可用的复制方法'); + handleCopyError(); } } catch (err) { + console.error('复制过程中发生错误:', err); handleCopyError(); } };