oauth-login.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. completeStepFromBackground,
  8. getErrorMessage,
  9. getLoginAuthStateLabel,
  10. getState,
  11. isStep6RecoverableResult,
  12. isStep6SuccessResult,
  13. refreshOAuthUrlBeforeStep6,
  14. reuseOrCreateTab,
  15. sendToContentScriptResilient,
  16. shouldSkipLoginVerificationForCpaCallback,
  17. skipLoginVerificationStepsForCpaCallback,
  18. STEP6_MAX_ATTEMPTS,
  19. throwIfStopped,
  20. } = deps;
  21. async function executeStep7(state) {
  22. if (shouldSkipLoginVerificationForCpaCallback(state)) {
  23. await skipLoginVerificationStepsForCpaCallback();
  24. return;
  25. }
  26. if (!state.email) {
  27. throw new Error('缺少邮箱地址,请先完成步骤 3。');
  28. }
  29. let attempt = 0;
  30. let lastError = null;
  31. while (attempt < STEP6_MAX_ATTEMPTS) {
  32. throwIfStopped();
  33. attempt += 1;
  34. try {
  35. const currentState = attempt === 1 ? state : await getState();
  36. const password = currentState.password || currentState.customPassword || '';
  37. const oauthUrl = await refreshOAuthUrlBeforeStep6(currentState);
  38. if (attempt === 1) {
  39. await addLog('步骤 7:正在打开最新 OAuth 链接并登录...');
  40. } else {
  41. await addLog(`步骤 7:上一轮失败后,正在进行第 ${attempt} 次尝试(最多 ${STEP6_MAX_ATTEMPTS} 次)...`, 'warn');
  42. }
  43. await reuseOrCreateTab('signup-page', oauthUrl);
  44. const result = await sendToContentScriptResilient(
  45. 'signup-page',
  46. {
  47. type: 'EXECUTE_STEP',
  48. step: 7,
  49. source: 'background',
  50. payload: {
  51. email: currentState.email,
  52. password,
  53. },
  54. },
  55. {
  56. timeoutMs: 180000,
  57. retryDelayMs: 700,
  58. logMessage: '步骤 7:认证页正在切换,等待页面重新就绪后继续登录...',
  59. }
  60. );
  61. if (result?.error) {
  62. throw new Error(result.error);
  63. }
  64. if (isStep6SuccessResult(result)) {
  65. await completeStepFromBackground(7, {
  66. loginVerificationRequestedAt: result.loginVerificationRequestedAt || null,
  67. });
  68. return;
  69. }
  70. if (isStep6RecoverableResult(result)) {
  71. const reasonMessage = result.message
  72. || `当前停留在${getLoginAuthStateLabel(result.state)},准备重新执行步骤 7。`;
  73. throw new Error(reasonMessage);
  74. }
  75. throw new Error('步骤 7:认证页未返回可识别的登录结果。');
  76. } catch (err) {
  77. throwIfStopped(err);
  78. lastError = err;
  79. if (attempt >= STEP6_MAX_ATTEMPTS) {
  80. break;
  81. }
  82. await addLog(`步骤 7:第 ${attempt} 次尝试失败,原因:${getErrorMessage(err)};准备重试...`, 'warn');
  83. }
  84. }
  85. throw new Error(`步骤 7:判断失败后已重试 ${STEP6_MAX_ATTEMPTS - 1} 次,仍未成功。最后原因:${getErrorMessage(lastError)}`);
  86. }
  87. return { executeStep7 };
  88. }
  89. return { createStep7Executor };
  90. });