| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- (function attachBackgroundStep8(root, factory) {
- root.MultiPageBackgroundStep8 = factory();
- })(typeof self !== 'undefined' ? self : globalThis, function createBackgroundStep8Module() {
- function createStep8Executor(deps = {}) {
- const {
- addLog,
- chrome,
- CLOUDFLARE_TEMP_EMAIL_PROVIDER,
- confirmCustomVerificationStepBypass,
- ensureStep8VerificationPageReady,
- executeStep7,
- getOAuthFlowRemainingMs,
- getOAuthFlowStepTimeoutMs,
- getMailConfig,
- getState,
- getTabId,
- HOTMAIL_PROVIDER,
- isTabAlive,
- isVerificationMailPollingError,
- LUCKMAIL_PROVIDER,
- resolveVerificationStep,
- reuseOrCreateTab,
- setState,
- setStepStatus,
- shouldUseCustomRegistrationEmail,
- sleepWithStop,
- STANDARD_MAIL_VERIFICATION_RESEND_INTERVAL_MS,
- STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS,
- throwIfStopped,
- } = deps;
- async function getStep8ReadyTimeoutMs(actionLabel) {
- if (typeof getOAuthFlowStepTimeoutMs !== 'function') {
- return 15000;
- }
- return getOAuthFlowStepTimeoutMs(15000, {
- step: 8,
- actionLabel,
- });
- }
- function getStep8RemainingTimeResolver() {
- if (typeof getOAuthFlowRemainingMs !== 'function') {
- return undefined;
- }
- return async (details = {}) => getOAuthFlowRemainingMs({
- step: 8,
- actionLabel: details.actionLabel || '登录验证码流程',
- });
- }
- async function runStep8Attempt(state) {
- const mail = getMailConfig(state);
- if (mail.error) throw new Error(mail.error);
- const stepStartedAt = Date.now();
- const authTabId = await getTabId('signup-page');
- if (authTabId) {
- await chrome.tabs.update(authTabId, { active: true });
- } else {
- if (!state.oauthUrl) {
- throw new Error('缺少登录用 OAuth 链接,请先完成步骤 7。');
- }
- await reuseOrCreateTab('signup-page', state.oauthUrl);
- }
- throwIfStopped();
- await ensureStep8VerificationPageReady({
- timeoutMs: await getStep8ReadyTimeoutMs('确认登录验证码页已就绪'),
- });
- await addLog('步骤 8:登录验证码页面已就绪,开始获取验证码。', 'info');
- if (shouldUseCustomRegistrationEmail(state)) {
- await confirmCustomVerificationStepBypass(8);
- return;
- }
- throwIfStopped();
- if (mail.provider === HOTMAIL_PROVIDER || mail.provider === LUCKMAIL_PROVIDER || mail.provider === CLOUDFLARE_TEMP_EMAIL_PROVIDER) {
- await addLog(`步骤 8:正在通过 ${mail.label} 轮询验证码...`);
- } else {
- await addLog(`步骤 8:正在打开${mail.label}...`);
- const alive = await isTabAlive(mail.source);
- if (alive) {
- if (mail.navigateOnReuse) {
- await reuseOrCreateTab(mail.source, mail.url, {
- inject: mail.inject,
- injectSource: mail.injectSource,
- });
- } else {
- const tabId = await getTabId(mail.source);
- await chrome.tabs.update(tabId, { active: true });
- }
- } else {
- await reuseOrCreateTab(mail.source, mail.url, {
- inject: mail.inject,
- injectSource: mail.injectSource,
- });
- }
- }
- await resolveVerificationStep(8, state, mail, {
- filterAfterTimestamp: stepStartedAt,
- getRemainingTimeMs: getStep8RemainingTimeResolver(),
- requestFreshCodeFirst: false,
- resendIntervalMs: (mail.provider === HOTMAIL_PROVIDER || mail.provider === '2925')
- ? 0
- : STANDARD_MAIL_VERIFICATION_RESEND_INTERVAL_MS,
- });
- }
- async function rerunStep7ForStep8Recovery(options = {}) {
- const {
- logMessage = '步骤 8:正在回到步骤 7,重新发起登录验证码流程...',
- postStepDelayMs = 3000,
- } = options;
- const currentState = await getState();
- await addLog(logMessage, 'warn');
- await executeStep7(currentState);
- if (postStepDelayMs > 0) {
- await sleepWithStop(postStepDelayMs);
- }
- }
- function isStep8RestartStep7Error(error) {
- const message = String(error?.message || error || '');
- return /STEP8_RESTART_STEP7::/i.test(message);
- }
- async function executeStep8(state) {
- let currentState = state;
- let mailPollingAttempt = 1;
- let lastMailPollingError = null;
- while (true) {
- try {
- await runStep8Attempt(currentState);
- return;
- } catch (err) {
- if (!isVerificationMailPollingError(err) && !isStep8RestartStep7Error(err)) {
- throw err;
- }
- lastMailPollingError = err;
- if (mailPollingAttempt >= STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS) {
- break;
- }
- mailPollingAttempt += 1;
- await addLog(
- isStep8RestartStep7Error(err)
- ? `步骤 8:检测到认证页进入重试/超时报错状态,准备从步骤 7 重新开始(${mailPollingAttempt}/${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS})...`
- : `步骤 8:检测到邮箱轮询类失败,准备从步骤 7 重新开始(${mailPollingAttempt}/${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS})...`,
- 'warn'
- );
- await rerunStep7ForStep8Recovery({
- logMessage: isStep8RestartStep7Error(err)
- ? '步骤 8:认证页进入重试/超时报错状态,正在回到步骤 7 重新发起登录流程...'
- : '步骤 8:正在回到步骤 7,重新发起登录验证码流程...',
- });
- currentState = await getState();
- }
- }
- if (lastMailPollingError) {
- throw new Error(
- `步骤 8:登录验证码流程在 ${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS} 轮邮箱轮询恢复后仍未成功。最后一次原因:${lastMailPollingError.message}`
- );
- }
- throw new Error('步骤 8:登录验证码流程未成功完成。');
- }
- return { executeStep8 };
- }
- return { createStep8Executor };
- });
|