oauth-login.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (function attachBackgroundStep6(root, factory) {
  2. root.MultiPageBackgroundStep6 = factory();
  3. })(typeof self !== 'undefined' ? self : globalThis, function createBackgroundStep6Module() {
  4. function createStep6Executor(deps = {}) {
  5. const {
  6. addLog,
  7. completeStepFromBackground,
  8. getLoginAuthStateLabel,
  9. getState,
  10. isStep6RecoverableResult,
  11. isStep6SuccessResult,
  12. refreshOAuthUrlBeforeStep6,
  13. reuseOrCreateTab,
  14. runPreStep6CookieCleanup,
  15. sendToContentScriptResilient,
  16. shouldSkipLoginVerificationForCpaCallback,
  17. skipLoginVerificationStepsForCpaCallback,
  18. throwIfStopped,
  19. } = deps;
  20. async function executeStep6(state) {
  21. if (shouldSkipLoginVerificationForCpaCallback(state)) {
  22. await skipLoginVerificationStepsForCpaCallback();
  23. return;
  24. }
  25. if (!state.email) {
  26. throw new Error('缺少邮箱地址,请先完成步骤 3。');
  27. }
  28. await runPreStep6CookieCleanup();
  29. let attempt = 0;
  30. while (true) {
  31. throwIfStopped();
  32. attempt += 1;
  33. const currentState = attempt === 1 ? state : await getState();
  34. const password = currentState.password || currentState.customPassword || '';
  35. const oauthUrl = await refreshOAuthUrlBeforeStep6(currentState);
  36. if (attempt === 1) {
  37. await addLog('步骤 6:正在打开最新 OAuth 链接并登录...');
  38. } else {
  39. await addLog(`步骤 6:上一轮登录未进入验证码页,正在重新发起第 ${attempt} 轮登录尝试...`, 'warn');
  40. }
  41. await reuseOrCreateTab('signup-page', oauthUrl);
  42. const result = await sendToContentScriptResilient(
  43. 'signup-page',
  44. {
  45. type: 'EXECUTE_STEP',
  46. step: 6,
  47. source: 'background',
  48. payload: {
  49. email: currentState.email,
  50. password,
  51. },
  52. },
  53. {
  54. timeoutMs: 180000,
  55. retryDelayMs: 700,
  56. logMessage: '步骤 6:认证页正在切换,等待页面重新就绪后继续登录...',
  57. }
  58. );
  59. if (result?.error) {
  60. throw new Error(result.error);
  61. }
  62. if (isStep6SuccessResult(result)) {
  63. await completeStepFromBackground(6, {
  64. loginVerificationRequestedAt: result.loginVerificationRequestedAt || null,
  65. });
  66. return;
  67. }
  68. if (isStep6RecoverableResult(result)) {
  69. const reasonMessage = result.message
  70. || `当前停留在${getLoginAuthStateLabel(result.state)},准备重新执行步骤 6。`;
  71. await addLog(`步骤 6:${reasonMessage}`, 'warn');
  72. continue;
  73. }
  74. throw new Error('步骤 6:认证页未返回可识别的登录结果。');
  75. }
  76. }
  77. return { executeStep6 };
  78. }
  79. return { createStep6Executor };
  80. });