fetch-login-code.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. (function attachBackgroundStep8(root, factory) {
  2. root.MultiPageBackgroundStep8 = factory();
  3. })(typeof self !== 'undefined' ? self : globalThis, function createBackgroundStep8Module() {
  4. function createStep8Executor(deps = {}) {
  5. const {
  6. A4SKY_PROVIDER,
  7. addLog,
  8. chrome,
  9. CLOUDFLARE_TEMP_EMAIL_PROVIDER,
  10. confirmCustomVerificationStepBypass,
  11. ensureStep8VerificationPageReady,
  12. executeStep7,
  13. getOAuthFlowRemainingMs,
  14. getOAuthFlowStepTimeoutMs,
  15. getMailConfig,
  16. getState,
  17. getTabId,
  18. HOTMAIL_PROVIDER,
  19. isTabAlive,
  20. isVerificationMailPollingError,
  21. LUCKMAIL_PROVIDER,
  22. resolveVerificationStep,
  23. reuseOrCreateTab,
  24. setState,
  25. setStepStatus,
  26. shouldUseCustomRegistrationEmail,
  27. sleepWithStop,
  28. STANDARD_MAIL_VERIFICATION_RESEND_INTERVAL_MS,
  29. STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS,
  30. throwIfStopped,
  31. } = deps;
  32. async function getStep8ReadyTimeoutMs(actionLabel) {
  33. if (typeof getOAuthFlowStepTimeoutMs !== 'function') {
  34. return 15000;
  35. }
  36. return getOAuthFlowStepTimeoutMs(15000, {
  37. step: 8,
  38. actionLabel,
  39. });
  40. }
  41. function getStep8RemainingTimeResolver() {
  42. if (typeof getOAuthFlowRemainingMs !== 'function') {
  43. return undefined;
  44. }
  45. return async (details = {}) => getOAuthFlowRemainingMs({
  46. step: 8,
  47. actionLabel: details.actionLabel || '登录验证码流程',
  48. });
  49. }
  50. async function runStep8Attempt(state) {
  51. const mail = getMailConfig(state);
  52. if (mail.error) throw new Error(mail.error);
  53. const stepStartedAt = Date.now();
  54. const authTabId = await getTabId('signup-page');
  55. if (authTabId) {
  56. await chrome.tabs.update(authTabId, { active: true });
  57. } else {
  58. if (!state.oauthUrl) {
  59. throw new Error('缺少登录用 OAuth 链接,请先完成步骤 7。');
  60. }
  61. await reuseOrCreateTab('signup-page', state.oauthUrl);
  62. }
  63. throwIfStopped();
  64. await ensureStep8VerificationPageReady({
  65. timeoutMs: await getStep8ReadyTimeoutMs('确认登录验证码页已就绪'),
  66. });
  67. await addLog('步骤 8:登录验证码页面已就绪,开始获取验证码。', 'info');
  68. if (shouldUseCustomRegistrationEmail(state)) {
  69. await confirmCustomVerificationStepBypass(8);
  70. return;
  71. }
  72. throwIfStopped();
  73. if (mail.provider === HOTMAIL_PROVIDER || mail.provider === LUCKMAIL_PROVIDER || mail.provider === CLOUDFLARE_TEMP_EMAIL_PROVIDER || mail.provider === A4SKY_PROVIDER) {
  74. await addLog(`步骤 8:正在通过 ${mail.label} 轮询验证码...`);
  75. } else {
  76. await addLog(`步骤 8:正在打开${mail.label}...`);
  77. const alive = await isTabAlive(mail.source);
  78. if (alive) {
  79. if (mail.navigateOnReuse) {
  80. await reuseOrCreateTab(mail.source, mail.url, {
  81. inject: mail.inject,
  82. injectSource: mail.injectSource,
  83. });
  84. } else {
  85. const tabId = await getTabId(mail.source);
  86. await chrome.tabs.update(tabId, { active: true });
  87. }
  88. } else {
  89. await reuseOrCreateTab(mail.source, mail.url, {
  90. inject: mail.inject,
  91. injectSource: mail.injectSource,
  92. });
  93. }
  94. }
  95. await resolveVerificationStep(8, state, mail, {
  96. filterAfterTimestamp: stepStartedAt,
  97. getRemainingTimeMs: getStep8RemainingTimeResolver(),
  98. requestFreshCodeFirst: false,
  99. resendIntervalMs: (mail.provider === HOTMAIL_PROVIDER || mail.provider === '2925')
  100. ? 0
  101. : STANDARD_MAIL_VERIFICATION_RESEND_INTERVAL_MS,
  102. });
  103. }
  104. async function rerunStep7ForStep8Recovery(options = {}) {
  105. const {
  106. logMessage = '步骤 8:正在回到步骤 7,重新发起登录验证码流程...',
  107. postStepDelayMs = 3000,
  108. } = options;
  109. const currentState = await getState();
  110. await addLog(logMessage, 'warn');
  111. await executeStep7(currentState);
  112. if (postStepDelayMs > 0) {
  113. await sleepWithStop(postStepDelayMs);
  114. }
  115. }
  116. function isStep8RestartStep7Error(error) {
  117. const message = String(error?.message || error || '');
  118. return /STEP8_RESTART_STEP7::/i.test(message);
  119. }
  120. async function executeStep8(state) {
  121. let currentState = state;
  122. let mailPollingAttempt = 1;
  123. let lastMailPollingError = null;
  124. while (true) {
  125. try {
  126. await runStep8Attempt(currentState);
  127. return;
  128. } catch (err) {
  129. if (!isVerificationMailPollingError(err) && !isStep8RestartStep7Error(err)) {
  130. throw err;
  131. }
  132. lastMailPollingError = err;
  133. if (mailPollingAttempt >= STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS) {
  134. break;
  135. }
  136. mailPollingAttempt += 1;
  137. await addLog(
  138. isStep8RestartStep7Error(err)
  139. ? `步骤 8:检测到认证页进入重试/超时报错状态,准备从步骤 7 重新开始(${mailPollingAttempt}/${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS})...`
  140. : `步骤 8:检测到邮箱轮询类失败,准备从步骤 7 重新开始(${mailPollingAttempt}/${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS})...`,
  141. 'warn'
  142. );
  143. await rerunStep7ForStep8Recovery({
  144. logMessage: isStep8RestartStep7Error(err)
  145. ? '步骤 8:认证页进入重试/超时报错状态,正在回到步骤 7 重新发起登录流程...'
  146. : '步骤 8:正在回到步骤 7,重新发起登录验证码流程...',
  147. });
  148. currentState = await getState();
  149. }
  150. }
  151. if (lastMailPollingError) {
  152. throw new Error(
  153. `步骤 8:登录验证码流程在 ${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS} 轮邮箱轮询恢复后仍未成功。最后一次原因:${lastMailPollingError.message}`
  154. );
  155. }
  156. throw new Error('步骤 8:登录验证码流程未成功完成。');
  157. }
  158. return { executeStep8 };
  159. }
  160. return { createStep8Executor };
  161. });