cursor-switch 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/README.md +76 -75
  2. package/SECURITY.md +22 -19
  3. package/bin/cswitch.js +31 -8
  4. package/dist/cli.d.ts +0 -4
  5. package/dist/cli.js +404 -241
  6. package/dist/core/account-store.d.ts +0 -7
  7. package/dist/core/account-store.js +22 -28
  8. package/dist/core/cdp-engine.d.ts +11 -49
  9. package/dist/core/cdp-engine.js +155 -110
  10. package/dist/core/config-store.d.ts +7 -6
  11. package/dist/core/config-store.js +33 -12
  12. package/dist/core/cursor-paths.d.ts +8 -18
  13. package/dist/core/cursor-paths.js +18 -37
  14. package/dist/core/dashboard-launcher.d.ts +0 -20
  15. package/dist/core/dashboard-launcher.js +3 -53
  16. package/dist/core/extract-client.d.ts +57 -0
  17. package/dist/core/extract-client.js +219 -0
  18. package/dist/core/extract-menu.d.ts +3 -0
  19. package/dist/core/extract-menu.js +360 -0
  20. package/dist/core/extract-service.d.ts +64 -0
  21. package/dist/core/extract-service.js +435 -0
  22. package/dist/core/fingerprint.d.ts +0 -4
  23. package/dist/core/fingerprint.js +0 -6
  24. package/dist/core/oauth-receiver.d.ts +1 -11
  25. package/dist/core/oauth-receiver.js +509 -527
  26. package/dist/core/official-api.d.ts +0 -17
  27. package/dist/core/official-api.js +4 -23
  28. package/dist/core/outlook-launcher.d.ts +0 -31
  29. package/dist/core/outlook-launcher.js +194 -248
  30. package/dist/core/process-manager.d.ts +0 -14
  31. package/dist/core/process-manager.js +7 -22
  32. package/dist/core/pure-login-service.d.ts +54 -38
  33. package/dist/core/pure-login-service.js +483 -395
  34. package/dist/core/refund-client.d.ts +0 -22
  35. package/dist/core/refund-client.js +7 -27
  36. package/dist/core/refund-service.d.ts +0 -16
  37. package/dist/core/refund-service.js +8 -34
  38. package/dist/core/safe-storage.d.ts +27 -0
  39. package/dist/core/safe-storage.js +284 -0
  40. package/dist/core/sqlite-bridge.d.ts +10 -11
  41. package/dist/core/sqlite-bridge.js +112 -47
  42. package/dist/core/storage-bridge.d.ts +14 -5
  43. package/dist/core/storage-bridge.js +84 -24
  44. package/dist/core/switch-orchestrator.d.ts +96 -0
  45. package/dist/core/switch-orchestrator.js +341 -0
  46. package/dist/core/token-exchange.d.ts +81 -0
  47. package/dist/core/token-exchange.js +275 -0
  48. package/dist/core/token-parser.d.ts +56 -20
  49. package/dist/core/token-parser.js +354 -43
  50. package/dist/core/update-checker.d.ts +1 -29
  51. package/dist/core/update-checker.js +28 -43
  52. package/dist/index.d.ts +1 -3
  53. package/dist/index.js +1 -3
  54. package/dist/menu.d.ts +0 -7
  55. package/dist/menu.js +445 -548
  56. package/dist/types.d.ts +12 -4
  57. package/dist/types.js +0 -4
  58. package/dist/utils/account-text-importer.d.ts +9 -0
  59. package/dist/utils/account-text-importer.js +96 -0
  60. package/dist/utils/browser-utils.d.ts +0 -24
  61. package/dist/utils/browser-utils.js +0 -57
  62. package/dist/utils/clipboard.d.ts +0 -4
  63. package/dist/utils/clipboard.js +0 -5
  64. package/dist/utils/credential-bundle-ui.d.ts +36 -0
  65. package/dist/utils/credential-bundle-ui.js +246 -0
  66. package/dist/utils/live-menu.d.ts +37 -9
  67. package/dist/utils/live-menu.js +510 -81
  68. package/dist/utils/plan-formatter.d.ts +0 -26
  69. package/dist/utils/plan-formatter.js +10 -44
  70. package/dist/utils/prompt-utils.d.ts +0 -18
  71. package/dist/utils/prompt-utils.js +0 -23
  72. package/dist/utils/proxy-bootstrap.d.ts +12 -0
  73. package/dist/utils/proxy-bootstrap.js +141 -0
  74. package/dist/utils/security.d.ts +2 -4
  75. package/dist/utils/security.js +36 -4
  76. package/dist/utils/table-renderer.d.ts +0 -4
  77. package/dist/utils/table-renderer.js +24 -8
  78. package/dist/utils/time-formatter.d.ts +0 -4
  79. package/dist/utils/time-formatter.js +0 -8
  80. package/dist/utils/token-preflight-ui.d.ts +28 -0
  81. package/dist/utils/token-preflight-ui.js +222 -0
  82. package/dist/utils/window-focus.d.ts +3 -0
  83. package/dist/utils/window-focus.js +106 -0
  84. package/package.json +12 -5
  85. package/dist/core/welcome-offer-service.d.ts +0 -52
  86. package/dist/core/welcome-offer-service.js +0 -369
@@ -1,9 +1,3 @@
1
- /**
2
- * Outlook / 微软邮箱独立浏览器实例自动登录与安全交接引擎
3
- * 支持 Windows / macOS / Linux / WSL 全平台多浏览器 (Chrome / Edge / Brave / Chromium)
4
- * 采用 Detached 独立进程常驻 + 原生零依赖 CDP 会话控制
5
- * 遇到辅助邮箱验证、两步验证、安全代码、密码错误时自动优雅停下交接给用户
6
- */
7
1
  import os from 'node:os';
8
2
  import path from 'node:path';
9
3
  import fs from 'node:fs';
@@ -14,10 +8,6 @@ import { CdpEngine } from './cdp-engine.js';
14
8
  import { findInstalledBrowsers, openBrowserUrl } from '../utils/browser-utils.js';
15
9
  import { copyToClipboard } from '../utils/clipboard.js';
16
10
  export class OutlookLauncher {
17
- /**
18
- * 生成专属隔离的 Outlook 浏览器 Profile 目录
19
- * 使得同一个邮箱的 Cookie 与登录态长期有效,下一次打开直接秒进收件箱
20
- */
21
11
  static getOutlookProfileDir(email) {
22
12
  const safeName = email
23
13
  .trim()
@@ -33,10 +23,6 @@ export class OutlookLauncher {
33
23
  }
34
24
  return profileDir;
35
25
  }
36
- /**
37
- * 构造微软官方带预填参数的直接登录 URL
38
- * 传入 login_hint 与 username 可跳过微软官网首页引导,直达该账号的密码验证页
39
- */
40
26
  static getOutlookLoginUrl(email) {
41
27
  const trimmed = email.trim();
42
28
  const domain = trimmed.includes('@') ? trimmed.split('@')[1].toLowerCase() : 'outlook.com';
@@ -56,15 +42,11 @@ export class OutlookLauncher {
56
42
  });
57
43
  return `https://login.live.com/login.srf?${params.toString()}`;
58
44
  }
59
- /**
60
- * 调起全新独立浏览器实例并执行 Outlook 自动化登录
61
- */
62
45
  static async openMailbox(options) {
63
46
  const email = options.email.trim();
64
47
  const password = (options.emailPassword || '').trim();
65
48
  const profileDir = this.getOutlookProfileDir(email);
66
49
  const loginUrl = this.getOutlookLoginUrl(email);
67
- // 1. 寻找本地可用的 Chromium 内核浏览器 (Chrome / Edge / Brave 等)
68
50
  const installed = findInstalledBrowsers();
69
51
  const chrome = installed.find((b) => b.name.includes('Chrome'));
70
52
  const edge = installed.find((b) => b.name.includes('Edge'));
@@ -73,7 +55,6 @@ export class OutlookLauncher {
73
55
  b.path.toLowerCase().endsWith('chrome.exe') ||
74
56
  b.path.toLowerCase().endsWith('msedge.exe'));
75
57
  const selectedBrowser = chrome || edge || brave || anyChromium || installed[0];
76
- // 若本地未检测到任何 Chromium 内核浏览器,执行优雅降级
77
58
  if (!selectedBrowser || !fs.existsSync(selectedBrowser.path)) {
78
59
  if (password) {
79
60
  await copyToClipboard(password);
@@ -87,7 +68,6 @@ export class OutlookLauncher {
87
68
  message: '未探测到系统 Chrome/Edge 浏览器,已降级在默认浏览器中打开,密码已复制到剪贴板。',
88
69
  };
89
70
  }
90
- // 2. 动态分配随机可用本地 CDP 端口
91
71
  let cdpPort;
92
72
  try {
93
73
  cdpPort = await CdpEngine.getFreePort();
@@ -95,7 +75,6 @@ export class OutlookLauncher {
95
75
  catch {
96
76
  cdpPort = 9222;
97
77
  }
98
- // 3. 构建 Detached 启动参数,确保浏览器即使 CLI 退出后依然独立常驻
99
78
  const launchArgs = [
100
79
  `--user-data-dir=${profileDir}`,
101
80
  `--remote-debugging-port=${cdpPort}`,
@@ -123,10 +102,8 @@ export class OutlookLauncher {
123
102
  message: `调起浏览器失败: ${err.message || String(err)}`,
124
103
  };
125
104
  }
126
- // 4. 等待 CDP 端口就绪并连接
127
105
  const target = await CdpEngine.waitForPageTarget(cdpPort, 15000);
128
106
  if (!target || !target.webSocketDebuggerUrl) {
129
- // 浏览器已打开,但 CDP 端口未在限定时间内响应(可能是已复用了旧实例)
130
107
  return {
131
108
  success: true,
132
109
  email,
@@ -140,7 +117,6 @@ export class OutlookLauncher {
140
117
  await cdp.connect(10000);
141
118
  await cdp.send('Page.enable');
142
119
  await cdp.send('Runtime.enable');
143
- // 若指定了同时打开 Cursor Dashboard,则预先注入 Token Cookie 并新建标签页直达后台
144
120
  if (options.openDashboard && options.cursorToken) {
145
121
  try {
146
122
  await cdp.send('Network.enable');
@@ -176,13 +152,12 @@ export class OutlookLauncher {
176
152
  message: '浏览器已成功调起,请在窗口中操作。',
177
153
  };
178
154
  }
179
- // 5. 自动化登录探测状态机
180
155
  let emailFilled = false;
181
156
  let passwordFilled = false;
182
157
  let passwordSubmittedAt = 0;
183
158
  let usePasswordAttempts = 0;
184
159
  let securityProofDetectedCount = 0;
185
- const deadline = Date.now() + 60000; // 最长观察 60 秒
160
+ const deadline = Date.now() + 60000;
186
161
  const securityBlockerMarkers = [
187
162
  'help us protect your account',
188
163
  'verify your identity',
@@ -220,7 +195,6 @@ export class OutlookLauncher {
220
195
  const curUrl = (await cdp.getCurrentUrl()).toLowerCase();
221
196
  const bodyText = await cdp.getBodyText();
222
197
  const bodyLower = bodyText.toLowerCase();
223
- // 状态 1: 已成功进入 Outlook 收件箱
224
198
  if (curUrl.includes('outlook.live.com/mail') ||
225
199
  curUrl.includes('outlook.office.com/mail') ||
226
200
  curUrl.includes('outlook.office365.com/mail')) {
@@ -244,7 +218,6 @@ export class OutlookLauncher {
244
218
  : 'Outlook 邮箱已成功登录并进入收件箱!',
245
219
  };
246
220
  }
247
- // 状态 2: 密码错误检测(仅在本次尝试提交过密码后才检测,防止旧错误文本误判)
248
221
  if (passwordFilled && passwordSubmittedAt > 0 && Date.now() - passwordSubmittedAt > 800) {
249
222
  if (wrongPasswordMarkers.some((m) => bodyLower.includes(m))) {
250
223
  await cdp.bringToFront();
@@ -258,14 +231,13 @@ export class OutlookLauncher {
258
231
  };
259
232
  }
260
233
  }
261
- // 状态 3: 检查密码输入框是否已出现 (优先处理密码输入,兼容 position: fixed 弹窗)
262
- const hasPwdInput = await cdp.evaluate(`(() => {
263
- const el = document.querySelector('input[name="passwd"], #i0118, #passwordEntry, input[type="password"]');
264
- if (!el || el.disabled) return false;
265
- const style = window.getComputedStyle(el);
266
- if (style.display === 'none' || style.visibility === 'hidden') return false;
267
- const r = el.getBoundingClientRect();
268
- return (r.width > 0 && r.height > 0) || el.getClientRects().length > 0;
234
+ const hasPwdInput = await cdp.evaluate(`(() => {
235
+ const el = document.querySelector('input[name="passwd"], #i0118, #passwordEntry, input[type="password"]');
236
+ if (!el || el.disabled) return false;
237
+ const style = window.getComputedStyle(el);
238
+ if (style.display === 'none' || style.visibility === 'hidden') return false;
239
+ const r = el.getBoundingClientRect();
240
+ return (r.width > 0 && r.height > 0) || el.getClientRects().length > 0;
269
241
  })()`);
270
242
  if (hasPwdInput && !passwordFilled) {
271
243
  if (password) {
@@ -282,7 +254,6 @@ export class OutlookLauncher {
282
254
  }
283
255
  }
284
256
  else {
285
- // 没有密码,置顶页面提示用户手工录入密码
286
257
  await cdp.bringToFront();
287
258
  cdp.disconnect();
288
259
  return {
@@ -294,15 +265,14 @@ export class OutlookLauncher {
294
265
  };
295
266
  }
296
267
  }
297
- // 状态 4: 处理登录账号输入 (过滤掉辅助邮箱验证输入框)
298
268
  if (!emailFilled) {
299
- const hasLoginInput = await cdp.evaluate(`(() => {
300
- const el = document.querySelector('input[name="loginfmt"], #i0116, input[type="email"]:not([id*="proof"])');
301
- if (!el || el.disabled) return false;
302
- const style = window.getComputedStyle(el);
303
- if (style.display === 'none' || style.visibility === 'hidden') return false;
304
- const r = el.getBoundingClientRect();
305
- return (r.width > 0 && r.height > 0) || el.getClientRects().length > 0;
269
+ const hasLoginInput = await cdp.evaluate(`(() => {
270
+ const el = document.querySelector('input[name="loginfmt"], #i0116, input[type="email"]:not([id*="proof"])');
271
+ if (!el || el.disabled) return false;
272
+ const style = window.getComputedStyle(el);
273
+ if (style.display === 'none' || style.visibility === 'hidden') return false;
274
+ const r = el.getBoundingClientRect();
275
+ return (r.width > 0 && r.height > 0) || el.getClientRects().length > 0;
306
276
  })()`);
307
277
  if (hasLoginInput) {
308
278
  console.log(chalk.cyan(' → 检测到邮箱账号输入框,正在填入账号...'));
@@ -316,18 +286,15 @@ export class OutlookLauncher {
316
286
  }
317
287
  }
318
288
  }
319
- // 状态 5: 智能切换为密码登录(优先寻找“使用密码”或“其他方式”,尽最大可能全自动完成登录)
320
289
  if (!passwordFilled && usePasswordAttempts < 2) {
321
290
  const switchRes = await this.tryClickUsePassword(cdp);
322
291
  if (switchRes.clicked) {
323
292
  usePasswordAttempts++;
324
293
  console.log(chalk.cyan(` → 检测到页面提供“${switchRes.type === 'password' ? '使用密码' : '其他登录方式'}”入口,已尝试自动点击切换 (尝试 ${usePasswordAttempts}/2)...`));
325
294
  await new Promise((r) => setTimeout(r, 1200));
326
- continue; // 继续下一轮状态机,优先检查密码输入框是否已出现 (状态 3)
295
+ continue;
327
296
  }
328
297
  }
329
- // 状态 6: 辅助邮箱 / 安全验证 / MFA 阻断检测
330
- // 关键逻辑:仅在【无法切换密码】(已尝试 2 次切换未果,或页面无任何密码通道)时,才置顶交接给用户
331
298
  const isSecurityProofUrl = curUrl.includes('/proofs/add') || curUrl.includes('/proofs/manage') || curUrl.includes('/proofs') || curUrl.includes('login.live.com/ppsecure');
332
299
  const hasSecurityMarker = securityBlockerMarkers.some((m) => bodyLower.includes(m));
333
300
  const hasMaskedEmail = /[a-z0-9._%+-]*\*{3,}@[a-z0-9.-]+/i.test(bodyText);
@@ -345,10 +312,9 @@ export class OutlookLauncher {
345
312
  };
346
313
  }
347
314
  }
348
- // 状态 7: 保持登录状态 (KMSI - Keep Me Signed In) 页面 -> 自动点击“是”
349
- const isKmsi = await cdp.evaluate(`(() => {
350
- const text = (document.body && document.body.innerText || '').toLowerCase();
351
- return text.includes('保持登录状态') || text.includes('stay signed in') || text.includes('stay sign in');
315
+ const isKmsi = await cdp.evaluate(`(() => {
316
+ const text = (document.body && document.body.innerText || '').toLowerCase();
317
+ return text.includes('保持登录状态') || text.includes('stay signed in') || text.includes('stay sign in');
352
318
  })()`);
353
319
  if (isKmsi) {
354
320
  console.log(chalk.cyan(' → 检测到“保持登录状态”引导,正在自动选择“是”...'));
@@ -358,7 +324,6 @@ export class OutlookLauncher {
358
324
  continue;
359
325
  }
360
326
  }
361
- // 状态 7.5: 微软服务协议与条款更新 (TOU - Terms of Use) 确认页 -> 自动点击“下一步” / “接受”
362
327
  const isTou = curUrl.includes('/tou/') ||
363
328
  curUrl.includes('/tou') ||
364
329
  bodyLower.includes('更新条款') ||
@@ -373,7 +338,6 @@ export class OutlookLauncher {
373
338
  continue;
374
339
  }
375
340
  }
376
- // 状态 8: 官网营销页误跳转处理
377
341
  if (curUrl.includes('microsoft.com') && curUrl.includes('outlook')) {
378
342
  const clicked = await cdp.click("a[href*='login.live.com'], a[href*='outlook.live.com']");
379
343
  if (clicked) {
@@ -383,7 +347,6 @@ export class OutlookLauncher {
383
347
  }
384
348
  await new Promise((r) => setTimeout(r, 700));
385
349
  }
386
- // 超时返回
387
350
  await cdp.bringToFront();
388
351
  cdp.disconnect();
389
352
  return {
@@ -407,127 +370,122 @@ export class OutlookLauncher {
407
370
  };
408
371
  }
409
372
  }
410
- /**
411
- * 优先尝试点击页面上的“使用密码 / 改用密码 / 密码登录 / Use your password / 使用其他方式”入口
412
- * 采用原生 CDP 屏幕坐标模拟真实用户点击,彻底击穿各类前端框架 (Fluent UI / React) 遮挡与事件委托限制
413
- */
414
373
  static async tryClickUsePassword(cdp) {
415
374
  try {
416
- const res = await cdp.evaluate(`(() => {
417
- // 1. 若存在半透明全屏加载蒙层,穿透它
418
- const loaders = document.querySelectorAll('div[aria-label*="加载"], div[aria-label*="loading"]');
419
- loaders.forEach(l => {
420
- if (l instanceof HTMLElement) l.style.pointerEvents = 'none';
421
- });
422
-
423
- const isVisible = (el) => {
424
- if (!el || el.disabled) return false;
425
- const rect = el.getBoundingClientRect();
426
- const style = window.getComputedStyle(el);
427
- return rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none';
428
- };
429
-
430
- const doClick = (el) => {
431
- try {
432
- el.scrollIntoView({ block: 'center' });
433
- el.focus();
434
- el.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, cancelable: true, view: window }));
435
- el.dispatchEvent(new MouseEvent('mouseup', { bubbles: true, cancelable: true, view: window }));
436
- el.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
437
- if (typeof el.click === 'function') el.click();
438
- } catch {}
439
- };
440
-
441
- // 优先级 1: 查找显式“使用密码 / 密码登录”入口 (官方专有 ID 以及包含密码/password 的凭据瓷贴与按钮)
442
- const pwdSelectors = [
443
- '#idA_PWD_SwitchToPassword',
444
- '#idA_IL_SwitchToPassword',
445
- 'a[role="button"][id*="SwitchToPassword"]',
446
- 'button[id*="SwitchToPassword"]',
447
- 'a[id*="SwitchToPassword"]',
448
- 'a[id*="switchToPassword"]',
449
- '#credentialList [role="button"]',
450
- '.tile',
451
- ];
452
-
453
- for (const sel of pwdSelectors) {
454
- const matches = Array.from(document.querySelectorAll(sel));
455
- for (const el of matches) {
456
- if (isVisible(el)) {
457
- const text = (el.innerText || el.textContent || el.getAttribute('aria-label') || '').trim().toLowerCase();
458
- if (text.includes('密码') || text.includes('password') || sel.includes('Password')) {
459
- doClick(el);
460
- const r = el.getBoundingClientRect();
461
- return { point: { x: r.left + r.width / 2, y: r.top + r.height / 2 }, type: 'password' };
462
- }
463
- }
464
- }
465
- }
466
-
467
- // 优先级 1.5: 页面内包含“密码”的各类交互链接 (a, button, span.fui-Link 等)
468
- const pwdMarkers = [
469
- '使用密码',
470
- '改用密码',
471
- '密码登录',
472
- '输入你的密码',
473
- '输入密码',
474
- '使用你的密码',
475
- 'use your password',
476
- 'use password',
477
- 'use your password instead',
478
- 'sign in with password',
479
- 'sign in with your password',
480
- ];
481
-
482
- const interactiveNodes = Array.from(document.querySelectorAll('[role="button"], button, a, span.fui-Link, div[role="button"]'));
483
- for (const el of interactiveNodes) {
484
- if (!isVisible(el)) continue;
485
- const text = (el.innerText || el.textContent || el.getAttribute('aria-label') || '').trim().replace(/\\s+/g, ' ').toLowerCase();
486
- for (const m of pwdMarkers) {
487
- if (text === m || (text.includes(m) && text.length <= m.length + 10)) {
488
- doClick(el);
489
- const r = el.getBoundingClientRect();
490
- return { point: { x: r.left + r.width / 2, y: r.top + r.height / 2 }, type: 'password' };
491
- }
492
- }
493
- }
494
-
495
- // 优先级 2: 若未找到直接的密码入口,点击“其他登录方式 / Sign in another way”展开凭据选择列表
496
- const anotherWaySelectors = ['#signInAnotherWay', '#idA_PWD_SwitchToCredPicker'];
497
- for (const sel of anotherWaySelectors) {
498
- const el = document.querySelector(sel);
499
- if (isVisible(el)) {
500
- doClick(el);
501
- const r = el.getBoundingClientRect();
502
- return { point: { x: r.left + r.width / 2, y: r.top + r.height / 2 }, type: 'another_way' };
503
- }
504
- }
505
-
506
- const anotherMarkers = [
507
- '使用另一种方式登录',
508
- '使用其他方式登录',
509
- '其他登录方式',
510
- '其他方式',
511
- '通过其他方式登录',
512
- 'sign in another way',
513
- 'other ways to sign in',
514
- ];
515
- for (const el of interactiveNodes) {
516
- if (!isVisible(el)) continue;
517
- const text = (el.innerText || el.textContent || el.getAttribute('aria-label') || '').trim().replace(/\\s+/g, ' ').toLowerCase();
518
- for (const m of anotherMarkers) {
519
- if (text === m || (text.includes(m) && text.length <= m.length + 10)) {
520
- doClick(el);
521
- const r = el.getBoundingClientRect();
522
- return { point: { x: r.left + r.width / 2, y: r.top + r.height / 2 }, type: 'another_way' };
523
- }
524
- }
525
- }
526
-
527
- return null;
375
+ const res = await cdp.evaluate(`(() => {
376
+ // 1. 若存在半透明全屏加载蒙层,穿透它
377
+ const loaders = document.querySelectorAll('div[aria-label*="加载"], div[aria-label*="loading"]');
378
+ loaders.forEach(l => {
379
+ if (l instanceof HTMLElement) l.style.pointerEvents = 'none';
380
+ });
381
+
382
+ const isVisible = (el) => {
383
+ if (!el || el.disabled) return false;
384
+ const rect = el.getBoundingClientRect();
385
+ const style = window.getComputedStyle(el);
386
+ return rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none';
387
+ };
388
+
389
+ const doClick = (el) => {
390
+ try {
391
+ el.scrollIntoView({ block: 'center' });
392
+ el.focus();
393
+ el.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, cancelable: true, view: window }));
394
+ el.dispatchEvent(new MouseEvent('mouseup', { bubbles: true, cancelable: true, view: window }));
395
+ el.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
396
+ if (typeof el.click === 'function') el.click();
397
+ } catch {}
398
+ };
399
+
400
+ // 优先级 1: 查找显式“使用密码 / 密码登录”入口 (官方专有 ID 以及包含密码/password 的凭据瓷贴与按钮)
401
+ const pwdSelectors = [
402
+ '#idA_PWD_SwitchToPassword',
403
+ '#idA_IL_SwitchToPassword',
404
+ 'a[role="button"][id*="SwitchToPassword"]',
405
+ 'button[id*="SwitchToPassword"]',
406
+ 'a[id*="SwitchToPassword"]',
407
+ 'a[id*="switchToPassword"]',
408
+ '#credentialList [role="button"]',
409
+ '.tile',
410
+ ];
411
+
412
+ for (const sel of pwdSelectors) {
413
+ const matches = Array.from(document.querySelectorAll(sel));
414
+ for (const el of matches) {
415
+ if (isVisible(el)) {
416
+ const text = (el.innerText || el.textContent || el.getAttribute('aria-label') || '').trim().toLowerCase();
417
+ if (text.includes('密码') || text.includes('password') || sel.includes('Password')) {
418
+ doClick(el);
419
+ const r = el.getBoundingClientRect();
420
+ return { point: { x: r.left + r.width / 2, y: r.top + r.height / 2 }, type: 'password' };
421
+ }
422
+ }
423
+ }
424
+ }
425
+
426
+ // 优先级 1.5: 页面内包含“密码”的各类交互链接 (a, button, span.fui-Link 等)
427
+ const pwdMarkers = [
428
+ '使用密码',
429
+ '改用密码',
430
+ '密码登录',
431
+ '输入你的密码',
432
+ '输入密码',
433
+ '使用你的密码',
434
+ 'use your password',
435
+ 'use password',
436
+ 'use your password instead',
437
+ 'sign in with password',
438
+ 'sign in with your password',
439
+ ];
440
+
441
+ const interactiveNodes = Array.from(document.querySelectorAll('[role="button"], button, a, span.fui-Link, div[role="button"]'));
442
+ for (const el of interactiveNodes) {
443
+ if (!isVisible(el)) continue;
444
+ const text = (el.innerText || el.textContent || el.getAttribute('aria-label') || '').trim().replace(/\\s+/g, ' ').toLowerCase();
445
+ for (const m of pwdMarkers) {
446
+ if (text === m || (text.includes(m) && text.length <= m.length + 10)) {
447
+ doClick(el);
448
+ const r = el.getBoundingClientRect();
449
+ return { point: { x: r.left + r.width / 2, y: r.top + r.height / 2 }, type: 'password' };
450
+ }
451
+ }
452
+ }
453
+
454
+ // 优先级 2: 若未找到直接的密码入口,点击“其他登录方式 / Sign in another way”展开凭据选择列表
455
+ const anotherWaySelectors = ['#signInAnotherWay', '#idA_PWD_SwitchToCredPicker'];
456
+ for (const sel of anotherWaySelectors) {
457
+ const el = document.querySelector(sel);
458
+ if (isVisible(el)) {
459
+ doClick(el);
460
+ const r = el.getBoundingClientRect();
461
+ return { point: { x: r.left + r.width / 2, y: r.top + r.height / 2 }, type: 'another_way' };
462
+ }
463
+ }
464
+
465
+ const anotherMarkers = [
466
+ '使用另一种方式登录',
467
+ '使用其他方式登录',
468
+ '其他登录方式',
469
+ '其他方式',
470
+ '通过其他方式登录',
471
+ 'sign in another way',
472
+ 'other ways to sign in',
473
+ ];
474
+ for (const el of interactiveNodes) {
475
+ if (!isVisible(el)) continue;
476
+ const text = (el.innerText || el.textContent || el.getAttribute('aria-label') || '').trim().replace(/\\s+/g, ' ').toLowerCase();
477
+ for (const m of anotherMarkers) {
478
+ if (text === m || (text.includes(m) && text.length <= m.length + 10)) {
479
+ doClick(el);
480
+ const r = el.getBoundingClientRect();
481
+ return { point: { x: r.left + r.width / 2, y: r.top + r.height / 2 }, type: 'another_way' };
482
+ }
483
+ }
484
+ }
485
+
486
+ return null;
528
487
  })()`);
529
488
  if (res && res.point) {
530
- // 同步派发真实 CDP 鼠标物理点击
531
489
  await cdp.clickPoint(res.point.x, res.point.y);
532
490
  return { clicked: true, type: res.type };
533
491
  }
@@ -537,9 +495,6 @@ export class OutlookLauncher {
537
495
  return { clicked: false };
538
496
  }
539
497
  }
540
- /**
541
- * 格式化打印操作结果卡片
542
- */
543
498
  static printResultCard(result) {
544
499
  if (result.status === 'logged_in') {
545
500
  console.log(boxen([
@@ -597,38 +552,29 @@ export class OutlookLauncher {
597
552
  }));
598
553
  }
599
554
  }
600
- /**
601
- * 在已到达 Outlook 收件箱页面时,利用 CDP 实时嗅探 Cursor / WorkOS 最新的 6 位数字验证码
602
- * 增强特性:
603
- * 1. 自动识别并无感切换【重点 / 其他 (Other)】选项卡,彻底击破微软邮件分类盲区
604
- * 2. 毫秒级匹配邮件列表项预览摘要 (如 "Your one-time code is 298372"),无需等待正文复杂渲染
605
- * 3. 兼容卡片大字号、iframe 嵌套及多语系正则提取,捕获即完成
606
- */
607
555
  static async sniffVerificationCode(cdp, timeoutMs = 35000) {
608
556
  const end = Date.now() + timeoutMs;
609
557
  console.log(chalk.cyan(' → 浏览器已成功进入收件箱,正在全自动嗅探提取 6 位验证码...'));
610
558
  let attempt = 0;
611
559
  let hasSwitchedToOther = false;
612
- // 初始等待 1.2 秒让 Outlook 单页应用渲染出邮件主框架
613
560
  await new Promise((r) => setTimeout(r, 1200));
614
561
  while (Date.now() < end) {
615
562
  attempt++;
616
563
  try {
617
- // 关键逻辑:若在当前栏扫描 2 次未果,主动检测并点击【其他】(Other) 分类选项卡!
618
564
  if (attempt >= 2 && !hasSwitchedToOther) {
619
- const switched = await cdp.evaluate(`(() => {
620
- const tabs = Array.from(document.querySelectorAll('button, div[role="tab"], span[role="tab"], a, [aria-label*="其他"], [aria-label*="Other"]'));
621
- for (const t of tabs) {
622
- const text = ((t.innerText || t.textContent || t.getAttribute('aria-label') || '').trim());
623
- if (text === '其他' || text === 'Other' || text.startsWith('其他') || text.startsWith('Other')) {
624
- const isSelected = t.getAttribute('aria-selected') === 'true' || t.classList.contains('is-selected') || t.classList.contains('active');
625
- if (!isSelected) {
626
- (t as HTMLElement).click();
627
- return true;
628
- }
629
- }
630
- }
631
- return false;
565
+ const switched = await cdp.evaluate(`(() => {
566
+ const tabs = Array.from(document.querySelectorAll('button, div[role="tab"], span[role="tab"], a, [aria-label*="其他"], [aria-label*="Other"]'));
567
+ for (const t of tabs) {
568
+ const text = ((t.innerText || t.textContent || t.getAttribute('aria-label') || '').trim());
569
+ if (text === '其他' || text === 'Other' || text.startsWith('其他') || text.startsWith('Other')) {
570
+ const isSelected = t.getAttribute('aria-selected') === 'true' || t.classList.contains('is-selected') || t.classList.contains('active');
571
+ if (!isSelected) {
572
+ (t as HTMLElement).click();
573
+ return true;
574
+ }
575
+ }
576
+ }
577
+ return false;
632
578
  })()`);
633
579
  if (switched) {
634
580
  hasSwitchedToOther = true;
@@ -636,56 +582,56 @@ export class OutlookLauncher {
636
582
  await new Promise((r) => setTimeout(r, 1000));
637
583
  }
638
584
  }
639
- const found = await cdp.evaluate(`(() => {
640
- // 1. 全文/阅读窗格直接正则提取 (包括列表摘要及展开后的卡片正文)
641
- const fullText = (document.body ? (document.body.innerText + '\\n' + document.body.textContent) : '');
642
-
643
- // 1.1 精准匹配 Cursor 官方模板规则 (例如: "Your one-time code is 298372" / "Your code is 298372")
644
- const directMatch = fullText.match(/(?:one-time code is|code is|your code is|one-time code|验证码为|验证码是|验证代码为|验证代码是)[^\\d]{0,25}(\\d{6})\\b/i);
645
- if (directMatch && directMatch[1]) {
646
- return directMatch[1];
647
- }
648
-
649
- // 2. 检查邮件列表项 (左侧邮件列表)
650
- const mailItems = Array.from(document.querySelectorAll('div[role="option"], div[data-convid], div[aria-label*="Cursor"], div[aria-label*="cursor"], div[draggable="true"]'));
651
- for (const item of mailItems) {
652
- const label = ((item.getAttribute('aria-label') || '') + ' ' + (item.innerText || '')).trim();
653
- if (/cursor|workos|sign in to cursor/i.test(label)) {
654
- // 邮件列表项的摘要预览文本中通常直接包含: "Your one-time code is 298372. Th..."
655
- const m = label.match(/(?:one-time code is|code is|code\\s*|验证码[::\\s]*|\\b)(\\d{6})\\b/i);
656
- if (m && m[1]) {
657
- return m[1];
658
- }
659
- // 尝试点选该邮件项展开阅读窗格
660
- try { (item as HTMLElement).click(); } catch {}
661
- }
662
- }
663
-
664
- // 3. 检查右侧阅读窗格或大号突出数字 (卡片大字号)
665
- const readingPanes = Array.from(document.querySelectorAll('div[role="region"], div[data-app-section="ConversationReadingPane"], div[aria-label*="邮件内容"], div[aria-label*="Reading"], #Item\\\\.MessageUniqueBody, div[aria-label*="Cursor"]'));
666
- for (const rp of readingPanes) {
667
- const text = (rp as HTMLElement).innerText || '';
668
- if (/cursor|one-time code|sign in/i.test(text)) {
669
- const m = text.match(/(?:code is|one-time code|验证码)[^\\d]{0,40}(\\d{6})\\b/i) || text.match(/\\b(\\d{6})\\b/);
670
- if (m && m[1]) return m[1];
671
- }
672
- }
673
-
674
- // 4. 检查可能嵌套在 iframe 里的邮件正文 (部分 Outlook 版本的正文置于隔离 iframe)
675
- const iframes = Array.from(document.querySelectorAll('iframe'));
676
- for (const f of iframes) {
677
- try {
678
- const doc = f.contentDocument || f.contentWindow?.document;
679
- if (doc && doc.body) {
680
- const iText = doc.body.innerText || doc.body.textContent || '';
681
- const m = iText.match(/(?:one-time code is|code is|one-time code|验证码)[^\\d]{0,25}(\\d{6})\\b/i) ||
682
- (iText.includes('Cursor') ? iText.match(/\\b(\\d{6})\\b/) : null);
683
- if (m && m[1]) return m[1];
684
- }
685
- } catch {}
686
- }
687
-
688
- return null;
585
+ const found = await cdp.evaluate(`(() => {
586
+ // 1. 全文/阅读窗格直接正则提取 (包括列表摘要及展开后的卡片正文)
587
+ const fullText = (document.body ? (document.body.innerText + '\\n' + document.body.textContent) : '');
588
+
589
+ // 1.1 精准匹配 Cursor 官方模板规则 (例如: "Your one-time code is 298372" / "Your code is 298372")
590
+ const directMatch = fullText.match(/(?:one-time code is|code is|your code is|one-time code|验证码为|验证码是|验证代码为|验证代码是)[^\\d]{0,25}(\\d{6})\\b/i);
591
+ if (directMatch && directMatch[1]) {
592
+ return directMatch[1];
593
+ }
594
+
595
+ // 2. 检查邮件列表项 (左侧邮件列表)
596
+ const mailItems = Array.from(document.querySelectorAll('div[role="option"], div[data-convid], div[aria-label*="Cursor"], div[aria-label*="cursor"], div[draggable="true"]'));
597
+ for (const item of mailItems) {
598
+ const label = ((item.getAttribute('aria-label') || '') + ' ' + (item.innerText || '')).trim();
599
+ if (/cursor|workos|sign in to cursor/i.test(label)) {
600
+ // 邮件列表项的摘要预览文本中通常直接包含: "Your one-time code is 298372. Th..."
601
+ const m = label.match(/(?:one-time code is|code is|code\\s*|验证码[::\\s]*|\\b)(\\d{6})\\b/i);
602
+ if (m && m[1]) {
603
+ return m[1];
604
+ }
605
+ // 尝试点选该邮件项展开阅读窗格
606
+ try { (item as HTMLElement).click(); } catch {}
607
+ }
608
+ }
609
+
610
+ // 3. 检查右侧阅读窗格或大号突出数字 (卡片大字号)
611
+ const readingPanes = Array.from(document.querySelectorAll('div[role="region"], div[data-app-section="ConversationReadingPane"], div[aria-label*="邮件内容"], div[aria-label*="Reading"], #Item\\\\.MessageUniqueBody, div[aria-label*="Cursor"]'));
612
+ for (const rp of readingPanes) {
613
+ const text = (rp as HTMLElement).innerText || '';
614
+ if (/cursor|one-time code|sign in/i.test(text)) {
615
+ const m = text.match(/(?:code is|one-time code|验证码)[^\\d]{0,40}(\\d{6})\\b/i) || text.match(/\\b(\\d{6})\\b/);
616
+ if (m && m[1]) return m[1];
617
+ }
618
+ }
619
+
620
+ // 4. 检查可能嵌套在 iframe 里的邮件正文 (部分 Outlook 版本的正文置于隔离 iframe)
621
+ const iframes = Array.from(document.querySelectorAll('iframe'));
622
+ for (const f of iframes) {
623
+ try {
624
+ const doc = f.contentDocument || f.contentWindow?.document;
625
+ if (doc && doc.body) {
626
+ const iText = doc.body.innerText || doc.body.textContent || '';
627
+ const m = iText.match(/(?:one-time code is|code is|one-time code|验证码)[^\\d]{0,25}(\\d{6})\\b/i) ||
628
+ (iText.includes('Cursor') ? iText.match(/\\b(\\d{6})\\b/) : null);
629
+ if (m && m[1]) return m[1];
630
+ }
631
+ } catch {}
632
+ }
633
+
634
+ return null;
689
635
  })()`);
690
636
  if (found && /^\d{6}$/.test(found)) {
691
637
  return found;