fetch-login-code.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 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 runStep8Attempt(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 链接,请先完成步骤 7。');
  41. }
  42. await reuseOrCreateTab('signup-page', state.oauthUrl);
  43. }
  44. throwIfStopped();
  45. await ensureStep8VerificationPageReady();
  46. await addLog('步骤 8:登录验证码页面已就绪,开始获取验证码。', 'info');
  47. if (shouldUseCustomRegistrationEmail(state)) {
  48. await confirmCustomVerificationStepBypass(8);
  49. return;
  50. }
  51. throwIfStopped();
  52. if (mail.provider === HOTMAIL_PROVIDER || mail.provider === LUCKMAIL_PROVIDER || mail.provider === CLOUDFLARE_TEMP_EMAIL_PROVIDER) {
  53. await addLog(`步骤 8:正在通过 ${mail.label} 轮询验证码...`);
  54. } else {
  55. await addLog(`步骤 8:正在打开${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 step7ReplayCompleted = false;
  76. await resolveVerificationStep(8, 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 (step7ReplayCompleted) {
  84. return;
  85. }
  86. step7ReplayCompleted = true;
  87. await addLog(`步骤 8:已拿到登录验证码 ${result.code},先刷新 CPA OAuth 链接并重走步骤 7,再回填验证码。`, 'warn');
  88. await rerunStep7ForStep8Recovery({
  89. logMessage: '步骤 8:正在重新获取最新 CPA OAuth 链接,并快速重走步骤 7...',
  90. postStepDelayMs: 1200,
  91. });
  92. await ensureStep8VerificationPageReady();
  93. await addLog('步骤 8:登录验证码页面已重新就绪,开始回填刚才获取到的验证码。', 'info');
  94. } : undefined,
  95. });
  96. }
  97. async function rerunStep7ForStep8Recovery(options = {}) {
  98. const {
  99. logMessage = '步骤 8:正在回到步骤 7,重新发起登录验证码流程...',
  100. postStepDelayMs = 3000,
  101. } = options;
  102. const currentState = await getState();
  103. await addLog(logMessage, 'warn');
  104. await executeStep7(currentState);
  105. if (postStepDelayMs > 0) {
  106. await sleepWithStop(postStepDelayMs);
  107. }
  108. }
  109. async function executeStep8(state) {
  110. if (shouldSkipLoginVerificationForCpaCallback(state)) {
  111. await setState({
  112. lastLoginCode: null,
  113. loginVerificationRequestedAt: null,
  114. });
  115. await setStepStatus(8, 'skipped');
  116. await addLog('步骤 8:当前已选择“第七步回调”,本轮无需获取登录验证码。', 'warn');
  117. return;
  118. }
  119. let currentState = state;
  120. let mailPollingAttempt = 1;
  121. let lastMailPollingError = null;
  122. while (true) {
  123. try {
  124. await runStep8Attempt(currentState);
  125. return;
  126. } catch (err) {
  127. if (!isVerificationMailPollingError(err)) {
  128. throw err;
  129. }
  130. lastMailPollingError = err;
  131. if (mailPollingAttempt >= STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS) {
  132. break;
  133. }
  134. mailPollingAttempt += 1;
  135. await addLog(
  136. `步骤 8:检测到邮箱轮询类失败,准备从步骤 7 重新开始(${mailPollingAttempt}/${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS})...`,
  137. 'warn'
  138. );
  139. await rerunStep7ForStep8Recovery();
  140. currentState = await getState();
  141. }
  142. }
  143. if (lastMailPollingError) {
  144. throw new Error(
  145. `步骤 8:登录验证码流程在 ${STEP7_MAIL_POLLING_RECOVERY_MAX_ATTEMPTS} 轮邮箱轮询恢复后仍未成功。最后一次原因:${lastMailPollingError.message}`
  146. );
  147. }
  148. throw new Error('步骤 8:登录验证码流程未成功完成。');
  149. }
  150. return { executeStep8 };
  151. }
  152. return { createStep8Executor };
  153. });