From 43914dcb785939033dbe60af24a92058284fbb89 Mon Sep 17 00:00:00 2001 From: QLHazyCoder <2825305047@qq.com> Date: Thu, 9 Apr 2026 22:40:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=8F=AF=E9=9D=A0?= =?UTF-8?q?=E7=9A=84=E9=82=AE=E4=BB=B6=E5=86=85=E5=AE=B9=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E9=80=9A=E4=BF=A1=E5=A4=84=E7=90=86=EF=BC=8C=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=81=A2=E5=A4=8D=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- background.js | 59 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/background.js b/background.js index 9229595..a57ddfd 100644 --- a/background.js +++ b/background.js @@ -548,6 +548,46 @@ async function sendToContentScriptResilient(source, message, options = {}) { throw lastError || new Error(`等待 ${getSourceLabel(source)} 重新就绪超时。`); } +async function sendToMailContentScriptResilient(mail, message, options = {}) { + const { timeoutMs = 45000, maxRecoveryAttempts = 2 } = options; + const start = Date.now(); + let lastError = null; + let recoveries = 0; + let logged = false; + + while (Date.now() - start < timeoutMs) { + throwIfStopped(); + + try { + return await sendToContentScript(mail.source, message); + } catch (err) { + if (!isRetryableContentScriptTransportError(err)) { + throw err; + } + + lastError = err; + if (!logged) { + await addLog(`步骤 ${message.step}:${mail.label} 页面通信异常,正在尝试让邮箱页重新就绪...`, 'warn'); + logged = true; + } + + if (recoveries >= maxRecoveryAttempts) { + break; + } + + recoveries += 1; + await reuseOrCreateTab(mail.source, mail.url, { + inject: mail.inject, + injectSource: mail.injectSource, + reloadIfSameUrl: true, + }); + await new Promise((resolve) => setTimeout(resolve, 800)); + } + } + + throw lastError || new Error(`${mail.label} 页面未能重新就绪。`); +} + // ============================================================ // Logging // ============================================================ @@ -1718,12 +1758,19 @@ async function pollFreshVerificationCode(step, state, mail, pollOverrides = {}) }); try { - const result = await sendToContentScript(mail.source, { - type: 'POLL_EMAIL', - step, - source: 'background', - payload, - }); + const result = await sendToMailContentScriptResilient( + mail, + { + type: 'POLL_EMAIL', + step, + source: 'background', + payload, + }, + { + timeoutMs: 45000, + maxRecoveryAttempts: 2, + } + ); if (result && result.error) { throw new Error(result.error);