fetch-login-code.js 5.8 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. 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. shouldSkipLoginVerificationForCpaCallback,
  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) {
  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. async function executeStep8(state) {
  117. if (shouldSkipLoginVerificationForCpaCallback(state)) {
  118. await setState({
  119. lastLoginCode: null,
  120. loginVerificationRequestedAt: null,
  121. oauthFlowDeadlineAt: null,
  122. });
  123. await setStepStatus(8, 'skipped');
  124. await addLog('步骤 8:当前已选择“第七步回调”,本轮无需获取登录验证码。', 'warn');
  125. return;
  126. }
  127. let currentState = state;
  128. let mailPollingAttempt = 1;
  129. let lastMailPollingError = null;
  130. while (true) {
  131. try {
  132. await runStep8Attempt(currentState);
  133. return;
  134. } catch (err) {
  135. if (!isVerificationMailPollingError(err)) {
  136. throw err;
  137. }
  138. lastMailPollingError = err;
  139. if (mailPollingAttempt >= STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS) {
  140. break;
  141. }
  142. mailPollingAttempt += 1;
  143. await addLog(
  144. `步骤 8:检测到邮箱轮询类失败,准备从步骤 7 重新开始(${mailPollingAttempt}/${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS})...`,
  145. 'warn'
  146. );
  147. await rerunStep7ForStep8Recovery();
  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. });