tt-help-cli-ycl 1.3.6 → 1.3.7
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.
- package/README.md +17 -17
- package/cli.js +9 -9
- package/package.json +45 -45
- package/src/cli/auto.js +131 -121
- package/src/cli/explore.js +147 -138
- package/src/cli/progress.js +111 -111
- package/src/cli/scrape.js +47 -47
- package/src/cli/utils.js +18 -18
- package/src/cli/videos.js +41 -41
- package/src/cli/watch.js +31 -31
- package/src/lib/args.js +391 -391
- package/src/lib/browser/anti-detect.js +23 -23
- package/src/lib/browser/cdp.js +142 -142
- package/src/lib/browser/launch.js +43 -43
- package/src/lib/browser/page.js +87 -87
- package/src/lib/constants.js +109 -95
- package/src/lib/delay.js +54 -54
- package/src/lib/explore-fetch.js +118 -118
- package/src/lib/fetcher.js +45 -45
- package/src/lib/filter.js +66 -66
- package/src/lib/io.js +54 -54
- package/src/lib/mac-or-uuid.js +82 -0
- package/src/lib/output.js +80 -80
- package/src/lib/parser.js +47 -47
- package/src/lib/retry.js +44 -44
- package/src/lib/scrape.js +40 -40
- package/src/lib/url.js +52 -52
- package/src/main.mjs +221 -221
- package/src/scraper/auto-core.mjs +185 -185
- package/src/scraper/core.mjs +190 -190
- package/src/scraper/explore-core.mjs +162 -162
- package/src/scraper/modules/captcha-handler.mjs +114 -114
- package/src/scraper/modules/comment-extractor.mjs +69 -69
- package/src/scraper/modules/follow-extractor.mjs +121 -121
- package/src/scraper/modules/guess-extractor.mjs +51 -51
- package/src/scraper/modules/page-error-detector.mjs +70 -70
- package/src/scraper/modules/page-helpers.mjs +48 -48
- package/src/scraper/modules/scroll-collector.mjs +189 -189
- package/src/test-auto-follow.cjs +109 -0
- package/src/test-extractors.cjs +75 -0
- package/src/test-follow.cjs +41 -0
- package/src/videos/core.mjs +126 -126
- package/src/watch/data-store.mjs +258 -261
- package/src/watch/public/index.html +466 -465
- package/src/watch/server.mjs +291 -281
- package/src/results/user-videos-bar.lar.lar.moeta.json +0 -37
|
@@ -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
|
+
}
|
package/src/lib/browser/cdp.js
CHANGED
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
import { exec } from 'child_process';
|
|
2
|
-
import http from 'http';
|
|
3
|
-
import os from 'os';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import { chromium } from 'playwright';
|
|
6
|
-
|
|
7
|
-
export const CDP_PORT = 9222;
|
|
8
|
-
|
|
9
|
-
function getEdgePath() {
|
|
10
|
-
const platform = os.platform();
|
|
11
|
-
if (platform === 'darwin') return '"Microsoft Edge"';
|
|
12
|
-
if (platform === 'win32') return 'msedge.exe';
|
|
13
|
-
return 'msedge';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function isEdgeRunning() {
|
|
17
|
-
return new Promise(resolve => {
|
|
18
|
-
const platform = os.platform();
|
|
19
|
-
let command;
|
|
20
|
-
if (platform === 'darwin') {
|
|
21
|
-
command = 'ps aux | grep -q "[M]icrosoft Edge.app/Contents/MacOS/Microsoft Edge" 2>/dev/null';
|
|
22
|
-
} else if (platform === 'win32') {
|
|
23
|
-
command = 'tasklist /FI "IMAGENAME eq msedge.exe" 2>nul | findstr /I msedge';
|
|
24
|
-
} else {
|
|
25
|
-
command = 'pgrep -f msedge > /dev/null 2>&1';
|
|
26
|
-
}
|
|
27
|
-
exec(command, (err) => resolve(!err));
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function checkCDPPort() {
|
|
32
|
-
return new Promise(resolve => {
|
|
33
|
-
const req = http.get(`http://127.0.0.1:${CDP_PORT}/json`, res => {
|
|
34
|
-
res.on('data', () => {});
|
|
35
|
-
res.on('end', () => resolve(res.statusCode === 200));
|
|
36
|
-
});
|
|
37
|
-
req.on('error', () => resolve(false));
|
|
38
|
-
req.setTimeout(3000, () => { resolve(false); req.destroy(); });
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function checkEdgeArgs() {
|
|
43
|
-
return new Promise(resolve => {
|
|
44
|
-
const platform = os.platform();
|
|
45
|
-
let command;
|
|
46
|
-
if (platform === 'darwin') {
|
|
47
|
-
command = 'ps aux | grep "[M]icrosoft Edge" | grep -v "Helper\\|crashpad" | grep "user-data-dir"';
|
|
48
|
-
} else if (platform === 'win32') {
|
|
49
|
-
command = 'wmic process where "name like \\"%msedge%\\"" get commandline | findstr "user-data-dir"';
|
|
50
|
-
} else {
|
|
51
|
-
command = 'ps aux | grep "[m]sedge" | grep -v "Helper\\|crashpad" | grep "user-data-dir"';
|
|
52
|
-
}
|
|
53
|
-
exec(command, (err, stdout) => resolve(!err && stdout.trim().length > 0));
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function killEdgeProcesses() {
|
|
58
|
-
return new Promise(resolve => {
|
|
59
|
-
const platform = os.platform();
|
|
60
|
-
let command;
|
|
61
|
-
if (platform === 'darwin') {
|
|
62
|
-
command = 'killall -9 "Microsoft Edge" 2>/dev/null; rm -f ~/Library/Caches/Microsoft\\ Edge/Singleton*; true';
|
|
63
|
-
} else if (platform === 'win32') {
|
|
64
|
-
command = 'taskkill /F /IM msedge.exe 2>nul || exit 0';
|
|
65
|
-
} else {
|
|
66
|
-
command = 'pkill -9 -f msedge 2>/dev/null; true';
|
|
67
|
-
}
|
|
68
|
-
exec(command, () => resolve());
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function launchEdgeWithCDP() {
|
|
73
|
-
return new Promise((resolve, reject) => {
|
|
74
|
-
const platform = os.platform();
|
|
75
|
-
const edgePath = getEdgePath();
|
|
76
|
-
const userDataDir = path.join(os.homedir(), 'Library', 'Application Support', 'Microsoft Edge For Testing');
|
|
77
|
-
let command;
|
|
78
|
-
|
|
79
|
-
if (platform === 'darwin') {
|
|
80
|
-
command = `open -a ${edgePath} --args --remote-debugging-port=${CDP_PORT} --user-data-dir="${userDataDir}"`;
|
|
81
|
-
} else if (platform === 'win32') {
|
|
82
|
-
command = `start msedge --remote-debugging-port=${CDP_PORT} --user-data-dir="${userDataDir}"`;
|
|
83
|
-
} else {
|
|
84
|
-
command = `msedge --remote-debugging-port=${CDP_PORT} --user-data-dir="${userDataDir}" &`;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
exec(command, (err) => {
|
|
88
|
-
if (err) reject(new Error(`启动 Edge 浏览器失败: ${err.message}`));
|
|
89
|
-
else resolve();
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async function waitForCDP(timeout = 30000, interval = 1000) {
|
|
95
|
-
const start = Date.now();
|
|
96
|
-
while (Date.now() - start < timeout) {
|
|
97
|
-
const ready = await checkCDPPort();
|
|
98
|
-
if (ready) return true;
|
|
99
|
-
await new Promise(r => setTimeout(r, interval));
|
|
100
|
-
}
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export async function ensureBrowserReady() {
|
|
105
|
-
const isReady = await checkCDPPort();
|
|
106
|
-
let needLaunch = !isReady;
|
|
107
|
-
|
|
108
|
-
if (!needLaunch) {
|
|
109
|
-
const edgeArgsValid = await checkEdgeArgs();
|
|
110
|
-
if (!edgeArgsValid) {
|
|
111
|
-
console.error('Edge 已运行但启动参数不完整,正在重启...');
|
|
112
|
-
await killEdgeProcesses();
|
|
113
|
-
await new Promise(r => setTimeout(r, 3000));
|
|
114
|
-
needLaunch = true;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (needLaunch) {
|
|
119
|
-
const edgeRunning = await isEdgeRunning();
|
|
120
|
-
if (edgeRunning) {
|
|
121
|
-
console.error(`Edge 已运行但 CDP 端口 ${CDP_PORT} 未启用,正在重启...`);
|
|
122
|
-
await killEdgeProcesses();
|
|
123
|
-
await new Promise(r => setTimeout(r, 3000));
|
|
124
|
-
} else {
|
|
125
|
-
console.error(`CDP 端口 ${CDP_PORT} 未就绪,正在启动 Edge 浏览器...`);
|
|
126
|
-
}
|
|
127
|
-
await launchEdgeWithCDP();
|
|
128
|
-
|
|
129
|
-
console.error('等待浏览器启动...');
|
|
130
|
-
const launched = await waitForCDP();
|
|
131
|
-
if (!launched) {
|
|
132
|
-
throw new Error(
|
|
133
|
-
`等待 CDP 端口 ${CDP_PORT} 超时。请确认 Edge 浏览器已安装,\n` +
|
|
134
|
-
'或手动启动: Microsoft Edge --remote-debugging-port=9222'
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
console.error('浏览器启动成功');
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const browser = await chromium.connectOverCDP(`http://127.0.0.1:${CDP_PORT}`);
|
|
141
|
-
return browser;
|
|
142
|
-
}
|
|
1
|
+
import { exec } from 'child_process';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { chromium } from 'playwright';
|
|
6
|
+
|
|
7
|
+
export const CDP_PORT = 9222;
|
|
8
|
+
|
|
9
|
+
function getEdgePath() {
|
|
10
|
+
const platform = os.platform();
|
|
11
|
+
if (platform === 'darwin') return '"Microsoft Edge"';
|
|
12
|
+
if (platform === 'win32') return 'msedge.exe';
|
|
13
|
+
return 'msedge';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isEdgeRunning() {
|
|
17
|
+
return new Promise(resolve => {
|
|
18
|
+
const platform = os.platform();
|
|
19
|
+
let command;
|
|
20
|
+
if (platform === 'darwin') {
|
|
21
|
+
command = 'ps aux | grep -q "[M]icrosoft Edge.app/Contents/MacOS/Microsoft Edge" 2>/dev/null';
|
|
22
|
+
} else if (platform === 'win32') {
|
|
23
|
+
command = 'tasklist /FI "IMAGENAME eq msedge.exe" 2>nul | findstr /I msedge';
|
|
24
|
+
} else {
|
|
25
|
+
command = 'pgrep -f msedge > /dev/null 2>&1';
|
|
26
|
+
}
|
|
27
|
+
exec(command, (err) => resolve(!err));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function checkCDPPort() {
|
|
32
|
+
return new Promise(resolve => {
|
|
33
|
+
const req = http.get(`http://127.0.0.1:${CDP_PORT}/json`, res => {
|
|
34
|
+
res.on('data', () => {});
|
|
35
|
+
res.on('end', () => resolve(res.statusCode === 200));
|
|
36
|
+
});
|
|
37
|
+
req.on('error', () => resolve(false));
|
|
38
|
+
req.setTimeout(3000, () => { resolve(false); req.destroy(); });
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function checkEdgeArgs() {
|
|
43
|
+
return new Promise(resolve => {
|
|
44
|
+
const platform = os.platform();
|
|
45
|
+
let command;
|
|
46
|
+
if (platform === 'darwin') {
|
|
47
|
+
command = 'ps aux | grep "[M]icrosoft Edge" | grep -v "Helper\\|crashpad" | grep "user-data-dir"';
|
|
48
|
+
} else if (platform === 'win32') {
|
|
49
|
+
command = 'wmic process where "name like \\"%msedge%\\"" get commandline | findstr "user-data-dir"';
|
|
50
|
+
} else {
|
|
51
|
+
command = 'ps aux | grep "[m]sedge" | grep -v "Helper\\|crashpad" | grep "user-data-dir"';
|
|
52
|
+
}
|
|
53
|
+
exec(command, (err, stdout) => resolve(!err && stdout.trim().length > 0));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function killEdgeProcesses() {
|
|
58
|
+
return new Promise(resolve => {
|
|
59
|
+
const platform = os.platform();
|
|
60
|
+
let command;
|
|
61
|
+
if (platform === 'darwin') {
|
|
62
|
+
command = 'killall -9 "Microsoft Edge" 2>/dev/null; rm -f ~/Library/Caches/Microsoft\\ Edge/Singleton*; true';
|
|
63
|
+
} else if (platform === 'win32') {
|
|
64
|
+
command = 'taskkill /F /IM msedge.exe 2>nul || exit 0';
|
|
65
|
+
} else {
|
|
66
|
+
command = 'pkill -9 -f msedge 2>/dev/null; true';
|
|
67
|
+
}
|
|
68
|
+
exec(command, () => resolve());
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function launchEdgeWithCDP() {
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const platform = os.platform();
|
|
75
|
+
const edgePath = getEdgePath();
|
|
76
|
+
const userDataDir = path.join(os.homedir(), 'Library', 'Application Support', 'Microsoft Edge For Testing');
|
|
77
|
+
let command;
|
|
78
|
+
|
|
79
|
+
if (platform === 'darwin') {
|
|
80
|
+
command = `open -a ${edgePath} --args --remote-debugging-port=${CDP_PORT} --user-data-dir="${userDataDir}"`;
|
|
81
|
+
} else if (platform === 'win32') {
|
|
82
|
+
command = `start msedge --remote-debugging-port=${CDP_PORT} --user-data-dir="${userDataDir}"`;
|
|
83
|
+
} else {
|
|
84
|
+
command = `msedge --remote-debugging-port=${CDP_PORT} --user-data-dir="${userDataDir}" &`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
exec(command, (err) => {
|
|
88
|
+
if (err) reject(new Error(`启动 Edge 浏览器失败: ${err.message}`));
|
|
89
|
+
else resolve();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function waitForCDP(timeout = 30000, interval = 1000) {
|
|
95
|
+
const start = Date.now();
|
|
96
|
+
while (Date.now() - start < timeout) {
|
|
97
|
+
const ready = await checkCDPPort();
|
|
98
|
+
if (ready) return true;
|
|
99
|
+
await new Promise(r => setTimeout(r, interval));
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function ensureBrowserReady() {
|
|
105
|
+
const isReady = await checkCDPPort();
|
|
106
|
+
let needLaunch = !isReady;
|
|
107
|
+
|
|
108
|
+
if (!needLaunch) {
|
|
109
|
+
const edgeArgsValid = await checkEdgeArgs();
|
|
110
|
+
if (!edgeArgsValid) {
|
|
111
|
+
console.error('Edge 已运行但启动参数不完整,正在重启...');
|
|
112
|
+
await killEdgeProcesses();
|
|
113
|
+
await new Promise(r => setTimeout(r, 3000));
|
|
114
|
+
needLaunch = true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (needLaunch) {
|
|
119
|
+
const edgeRunning = await isEdgeRunning();
|
|
120
|
+
if (edgeRunning) {
|
|
121
|
+
console.error(`Edge 已运行但 CDP 端口 ${CDP_PORT} 未启用,正在重启...`);
|
|
122
|
+
await killEdgeProcesses();
|
|
123
|
+
await new Promise(r => setTimeout(r, 3000));
|
|
124
|
+
} else {
|
|
125
|
+
console.error(`CDP 端口 ${CDP_PORT} 未就绪,正在启动 Edge 浏览器...`);
|
|
126
|
+
}
|
|
127
|
+
await launchEdgeWithCDP();
|
|
128
|
+
|
|
129
|
+
console.error('等待浏览器启动...');
|
|
130
|
+
const launched = await waitForCDP();
|
|
131
|
+
if (!launched) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
`等待 CDP 端口 ${CDP_PORT} 超时。请确认 Edge 浏览器已安装,\n` +
|
|
134
|
+
'或手动启动: Microsoft Edge --remote-debugging-port=9222'
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
console.error('浏览器启动成功');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const browser = await chromium.connectOverCDP(`http://127.0.0.1:${CDP_PORT}`);
|
|
141
|
+
return browser;
|
|
142
|
+
}
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { accessSync } from 'fs';
|
|
2
|
-
|
|
3
|
-
export function detectBrowser() {
|
|
4
|
-
const isMac = process.platform === 'darwin';
|
|
5
|
-
const isWin = process.platform === 'win32';
|
|
6
|
-
const isLinux = process.platform === 'linux';
|
|
7
|
-
|
|
8
|
-
const paths = [];
|
|
9
|
-
|
|
10
|
-
if (isMac) {
|
|
11
|
-
paths.push(
|
|
12
|
-
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
13
|
-
'/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
|
|
14
|
-
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
15
|
-
'/Applications/Brave Browser.app/Contents/MacOS/Brave Browser',
|
|
16
|
-
);
|
|
17
|
-
} else if (isWin) {
|
|
18
|
-
const localAppData = process.env.LOCALAPPDATA || '';
|
|
19
|
-
const programFiles = process.env.PROGRAMFILES || '';
|
|
20
|
-
const programFilesX86 = process.env['PROGRAMFILES(X86)'] || '';
|
|
21
|
-
paths.push(
|
|
22
|
-
`${programFiles}\\Google\\Chrome\\Application\\chrome.exe`,
|
|
23
|
-
`${programFilesX86}\\Google\\Chrome\\Application\\chrome.exe`,
|
|
24
|
-
`${localAppData}\\Google\\Chrome\\Application\\chrome.exe`,
|
|
25
|
-
`${programFiles}\\Microsoft\\Edge\\Application\\msedge.exe`,
|
|
26
|
-
`${programFilesX86}\\Microsoft\\Edge\\Application\\msedge.exe`,
|
|
27
|
-
);
|
|
28
|
-
} else if (isLinux) {
|
|
29
|
-
paths.push(
|
|
30
|
-
'/usr/bin/google-chrome',
|
|
31
|
-
'/usr/bin/google-chrome-stable',
|
|
32
|
-
'/usr/bin/chromium-browser',
|
|
33
|
-
'/usr/bin/chromium',
|
|
34
|
-
'/snap/bin/chromium',
|
|
35
|
-
'/usr/bin/microsoft-edge',
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
for (const p of paths) {
|
|
40
|
-
try { accessSync(p); return p; } catch { /* not found */ }
|
|
41
|
-
}
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
1
|
+
import { accessSync } from 'fs';
|
|
2
|
+
|
|
3
|
+
export function detectBrowser() {
|
|
4
|
+
const isMac = process.platform === 'darwin';
|
|
5
|
+
const isWin = process.platform === 'win32';
|
|
6
|
+
const isLinux = process.platform === 'linux';
|
|
7
|
+
|
|
8
|
+
const paths = [];
|
|
9
|
+
|
|
10
|
+
if (isMac) {
|
|
11
|
+
paths.push(
|
|
12
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
13
|
+
'/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
|
|
14
|
+
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
15
|
+
'/Applications/Brave Browser.app/Contents/MacOS/Brave Browser',
|
|
16
|
+
);
|
|
17
|
+
} else if (isWin) {
|
|
18
|
+
const localAppData = process.env.LOCALAPPDATA || '';
|
|
19
|
+
const programFiles = process.env.PROGRAMFILES || '';
|
|
20
|
+
const programFilesX86 = process.env['PROGRAMFILES(X86)'] || '';
|
|
21
|
+
paths.push(
|
|
22
|
+
`${programFiles}\\Google\\Chrome\\Application\\chrome.exe`,
|
|
23
|
+
`${programFilesX86}\\Google\\Chrome\\Application\\chrome.exe`,
|
|
24
|
+
`${localAppData}\\Google\\Chrome\\Application\\chrome.exe`,
|
|
25
|
+
`${programFiles}\\Microsoft\\Edge\\Application\\msedge.exe`,
|
|
26
|
+
`${programFilesX86}\\Microsoft\\Edge\\Application\\msedge.exe`,
|
|
27
|
+
);
|
|
28
|
+
} else if (isLinux) {
|
|
29
|
+
paths.push(
|
|
30
|
+
'/usr/bin/google-chrome',
|
|
31
|
+
'/usr/bin/google-chrome-stable',
|
|
32
|
+
'/usr/bin/chromium-browser',
|
|
33
|
+
'/usr/bin/chromium',
|
|
34
|
+
'/snap/bin/chromium',
|
|
35
|
+
'/usr/bin/microsoft-edge',
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
for (const p of paths) {
|
|
40
|
+
try { accessSync(p); return p; } catch { /* not found */ }
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
package/src/lib/browser/page.js
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
import { delay } from '../delay.js';
|
|
2
|
-
import { retryWithBackoff } from '../retry.js';
|
|
3
|
-
import { getDelayConfig } from '../delay.js';
|
|
4
|
-
|
|
5
|
-
export async function isLoggedIn(page) {
|
|
6
|
-
return page.evaluate(() => {
|
|
7
|
-
// 已登录时会出现在个人主页区域的元素
|
|
8
|
-
const hasProfileContainer = !!document.querySelector(
|
|
9
|
-
'[class*="DivProfileContainer"], [class*="DivUserContainer"]'
|
|
10
|
-
);
|
|
11
|
-
// 已登录时顶部导航栏有用户相关菜单
|
|
12
|
-
const hasUserMenu = !!document.querySelector(
|
|
13
|
-
'[class*="UserMenu"], [class*="user-menu"], [class*="CurrentUserInfo"]'
|
|
14
|
-
);
|
|
15
|
-
// 有登录按钮说明未登录
|
|
16
|
-
const hasLoginButton = Array.from(document.querySelectorAll('button, [role="button"]'))
|
|
17
|
-
.some(el => /^(登录|Log in|Sign in)$/i.test(el.textContent.trim()));
|
|
18
|
-
|
|
19
|
-
return (hasProfileContainer || hasUserMenu) && !hasLoginButton;
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function closeCommentPanel(page) {
|
|
24
|
-
await page.evaluate(() => {
|
|
25
|
-
const rightPanel = document.querySelector('[class*="RightPanelContainer"]');
|
|
26
|
-
if (rightPanel) {
|
|
27
|
-
const tabContainer = rightPanel.querySelector('[class*="TabContainer"]');
|
|
28
|
-
if (tabContainer) {
|
|
29
|
-
const closeOverlay = tabContainer.querySelector('div:last-child');
|
|
30
|
-
if (closeOverlay) closeOverlay.click();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export async function ensureTikTokPage(browser, url) {
|
|
37
|
-
const contexts = browser.contexts();
|
|
38
|
-
let page = null;
|
|
39
|
-
|
|
40
|
-
for (const ctx of contexts) {
|
|
41
|
-
for (const p of ctx.pages()) {
|
|
42
|
-
if (p.url().includes('tiktok.com')) {
|
|
43
|
-
page = p;
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (page) break;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (!page) {
|
|
51
|
-
console.error('未找到 TikTok 页面,正在打开...');
|
|
52
|
-
const defaultCtx = browser.contexts()[0];
|
|
53
|
-
page = await defaultCtx.newPage();
|
|
54
|
-
await retryWithBackoff(() => page.goto(url, { waitUntil: 'load', timeout: 30000 }));
|
|
55
|
-
const config = getDelayConfig();
|
|
56
|
-
await delay(Math.round(config.switchMax * 0.5), config.switchMax);
|
|
57
|
-
console.error('TikTok 页面已打开');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return page;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export async function findTikTokPage(browser) {
|
|
64
|
-
const contexts = browser.contexts();
|
|
65
|
-
for (const ctx of contexts) {
|
|
66
|
-
for (const p of ctx.pages()) {
|
|
67
|
-
if (p.url().includes('tiktok.com')) return p;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export async function getOrCreatePage(browser) {
|
|
74
|
-
let page = await findTikTokPage(browser);
|
|
75
|
-
if (!page) {
|
|
76
|
-
const defaultCtx = browser.contexts()[0] || await browser.newContext();
|
|
77
|
-
page = await defaultCtx.newPage();
|
|
78
|
-
}
|
|
79
|
-
return page;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function assertPageUrl(page, expectedPath) {
|
|
83
|
-
const actual = page.url();
|
|
84
|
-
if (!actual.includes(expectedPath)) {
|
|
85
|
-
throw new Error(`[代理错误] 预期访问 ${expectedPath},实际跳转到了 ${actual}`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
1
|
+
import { delay } from '../delay.js';
|
|
2
|
+
import { retryWithBackoff } from '../retry.js';
|
|
3
|
+
import { getDelayConfig } from '../delay.js';
|
|
4
|
+
|
|
5
|
+
export async function isLoggedIn(page) {
|
|
6
|
+
return page.evaluate(() => {
|
|
7
|
+
// 已登录时会出现在个人主页区域的元素
|
|
8
|
+
const hasProfileContainer = !!document.querySelector(
|
|
9
|
+
'[class*="DivProfileContainer"], [class*="DivUserContainer"]'
|
|
10
|
+
);
|
|
11
|
+
// 已登录时顶部导航栏有用户相关菜单
|
|
12
|
+
const hasUserMenu = !!document.querySelector(
|
|
13
|
+
'[class*="UserMenu"], [class*="user-menu"], [class*="CurrentUserInfo"]'
|
|
14
|
+
);
|
|
15
|
+
// 有登录按钮说明未登录
|
|
16
|
+
const hasLoginButton = Array.from(document.querySelectorAll('button, [role="button"]'))
|
|
17
|
+
.some(el => /^(登录|Log in|Sign in)$/i.test(el.textContent.trim()));
|
|
18
|
+
|
|
19
|
+
return (hasProfileContainer || hasUserMenu) && !hasLoginButton;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function closeCommentPanel(page) {
|
|
24
|
+
await page.evaluate(() => {
|
|
25
|
+
const rightPanel = document.querySelector('[class*="RightPanelContainer"]');
|
|
26
|
+
if (rightPanel) {
|
|
27
|
+
const tabContainer = rightPanel.querySelector('[class*="TabContainer"]');
|
|
28
|
+
if (tabContainer) {
|
|
29
|
+
const closeOverlay = tabContainer.querySelector('div:last-child');
|
|
30
|
+
if (closeOverlay) closeOverlay.click();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function ensureTikTokPage(browser, url) {
|
|
37
|
+
const contexts = browser.contexts();
|
|
38
|
+
let page = null;
|
|
39
|
+
|
|
40
|
+
for (const ctx of contexts) {
|
|
41
|
+
for (const p of ctx.pages()) {
|
|
42
|
+
if (p.url().includes('tiktok.com')) {
|
|
43
|
+
page = p;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (page) break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!page) {
|
|
51
|
+
console.error('未找到 TikTok 页面,正在打开...');
|
|
52
|
+
const defaultCtx = browser.contexts()[0];
|
|
53
|
+
page = await defaultCtx.newPage();
|
|
54
|
+
await retryWithBackoff(() => page.goto(url, { waitUntil: 'load', timeout: 30000 }));
|
|
55
|
+
const config = getDelayConfig();
|
|
56
|
+
await delay(Math.round(config.switchMax * 0.5), config.switchMax);
|
|
57
|
+
console.error('TikTok 页面已打开');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return page;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function findTikTokPage(browser) {
|
|
64
|
+
const contexts = browser.contexts();
|
|
65
|
+
for (const ctx of contexts) {
|
|
66
|
+
for (const p of ctx.pages()) {
|
|
67
|
+
if (p.url().includes('tiktok.com')) return p;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function getOrCreatePage(browser) {
|
|
74
|
+
let page = await findTikTokPage(browser);
|
|
75
|
+
if (!page) {
|
|
76
|
+
const defaultCtx = browser.contexts()[0] || await browser.newContext();
|
|
77
|
+
page = await defaultCtx.newPage();
|
|
78
|
+
}
|
|
79
|
+
return page;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function assertPageUrl(page, expectedPath) {
|
|
83
|
+
const actual = page.url();
|
|
84
|
+
if (!actual.includes(expectedPath)) {
|
|
85
|
+
throw new Error(`[代理错误] 预期访问 ${expectedPath},实际跳转到了 ${actual}`);
|
|
86
|
+
}
|
|
87
|
+
}
|