From ed3a4c0ee36a885e8d050d40172e8e16eaf3ce18 Mon Sep 17 00:00:00 2001 From: Xi Xu Date: Sun, 10 Aug 2025 12:12:23 +0800 Subject: [PATCH] Prevent multiple suggest calls in handleDownload Introduces a guard to ensure the suggest callback is only called once in handleDownload, avoiding potential issues from multiple invocations in error or early return scenarios. --- background.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/background.js b/background.js index 57f2c28..1ad1392 100644 --- a/background.js +++ b/background.js @@ -215,12 +215,21 @@ chrome.downloads.onDeterminingFilename.addListener((downloadItem, suggest) => { }); async function handleDownload(downloadItem, suggest) { + let suggestionCalled = false; + + const callSuggest = () => { + if (!suggestionCalled) { + suggestionCalled = true; + suggest(); + } + }; + try { const settings = await chrome.storage.sync.get(DEFAULT_SETTINGS); // 检查扩展是否已启用并配置了域名 if (!settings.enabled || !settings.xgetDomain) { - suggest(); + callSuggest(); return; } @@ -254,11 +263,11 @@ async function handleDownload(downloadItem, suggest) { console.log("无法向标签页发送通知"); } } else { - suggest(); + callSuggest(); } } catch (error) { console.error("处理下载时出错:", error); - suggest(); + callSuggest(); } }