fetch-login-code.js 5.3 KB

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