submit-signup-email.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. (function attachBackgroundStep2(root, factory) {
  2. root.MultiPageBackgroundStep2 = factory();
  3. })(typeof self !== 'undefined' ? self : globalThis, function createBackgroundStep2Module() {
  4. function createStep2Executor(deps = {}) {
  5. const {
  6. addLog,
  7. chrome,
  8. completeStepFromBackground,
  9. ensureContentScriptReadyOnTab,
  10. ensureSignupEntryPageReady,
  11. ensureSignupPostEmailPageReadyInTab,
  12. getTabId,
  13. isTabAlive,
  14. resolveSignupEmailForFlow,
  15. sendToContentScriptResilient,
  16. SIGNUP_PAGE_INJECT_FILES,
  17. } = deps;
  18. async function executeStep2(state) {
  19. const resolvedEmail = await resolveSignupEmailForFlow(state);
  20. const maxAttempts = 2;
  21. let lastError = null;
  22. for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
  23. let signupTabId = await getTabId('signup-page');
  24. if (!signupTabId || !(await isTabAlive('signup-page'))) {
  25. await addLog('步骤 2:未发现可用的注册页标签,正在重新打开 ChatGPT 官网...', 'warn');
  26. signupTabId = (await ensureSignupEntryPageReady(2, { reloadIfSameUrl: true })).tabId;
  27. } else if (attempt > 1) {
  28. await addLog(`步骤 2:第 ${attempt}/${maxAttempts} 次尝试,正在重新打开注册入口后重试邮箱提交...`, 'warn');
  29. signupTabId = (await ensureSignupEntryPageReady(2, { reloadIfSameUrl: true })).tabId;
  30. } else {
  31. await chrome.tabs.update(signupTabId, { active: true });
  32. await ensureContentScriptReadyOnTab('signup-page', signupTabId, {
  33. inject: SIGNUP_PAGE_INJECT_FILES,
  34. injectSource: 'signup-page',
  35. timeoutMs: 45000,
  36. retryDelayMs: 900,
  37. logMessage: '步骤 2:注册入口页内容脚本未就绪,正在等待页面恢复...',
  38. });
  39. }
  40. try {
  41. const step2Result = await sendToContentScriptResilient('signup-page', {
  42. type: 'EXECUTE_STEP',
  43. step: 2,
  44. source: 'background',
  45. payload: { email: resolvedEmail },
  46. }, {
  47. timeoutMs: 20000,
  48. retryDelayMs: 700,
  49. logMessage: '步骤 2:官网注册入口正在切换,等待页面恢复后继续输入邮箱...',
  50. });
  51. if (step2Result?.error) {
  52. throw new Error(step2Result.error);
  53. }
  54. if (!step2Result?.alreadyOnPasswordPage) {
  55. await addLog(`步骤 2:邮箱 ${resolvedEmail} 已提交,正在等待页面加载并确认下一步入口...`);
  56. }
  57. const landingResult = await ensureSignupPostEmailPageReadyInTab(signupTabId, 2, {
  58. skipUrlWait: Boolean(step2Result?.alreadyOnPasswordPage),
  59. });
  60. await completeStepFromBackground(2, {
  61. email: resolvedEmail,
  62. nextSignupState: landingResult?.state || 'password_page',
  63. nextSignupUrl: landingResult?.url || step2Result?.url || '',
  64. skippedPasswordStep: landingResult?.state === 'verification_page',
  65. });
  66. return;
  67. } catch (error) {
  68. lastError = error;
  69. if (attempt >= maxAttempts) {
  70. throw error;
  71. }
  72. await addLog(`步骤 2:当前尝试未能稳定进入下一页面,准备重新打开注册入口后重试。原因:${error?.message || error}`, 'warn');
  73. }
  74. }
  75. throw lastError || new Error('步骤 2:邮箱提交流程失败。');
  76. }
  77. return { executeStep2 };
  78. }
  79. return { createStep2Executor };
  80. });