tt-help-cli-ycl 1.3.48 → 1.3.50

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 (64) hide show
  1. package/README.md +33 -33
  2. package/cli.js +9 -9
  3. package/package.json +52 -52
  4. package/scripts/run-explore copy.bat +101 -101
  5. package/scripts/run-explore.bat +134 -134
  6. package/scripts/run-explore.ps1 +159 -159
  7. package/scripts/run-explore.sh +121 -121
  8. package/scripts/test-captcha-lib.mjs +68 -0
  9. package/scripts/test-captcha.mjs +81 -0
  10. package/scripts/test-incognito-lib.mjs +36 -0
  11. package/scripts/test-login-state.mjs +128 -0
  12. package/scripts/test-safe-click.mjs +45 -0
  13. package/scripts/test-watch-db-smoke.mjs +246 -0
  14. package/src/cli/attach.js +331 -331
  15. package/src/cli/auto.js +265 -265
  16. package/src/cli/comments.js +620 -620
  17. package/src/cli/config.js +170 -170
  18. package/src/cli/db-import.js +51 -51
  19. package/src/cli/explore.js +555 -555
  20. package/src/cli/open.js +109 -111
  21. package/src/cli/progress.js +111 -111
  22. package/src/cli/refresh.js +288 -288
  23. package/src/cli/scrape.js +47 -47
  24. package/src/cli/utils.js +18 -18
  25. package/src/cli/videos.js +41 -41
  26. package/src/cli/videostats.js +196 -196
  27. package/src/cli/watch.js +30 -30
  28. package/src/lib/api-interceptor.js +161 -161
  29. package/src/lib/args.js +809 -809
  30. package/src/lib/browser/anti-detect.js +23 -23
  31. package/src/lib/browser/cdp.js +261 -261
  32. package/src/lib/browser/health-checker.js +114 -114
  33. package/src/lib/browser/launch.js +43 -43
  34. package/src/lib/browser/page.js +184 -184
  35. package/src/lib/constants.js +297 -297
  36. package/src/lib/delay.js +54 -54
  37. package/src/lib/explore-fetch.js +118 -118
  38. package/src/lib/fetcher.js +45 -45
  39. package/src/lib/filter.js +66 -66
  40. package/src/lib/io.js +54 -54
  41. package/src/lib/output.js +80 -80
  42. package/src/lib/page-error-detector.js +109 -109
  43. package/src/lib/parse-ssr.mjs +69 -69
  44. package/src/lib/parser.js +47 -47
  45. package/src/lib/retry.js +45 -45
  46. package/src/lib/scrape.js +90 -90
  47. package/src/lib/target-locations.js +61 -61
  48. package/src/lib/tiktok-scraper.mjs +98 -61
  49. package/src/lib/url.js +52 -52
  50. package/src/main.js +73 -73
  51. package/src/npm-main.js +70 -70
  52. package/src/results/user-videos-bar.lar.lar.moeta.json +37 -0
  53. package/src/scraper/auto-core.js +203 -203
  54. package/src/scraper/core.js +255 -255
  55. package/src/scraper/explore-core.js +208 -208
  56. package/src/scraper/modules/captcha-handler.js +114 -114
  57. package/src/scraper/modules/follow-extractor.js +250 -250
  58. package/src/scraper/modules/guess-extractor.js +51 -51
  59. package/src/scraper/modules/page-helpers.js +48 -48
  60. package/src/scraper/refresh-core.js +213 -213
  61. package/src/videos/core.js +143 -143
  62. package/src/watch/data-store.js +2980 -2980
  63. package/src/watch/public/index.html +2355 -2355
  64. package/src/watch/server.js +727 -727
@@ -1,23 +1,23 @@
1
- export function getAntiDetectScript() {
2
- return () => {
3
- Object.defineProperty(navigator, 'webdriver', { get: () => false });
4
-
5
- if (!window.chrome) {
6
- window.chrome = { runtime: {} };
7
- }
8
-
9
- const originalQuery = window.navigator.permissions.query;
10
- window.navigator.permissions.query = (params) =>
11
- params.name === 'notifications'
12
- ? Promise.resolve({ state: Notification.permission })
13
- : originalQuery(params);
14
-
15
- Object.defineProperty(navigator, 'languages', {
16
- get: () => ['en-US', 'en'],
17
- });
18
-
19
- Object.defineProperty(navigator, 'plugins', {
20
- get: () => [1, 2, 3, 4, 5],
21
- });
22
- };
23
- }
1
+ export function getAntiDetectScript() {
2
+ return () => {
3
+ Object.defineProperty(navigator, 'webdriver', { get: () => false });
4
+
5
+ if (!window.chrome) {
6
+ window.chrome = { runtime: {} };
7
+ }
8
+
9
+ const originalQuery = window.navigator.permissions.query;
10
+ window.navigator.permissions.query = (params) =>
11
+ params.name === 'notifications'
12
+ ? Promise.resolve({ state: Notification.permission })
13
+ : originalQuery(params);
14
+
15
+ Object.defineProperty(navigator, 'languages', {
16
+ get: () => ['en-US', 'en'],
17
+ });
18
+
19
+ Object.defineProperty(navigator, 'plugins', {
20
+ get: () => [1, 2, 3, 4, 5],
21
+ });
22
+ };
23
+ }
@@ -1,261 +1,261 @@
1
- import { exec } from "child_process";
2
- import { execSync } from "child_process";
3
- import fs from "fs";
4
- import http from "http";
5
- import os from "os";
6
- import path from "path";
7
- import { chromium } from "playwright";
8
-
9
- const DEFAULT_CDP_PORT = 9222;
10
- const DEFAULT_USER_DATA_DIR = path.join(
11
- os.homedir(),
12
- "Library",
13
- "Application Support",
14
- "Microsoft Edge For Testing",
15
- );
16
-
17
- function getEdgePath() {
18
- const platform = os.platform();
19
- if (platform === "darwin") return '"Microsoft Edge"';
20
- if (platform === "win32") return "msedge.exe";
21
- return "msedge";
22
- }
23
-
24
- function isEdgeRunning() {
25
- return new Promise((resolve) => {
26
- const platform = os.platform();
27
- let command;
28
- if (platform === "darwin") {
29
- command =
30
- 'ps aux | grep -q "[M]icrosoft Edge.app/Contents/MacOS/Microsoft Edge" 2>/dev/null';
31
- } else if (platform === "win32") {
32
- command =
33
- 'tasklist /FI "IMAGENAME eq msedge.exe" 2>nul | findstr /I msedge';
34
- } else {
35
- command = "pgrep -f msedge > /dev/null 2>&1";
36
- }
37
- exec(command, (err) => resolve(!err));
38
- });
39
- }
40
-
41
- function checkCDPPort(port) {
42
- return new Promise((resolve) => {
43
- const req = http.get(`http://127.0.0.1:${port}/json`, (res) => {
44
- res.on("data", () => {});
45
- res.on("end", () => resolve(res.statusCode === 200));
46
- });
47
- req.on("error", () => resolve(false));
48
- req.setTimeout(3000, () => {
49
- resolve(false);
50
- req.destroy();
51
- });
52
- });
53
- }
54
-
55
- function checkEdgeArgs() {
56
- return new Promise((resolve) => {
57
- const platform = os.platform();
58
- let command;
59
- if (platform === "darwin") {
60
- command =
61
- 'ps aux | grep "[M]icrosoft Edge" | grep -v "Helper\\|crashpad" | grep "user-data-dir"';
62
- } else if (platform === "win32") {
63
- command =
64
- 'wmic process where "name like \\"%msedge%\\"" get commandline | findstr "user-data-dir"';
65
- } else {
66
- command =
67
- 'ps aux | grep "[m]sedge" | grep -v "Helper\\|crashpad" | grep "user-data-dir"';
68
- }
69
- exec(command, (err, stdout) => resolve(!err && stdout.trim().length > 0));
70
- });
71
- }
72
-
73
- function killEdgeProcesses(targetDir) {
74
- return new Promise((resolve) => {
75
- const platform = os.platform();
76
- let command;
77
- if (platform === "darwin") {
78
- if (targetDir) {
79
- let pids = "";
80
- try {
81
- // ps aux 输出中 --user-data-dir= 后面没有引号
82
- // 用路径精确匹配,结尾加空格或行尾避免子串误杀
83
- const escapedDir = targetDir.replace(
84
- /[-\/\\^$*+?.()|[\]{}]/g,
85
- "\\$&",
86
- );
87
- pids = execSync(
88
- `ps aux | grep "[M]icrosoft Edge" | grep -v "Helper\\|crashpad" | grep -E -- '--user-data-dir=${escapedDir}($|[^A-Za-z0-9_])' | awk '{print $2}'`,
89
- )
90
- .toString()
91
- .trim();
92
- } catch (e) {
93
- pids = "";
94
- }
95
- if (pids) {
96
- command = `kill -9 ${pids} 2>/dev/null; rm -f ~/Library/Caches/Microsoft\\ Edge/Singleton*; true`;
97
- } else {
98
- command = "true";
99
- }
100
- } else {
101
- command =
102
- 'killall -9 "Microsoft Edge" 2>/dev/null; rm -f ~/Library/Caches/Microsoft\\ Edge/Singleton*; true';
103
- }
104
- } else if (platform === "win32") {
105
- if (targetDir) {
106
- try {
107
- const ps1Path = path.join(os.tmpdir(), `kill-edge-${Date.now()}.ps1`);
108
- const escapedDir = targetDir.replace(/'/g, "''");
109
- const ps1Content =
110
- `$procs = Get-CimInstance Win32_Process -Filter "Name='msedge.exe'" 2>$null; ` +
111
- `$count = 0; ` +
112
- `foreach ($p in $procs) { ` +
113
- ` if ($p.CommandLine -and $p.CommandLine -like "*${escapedDir}*") { ` +
114
- ` Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue; ` +
115
- ` $count++ ` +
116
- ` } ` +
117
- `}; ` +
118
- `Get-ChildItem "$env:LOCALAPPDATA\\Microsoft\\Edge\\Singleton*" -ErrorAction SilentlyContinue | Remove-Item -Force; ` +
119
- `Write-Output $count`;
120
- fs.writeFileSync(ps1Path, ps1Content);
121
- const result = execSync(
122
- `powershell.exe -ExecutionPolicy Bypass -File "${ps1Path}"`,
123
- )
124
- .toString()
125
- .trim();
126
- fs.unlinkSync(ps1Path);
127
- command = "exit 0";
128
- } catch {
129
- command = "taskkill /F /IM msedge.exe 2>nul || exit 0";
130
- }
131
- } else {
132
- command = "taskkill /F /IM msedge.exe 2>nul || exit 0";
133
- }
134
- } else {
135
- if (targetDir) {
136
- const escapedDir = targetDir.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
137
- command = `ps aux | grep msedge | grep -v Helper | grep -E -- '--user-data-dir=${escapedDir}($|[^A-Za-z0-9_])' | awk '{print $2}' | xargs -r kill -9 2>/dev/null; true`;
138
- } else {
139
- command = "pkill -9 -f msedge 2>/dev/null; true";
140
- }
141
- }
142
- exec(command, () => resolve());
143
- });
144
- }
145
-
146
- function launchEdgeWithCDP(port, userDataDir) {
147
- return new Promise((resolve, reject) => {
148
- const platform = os.platform();
149
- const edgePath = getEdgePath();
150
- let command;
151
-
152
- const extraArgs = [
153
- `--remote-debugging-port=${port}`,
154
- `--user-data-dir="${userDataDir}"`,
155
- "--disable-blink-features=AutomationControlled",
156
- "--no-first-run",
157
- "--no-default-browser-check",
158
- "--password-store=basic",
159
- "--disable-background-mode",
160
- "--disable-component-update",
161
- "--disable-crash-reporter",
162
- "--disable-breakpad",
163
- "--disable-background-networking",
164
- "--disable-sync",
165
- ].join(" ");
166
-
167
- if (platform === "darwin") {
168
- command = `open -a ${edgePath} --new --args ${extraArgs}`;
169
- } else if (platform === "win32") {
170
- command = `start msedge ${extraArgs}`;
171
- } else {
172
- command = `msedge ${extraArgs} &`;
173
- }
174
-
175
- exec(command, (err) => {
176
- if (err) reject(new Error(`启动 Edge 浏览器失败: ${err.message}`));
177
- else resolve();
178
- });
179
- });
180
- }
181
-
182
- async function waitForCDP(port, timeout = 30000, interval = 1000) {
183
- const start = Date.now();
184
- while (Date.now() - start < timeout) {
185
- const ready = await checkCDPPort(port);
186
- if (ready) return true;
187
- await new Promise((r) => setTimeout(r, interval));
188
- }
189
- return false;
190
- }
191
-
192
- export { killEdgeProcesses };
193
-
194
- export async function ensureBrowserReady(options = {}) {
195
- const port = options.port || DEFAULT_CDP_PORT;
196
- const userDataDir = options.userDataDir || DEFAULT_USER_DATA_DIR;
197
- const isCustom = port !== DEFAULT_CDP_PORT || !!options.userDataDir;
198
-
199
- const isReady = await checkCDPPort(port);
200
- let needLaunch = !isReady;
201
-
202
- if (!needLaunch) {
203
- if (!isCustom) {
204
- const edgeArgsValid = await checkEdgeArgs();
205
- if (!edgeArgsValid) {
206
- console.error(`Edge 已运行但启动参数不完整,正在重启端口 ${port}...`);
207
- await killEdgeProcesses(userDataDir);
208
- await new Promise((r) => setTimeout(r, 3000));
209
- needLaunch = true;
210
- }
211
- }
212
- }
213
-
214
- if (needLaunch) {
215
- if (isCustom) {
216
- console.error(`CDP 端口 ${port} 未就绪,正在启动 Edge 浏览器...`);
217
- } else {
218
- const edgeRunning = await isEdgeRunning();
219
- if (edgeRunning) {
220
- console.error(`Edge 已运行但 CDP 端口 ${port} 未启用,正在重启...`);
221
- await killEdgeProcesses(userDataDir);
222
- await new Promise((r) => setTimeout(r, 3000));
223
- } else {
224
- console.error(`CDP 端口 ${port} 未就绪,正在启动 Edge 浏览器...`);
225
- }
226
- }
227
- await launchEdgeWithCDP(port, userDataDir);
228
-
229
- console.error("等待浏览器启动...");
230
- const launched = await waitForCDP(port);
231
- if (!launched) {
232
- throw new Error(
233
- `等待 CDP 端口 ${port} 超时。请确认 Edge 浏览器已安装,\n` +
234
- `或手动启动: Microsoft Edge --remote-debugging-port=${port} [参见 cdp.js extraArgs]`,
235
- );
236
- }
237
- console.error("浏览器启动成功");
238
- }
239
-
240
- const browser = await chromium.connectOverCDP(`http://127.0.0.1:${port}`);
241
- return browser;
242
- }
243
-
244
- export async function switchAccount(oldAccount, newAccount) {
245
- console.error(`\n[账户切换] 从端口 ${oldAccount.port} -> ${newAccount.port}`);
246
- console.error(` [账户切换] 等待 30 秒,请完成当前操作后自动切换...`);
247
- await new Promise((r) => setTimeout(r, 30000));
248
-
249
- await killEdgeProcesses(oldAccount.userDataDir);
250
- await new Promise((r) => setTimeout(r, 3000));
251
-
252
- const newCdpOptions = {};
253
- newCdpOptions.port = newAccount.port;
254
- newCdpOptions.userDataDir = newAccount.userDataDir;
255
-
256
- const browser = await ensureBrowserReady(newCdpOptions);
257
-
258
- await new Promise((r) => setTimeout(r, 10000));
259
-
260
- return browser;
261
- }
1
+ import { exec } from "child_process";
2
+ import { execSync } from "child_process";
3
+ import fs from "fs";
4
+ import http from "http";
5
+ import os from "os";
6
+ import path from "path";
7
+ import { chromium } from "playwright";
8
+
9
+ const DEFAULT_CDP_PORT = 9222;
10
+ const DEFAULT_USER_DATA_DIR = path.join(
11
+ os.homedir(),
12
+ "Library",
13
+ "Application Support",
14
+ "Microsoft Edge For Testing",
15
+ );
16
+
17
+ function getEdgePath() {
18
+ const platform = os.platform();
19
+ if (platform === "darwin") return '"Microsoft Edge"';
20
+ if (platform === "win32") return "msedge.exe";
21
+ return "msedge";
22
+ }
23
+
24
+ function isEdgeRunning() {
25
+ return new Promise((resolve) => {
26
+ const platform = os.platform();
27
+ let command;
28
+ if (platform === "darwin") {
29
+ command =
30
+ 'ps aux | grep -q "[M]icrosoft Edge.app/Contents/MacOS/Microsoft Edge" 2>/dev/null';
31
+ } else if (platform === "win32") {
32
+ command =
33
+ 'tasklist /FI "IMAGENAME eq msedge.exe" 2>nul | findstr /I msedge';
34
+ } else {
35
+ command = "pgrep -f msedge > /dev/null 2>&1";
36
+ }
37
+ exec(command, (err) => resolve(!err));
38
+ });
39
+ }
40
+
41
+ function checkCDPPort(port) {
42
+ return new Promise((resolve) => {
43
+ const req = http.get(`http://127.0.0.1:${port}/json`, (res) => {
44
+ res.on("data", () => {});
45
+ res.on("end", () => resolve(res.statusCode === 200));
46
+ });
47
+ req.on("error", () => resolve(false));
48
+ req.setTimeout(3000, () => {
49
+ resolve(false);
50
+ req.destroy();
51
+ });
52
+ });
53
+ }
54
+
55
+ function checkEdgeArgs() {
56
+ return new Promise((resolve) => {
57
+ const platform = os.platform();
58
+ let command;
59
+ if (platform === "darwin") {
60
+ command =
61
+ 'ps aux | grep "[M]icrosoft Edge" | grep -v "Helper\\|crashpad" | grep "user-data-dir"';
62
+ } else if (platform === "win32") {
63
+ command =
64
+ 'wmic process where "name like \\"%msedge%\\"" get commandline | findstr "user-data-dir"';
65
+ } else {
66
+ command =
67
+ 'ps aux | grep "[m]sedge" | grep -v "Helper\\|crashpad" | grep "user-data-dir"';
68
+ }
69
+ exec(command, (err, stdout) => resolve(!err && stdout.trim().length > 0));
70
+ });
71
+ }
72
+
73
+ function killEdgeProcesses(targetDir) {
74
+ return new Promise((resolve) => {
75
+ const platform = os.platform();
76
+ let command;
77
+ if (platform === "darwin") {
78
+ if (targetDir) {
79
+ let pids = "";
80
+ try {
81
+ // ps aux 输出中 --user-data-dir= 后面没有引号
82
+ // 用路径精确匹配,结尾加空格或行尾避免子串误杀
83
+ const escapedDir = targetDir.replace(
84
+ /[-\/\\^$*+?.()|[\]{}]/g,
85
+ "\\$&",
86
+ );
87
+ pids = execSync(
88
+ `ps aux | grep "[M]icrosoft Edge" | grep -v "Helper\\|crashpad" | grep -E -- '--user-data-dir=${escapedDir}($|[^A-Za-z0-9_])' | awk '{print $2}'`,
89
+ )
90
+ .toString()
91
+ .trim();
92
+ } catch (e) {
93
+ pids = "";
94
+ }
95
+ if (pids) {
96
+ command = `kill -9 ${pids} 2>/dev/null; rm -f ~/Library/Caches/Microsoft\\ Edge/Singleton*; true`;
97
+ } else {
98
+ command = "true";
99
+ }
100
+ } else {
101
+ command =
102
+ 'killall -9 "Microsoft Edge" 2>/dev/null; rm -f ~/Library/Caches/Microsoft\\ Edge/Singleton*; true';
103
+ }
104
+ } else if (platform === "win32") {
105
+ if (targetDir) {
106
+ try {
107
+ const ps1Path = path.join(os.tmpdir(), `kill-edge-${Date.now()}.ps1`);
108
+ const escapedDir = targetDir.replace(/'/g, "''");
109
+ const ps1Content =
110
+ `$procs = Get-CimInstance Win32_Process -Filter "Name='msedge.exe'" 2>$null; ` +
111
+ `$count = 0; ` +
112
+ `foreach ($p in $procs) { ` +
113
+ ` if ($p.CommandLine -and $p.CommandLine -like "*${escapedDir}*") { ` +
114
+ ` Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue; ` +
115
+ ` $count++ ` +
116
+ ` } ` +
117
+ `}; ` +
118
+ `Get-ChildItem "$env:LOCALAPPDATA\\Microsoft\\Edge\\Singleton*" -ErrorAction SilentlyContinue | Remove-Item -Force; ` +
119
+ `Write-Output $count`;
120
+ fs.writeFileSync(ps1Path, ps1Content);
121
+ const result = execSync(
122
+ `powershell.exe -ExecutionPolicy Bypass -File "${ps1Path}"`,
123
+ )
124
+ .toString()
125
+ .trim();
126
+ fs.unlinkSync(ps1Path);
127
+ command = "exit 0";
128
+ } catch {
129
+ command = "taskkill /F /IM msedge.exe 2>nul || exit 0";
130
+ }
131
+ } else {
132
+ command = "taskkill /F /IM msedge.exe 2>nul || exit 0";
133
+ }
134
+ } else {
135
+ if (targetDir) {
136
+ const escapedDir = targetDir.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
137
+ command = `ps aux | grep msedge | grep -v Helper | grep -E -- '--user-data-dir=${escapedDir}($|[^A-Za-z0-9_])' | awk '{print $2}' | xargs -r kill -9 2>/dev/null; true`;
138
+ } else {
139
+ command = "pkill -9 -f msedge 2>/dev/null; true";
140
+ }
141
+ }
142
+ exec(command, () => resolve());
143
+ });
144
+ }
145
+
146
+ function launchEdgeWithCDP(port, userDataDir) {
147
+ return new Promise((resolve, reject) => {
148
+ const platform = os.platform();
149
+ const edgePath = getEdgePath();
150
+ let command;
151
+
152
+ const extraArgs = [
153
+ `--remote-debugging-port=${port}`,
154
+ `--user-data-dir="${userDataDir}"`,
155
+ "--disable-blink-features=AutomationControlled",
156
+ "--no-first-run",
157
+ "--no-default-browser-check",
158
+ "--password-store=basic",
159
+ "--disable-background-mode",
160
+ "--disable-component-update",
161
+ "--disable-crash-reporter",
162
+ "--disable-breakpad",
163
+ "--disable-background-networking",
164
+ "--disable-sync",
165
+ ].join(" ");
166
+
167
+ if (platform === "darwin") {
168
+ command = `open -a ${edgePath} --new --args ${extraArgs}`;
169
+ } else if (platform === "win32") {
170
+ command = `start msedge ${extraArgs}`;
171
+ } else {
172
+ command = `msedge ${extraArgs} &`;
173
+ }
174
+
175
+ exec(command, (err) => {
176
+ if (err) reject(new Error(`启动 Edge 浏览器失败: ${err.message}`));
177
+ else resolve();
178
+ });
179
+ });
180
+ }
181
+
182
+ async function waitForCDP(port, timeout = 30000, interval = 1000) {
183
+ const start = Date.now();
184
+ while (Date.now() - start < timeout) {
185
+ const ready = await checkCDPPort(port);
186
+ if (ready) return true;
187
+ await new Promise((r) => setTimeout(r, interval));
188
+ }
189
+ return false;
190
+ }
191
+
192
+ export { killEdgeProcesses };
193
+
194
+ export async function ensureBrowserReady(options = {}) {
195
+ const port = options.port || DEFAULT_CDP_PORT;
196
+ const userDataDir = options.userDataDir || DEFAULT_USER_DATA_DIR;
197
+ const isCustom = port !== DEFAULT_CDP_PORT || !!options.userDataDir;
198
+
199
+ const isReady = await checkCDPPort(port);
200
+ let needLaunch = !isReady;
201
+
202
+ if (!needLaunch) {
203
+ if (!isCustom) {
204
+ const edgeArgsValid = await checkEdgeArgs();
205
+ if (!edgeArgsValid) {
206
+ console.error(`Edge 已运行但启动参数不完整,正在重启端口 ${port}...`);
207
+ await killEdgeProcesses(userDataDir);
208
+ await new Promise((r) => setTimeout(r, 3000));
209
+ needLaunch = true;
210
+ }
211
+ }
212
+ }
213
+
214
+ if (needLaunch) {
215
+ if (isCustom) {
216
+ console.error(`CDP 端口 ${port} 未就绪,正在启动 Edge 浏览器...`);
217
+ } else {
218
+ const edgeRunning = await isEdgeRunning();
219
+ if (edgeRunning) {
220
+ console.error(`Edge 已运行但 CDP 端口 ${port} 未启用,正在重启...`);
221
+ await killEdgeProcesses(userDataDir);
222
+ await new Promise((r) => setTimeout(r, 3000));
223
+ } else {
224
+ console.error(`CDP 端口 ${port} 未就绪,正在启动 Edge 浏览器...`);
225
+ }
226
+ }
227
+ await launchEdgeWithCDP(port, userDataDir);
228
+
229
+ console.error("等待浏览器启动...");
230
+ const launched = await waitForCDP(port);
231
+ if (!launched) {
232
+ throw new Error(
233
+ `等待 CDP 端口 ${port} 超时。请确认 Edge 浏览器已安装,\n` +
234
+ `或手动启动: Microsoft Edge --remote-debugging-port=${port} [参见 cdp.js extraArgs]`,
235
+ );
236
+ }
237
+ console.error("浏览器启动成功");
238
+ }
239
+
240
+ const browser = await chromium.connectOverCDP(`http://127.0.0.1:${port}`);
241
+ return browser;
242
+ }
243
+
244
+ export async function switchAccount(oldAccount, newAccount) {
245
+ console.error(`\n[账户切换] 从端口 ${oldAccount.port} -> ${newAccount.port}`);
246
+ console.error(` [账户切换] 等待 30 秒,请完成当前操作后自动切换...`);
247
+ await new Promise((r) => setTimeout(r, 30000));
248
+
249
+ await killEdgeProcesses(oldAccount.userDataDir);
250
+ await new Promise((r) => setTimeout(r, 3000));
251
+
252
+ const newCdpOptions = {};
253
+ newCdpOptions.port = newAccount.port;
254
+ newCdpOptions.userDataDir = newAccount.userDataDir;
255
+
256
+ const browser = await ensureBrowserReady(newCdpOptions);
257
+
258
+ await new Promise((r) => setTimeout(r, 10000));
259
+
260
+ return browser;
261
+ }