fetch-login-code.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. (function attachBackgroundStep7(root, factory) {
  2. root.MultiPageBackgroundStep7 = factory();
  3. })(typeof self !== 'undefined' ? self : globalThis, function createBackgroundStep7Module() {
  4. function createStep7Executor(deps = {}) {
  5. const {
  6. addLog,
  7. chrome,
  8. CLOUDFLARE_TEMP_EMAIL_PROVIDER,
  9. confirmCustomVerificationStepBypass,
  10. ensureStep7VerificationPageReady,
  11. executeStep6,
  12. getPanelMode,
  13. getMailConfig,
  14. getState,
  15. getTabId,
  16. HOTMAIL_PROVIDER,
  17. isTabAlive,
  18. isVerificationMailPollingError,
  19. LUCKMAIL_PROVIDER,
  20. resolveVerificationStep,
  21. reuseOrCreateTab,
  22. setState,
  23. setStepStatus,
  24. shouldSkipLoginVerificationForCpaCallback,
  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 runStep7Attempt(state) {
  32. const mail = getMailConfig(state);
  33. if (mail.error) throw new Error(mail.error);
  34. const stepStartedAt = Date.now();
  35. const authTabId = await getTabId('signup-page');
  36. if (authTabId) {
  37. await chrome.tabs.update(authTabId, { active: true });
  38. } else {
  39. if (!state.oauthUrl) {
  40. throw new Error('缺少登录用 OAuth 链接,请先完成步骤 6。');
  41. }
  42. await reuseOrCreateTab('signup-page', state.oauthUrl);
  43. }
  44. throwIfStopped();
  45. await ensureStep7VerificationPageReady();
  46. await addLog('步骤 7:登录验证码页面已就绪,开始获取验证码。', 'info');
  47. if (shouldUseCustomRegistrationEmail(state)) {
  48. await confirmCustomVerificationStepBypass(7);
  49. return;
  50. }
  51. throwIfStopped();
  52. if (mail.provider === HOTMAIL_PROVIDER || mail.provider === LUCKMAIL_PROVIDER || mail.provider === CLOUDFLARE_TEMP_EMAIL_PROVIDER) {
  53. await addLog(`步骤 7:正在通过 ${mail.label} 轮询验证码...`);
  54. } else {
  55. await addLog(`步骤 7:正在打开${mail.label}...`);
  56. const alive = await isTabAlive(mail.source);
  57. if (alive) {
  58. if (mail.navigateOnReuse) {
  59. await reuseOrCreateTab(mail.source, mail.url, {
  60. inject: mail.inject,
  61. injectSource: mail.injectSource,
  62. });
  63. } else {
  64. const tabId = await getTabId(mail.source);
  65. await chrome.tabs.update(tabId, { active: true });
  66. }
  67. } else {
  68. await reuseOrCreateTab(mail.source, mail.url, {
  69. inject: mail.inject,
  70. injectSource: mail.injectSource,
  71. });
  72. }
  73. }
  74. const shouldRefreshOAuthBeforeSubmit = getPanelMode(state) === 'cpa';
  75. let step6ReplayCompleted = false;
  76. await resolveVerificationStep(7, state, mail, {
  77. filterAfterTimestamp: mail.provider === HOTMAIL_PROVIDER ? undefined : Math.max(0, stepStartedAt - 60000),
  78. requestFreshCodeFirst: false,
  79. resendIntervalMs: (mail.provider === HOTMAIL_PROVIDER || mail.provider === '2925')
  80. ? 0
  81. : STANDARD_MAIL_VERIFICATION_RESEND_INTERVAL_MS,
  82. beforeSubmit: shouldRefreshOAuthBeforeSubmit ? async (result) => {
  83. if (step6ReplayCompleted) {
  84. return;
  85. }
  86. step6ReplayCompleted = true;
  87. await addLog(`步骤 7:已拿到登录验证码 ${result.code},先刷新 CPA OAuth 链接并重走步骤 6,再回填验证码。`, 'warn');
  88. await rerunStep6ForStep7Recovery({
  89. logMessage: '步骤 7:正在重新获取最新 CPA OAuth 链接,并快速重走步骤 6...',
  90. skipPreLoginCleanup: true,
  91. postStepDelayMs: 1200,
  92. });
  93. await ensureStep7VerificationPageReady();
  94. await addLog('步骤 7:登录验证码页面已重新就绪,开始回填刚才获取到的验证码。', 'info');
  95. } : undefined,
  96. });
  97. }
  98. async function rerunStep6ForStep7Recovery(options = {}) {
  99. const {
  100. logMessage = '步骤 7:正在回到步骤 6,重新发起登录验证码流程...',
  101. skipPreLoginCleanup = false,
  102. postStepDelayMs = 3000,
  103. } = options;
  104. const currentState = await getState();
  105. await addLog(logMessage, 'warn');
  106. await executeStep6(currentState, { skipPreLoginCleanup });
  107. if (postStepDelayMs > 0) {
  108. await sleepWithStop(postStepDelayMs);
  109. }
  110. }
  111. async function executeStep7(state) {
  112. if (shouldSkipLoginVerificationForCpaCallback(state)) {
  113. await setState({
  114. lastLoginCode: null,
  115. loginVerificationRequestedAt: null,
  116. });
  117. await setStepStatus(7, 'skipped');
  118. await addLog('步骤 7:当前已选择“第六步回调”,本轮无需获取登录验证码。', 'warn');
  119. return;
  120. }
  121. let currentState = state;
  122. let mailPollingAttempt = 1;
  123. let lastMailPollingError = null;
  124. while (true) {
  125. try {
  126. await runStep7Attempt(currentState);
  127. return;
  128. } catch (err) {
  129. if (!isVerificationMailPollingError(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. `步骤 7:检测到邮箱轮询类失败,准备从步骤 6 重新开始(${mailPollingAttempt}/${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS})...`,
  139. 'warn'
  140. );
  141. await rerunStep6ForStep7Recovery();
  142. currentState = await getState();
  143. }
  144. }
  145. if (lastMailPollingError) {
  146. throw new Error(
  147. `步骤 7:登录验证码流程在 ${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS} 轮邮箱轮询恢复后仍未成功。最后一次原因:${lastMailPollingError.message}`
  148. );
  149. }
  150. throw new Error('步骤 7:登录验证码流程未成功完成。');
  151. }
  152. return { executeStep7 };
  153. }
  154. return { createStep7Executor };
  155. });