fill-password.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (function attachBackgroundStep3(root, factory) {
  2. root.MultiPageBackgroundStep3 = factory();
  3. })(typeof self !== 'undefined' ? self : globalThis, function createBackgroundStep3Module() {
  4. function createStep3Executor(deps = {}) {
  5. const {
  6. addLog,
  7. chrome,
  8. ensureContentScriptReadyOnTab,
  9. generatePassword,
  10. getTabId,
  11. isTabAlive,
  12. sendToContentScript,
  13. setPasswordState,
  14. setState,
  15. SIGNUP_PAGE_INJECT_FILES,
  16. } = deps;
  17. async function executeStep3(state) {
  18. const resolvedEmail = state.email;
  19. if (!resolvedEmail) {
  20. throw new Error('缺少邮箱地址,请先完成步骤 2。');
  21. }
  22. const signupTabId = await getTabId('signup-page');
  23. if (!signupTabId || !(await isTabAlive('signup-page'))) {
  24. throw new Error('认证页面标签页已关闭,请先重新完成步骤 2。');
  25. }
  26. const password = state.customPassword || state.password || generatePassword();
  27. await setPasswordState(password);
  28. const accounts = state.accounts || [];
  29. accounts.push({ email: resolvedEmail, password, createdAt: new Date().toISOString() });
  30. await setState({ accounts });
  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: '步骤 3:密码页内容脚本未就绪,正在等待页面恢复...',
  38. });
  39. await addLog(
  40. `步骤 3:正在填写密码,邮箱为 ${resolvedEmail},密码为${state.customPassword ? '自定义' : '自动生成'}(${password.length} 位)`
  41. );
  42. await sendToContentScript('signup-page', {
  43. type: 'EXECUTE_STEP',
  44. step: 3,
  45. source: 'background',
  46. payload: { email: resolvedEmail, password },
  47. });
  48. }
  49. return { executeStep3 };
  50. }
  51. return { createStep3Executor };
  52. });