tt-help-cli-ycl 1.3.12 → 1.3.14
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 +47 -45
- package/scripts/run-explore.bat +68 -68
- package/scripts/run-explore.ps1 +81 -81
- package/scripts/run-explore.sh +73 -73
- package/scripts/test-captcha-lib.mjs +68 -0
- package/scripts/test-captcha.mjs +81 -0
- package/scripts/test-incognito-lib.mjs +36 -0
- package/scripts/test-login-state.mjs +128 -0
- package/scripts/test-safe-click.mjs +45 -0
- package/src/cli/attach.js +160 -0
- package/src/cli/auto.js +186 -157
- package/src/cli/config.js +39 -3
- package/src/cli/explore.js +234 -193
- package/src/cli/info.js +88 -0
- package/src/cli/progress.js +111 -111
- package/src/cli/refresh.js +216 -0
- 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 +517 -402
- package/src/lib/browser/anti-detect.js +23 -23
- package/src/lib/browser/cdp.js +52 -10
- package/src/lib/browser/launch.js +43 -43
- package/src/lib/browser/page.js +146 -87
- package/src/lib/constants.js +199 -115
- 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/output.js +80 -80
- package/src/lib/parse-ssr.mjs +69 -0
- package/src/lib/parser.js +47 -47
- package/src/lib/retry.js +45 -45
- package/src/lib/scrape.js +89 -40
- package/src/lib/tiktok-scraper.mjs +176 -0
- package/src/lib/url.js +52 -52
- package/src/main.js +12 -16
- package/src/results/user-videos-bar.lar.lar.moeta.json +37 -0
- package/src/scraper/auto-core.js +203 -194
- package/src/scraper/core.js +211 -190
- package/src/scraper/explore-core.js +162 -171
- package/src/scraper/modules/captcha-handler.js +114 -114
- package/src/scraper/modules/comment-extractor.js +74 -69
- package/src/scraper/modules/follow-extractor.js +121 -121
- package/src/scraper/modules/guess-extractor.js +51 -51
- package/src/scraper/modules/page-helpers.js +48 -48
- package/src/scraper/refresh-core.js +179 -0
- package/src/videos/core.js +126 -126
- package/src/watch/data-store.js +536 -302
- package/src/watch/public/index.html +721 -701
- package/src/watch/server.js +527 -359
|
@@ -1,171 +1,162 @@
|
|
|
1
|
-
import {
|
|
2
|
-
delay,
|
|
3
|
-
ensureBrowserReady,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
result.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
log(` @${username}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
} catch (e) {
|
|
164
|
-
result.error = e.message;
|
|
165
|
-
log(` [错误] ${e.message}`);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return result;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export { processExplore };
|
|
1
|
+
import {
|
|
2
|
+
delay,
|
|
3
|
+
ensureBrowserReady,
|
|
4
|
+
retryWithBackoff,
|
|
5
|
+
detectPageError,
|
|
6
|
+
isLoggedIn,
|
|
7
|
+
assertPageUrl,
|
|
8
|
+
} from './modules/page-helpers.js';
|
|
9
|
+
import { detectCaptcha } from './modules/captcha-handler.js';
|
|
10
|
+
export { ensureBrowserReady };
|
|
11
|
+
import { getUserInfo, collectVideos } from '../videos/core.js';
|
|
12
|
+
import { extractFollowAndFollowers } from './modules/follow-extractor.js';
|
|
13
|
+
import { extractVideoLocation } from '../lib/scrape.js';
|
|
14
|
+
import { maxFollowing as globalMaxFollowing, maxFollowers as globalMaxFollowers, maxVideos as globalMaxVideos } from '../lib/constants.js';
|
|
15
|
+
|
|
16
|
+
async function processExplore(page, username, options, log) {
|
|
17
|
+
const {
|
|
18
|
+
maxVideos = 1,
|
|
19
|
+
enableFollow = true,
|
|
20
|
+
maxFollowing = 5,
|
|
21
|
+
maxFollowers = 5,
|
|
22
|
+
location = 'PL,NL,BE,DE,FR,IT,ES,IE',
|
|
23
|
+
} = options;
|
|
24
|
+
|
|
25
|
+
const result = {
|
|
26
|
+
userInfo: null,
|
|
27
|
+
discoveredVideoAuthors: [],
|
|
28
|
+
discoveredCommentAuthors: [],
|
|
29
|
+
discoveredGuessAuthors: [],
|
|
30
|
+
discoveredFollowing: [],
|
|
31
|
+
discoveredFollowers: [],
|
|
32
|
+
collectedVideos: 0,
|
|
33
|
+
processed: false,
|
|
34
|
+
hasFollowData: false,
|
|
35
|
+
keepFollow: false,
|
|
36
|
+
locationCreated: null,
|
|
37
|
+
noVideo: false,
|
|
38
|
+
error: null,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
log(` 访问 @${username} 主页...`);
|
|
43
|
+
const homeUrl = `https://www.tiktok.com/@${username}`;
|
|
44
|
+
await retryWithBackoff(async () => {
|
|
45
|
+
await page.goto(homeUrl, { waitUntil: 'domcontentloaded', timeout: 30000 });
|
|
46
|
+
assertPageUrl(page, `@${username}`);
|
|
47
|
+
}, { log });
|
|
48
|
+
await page.waitForSelector('[class*="DivVideoList"]', { timeout: 10000 }).catch(() => {});
|
|
49
|
+
await delay(1000, 2000);
|
|
50
|
+
|
|
51
|
+
log(' 获取用户信息...');
|
|
52
|
+
const info = await getUserInfo(page);
|
|
53
|
+
if (info) {
|
|
54
|
+
result.userInfo = info;
|
|
55
|
+
log(` 用户: ${info.nickname || username} | 粉丝: ${info.followerCount || '-'} | 视频: ${info.videoCount || '-'}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const captcha = await detectCaptcha(page);
|
|
59
|
+
if (captcha && captcha.visible) {
|
|
60
|
+
log(`[验证码] @${username} 页面出现验证码`);
|
|
61
|
+
result.captchaDetected = true;
|
|
62
|
+
result.captchaStage = result.captchaStage || 'video-page';
|
|
63
|
+
result.captchaMessage = result.captchaMessage || '视频页出现验证码';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const isSeller = result.userInfo?.ttSeller === true;
|
|
67
|
+
const effectiveMaxVideos = isSeller ? globalMaxVideos : maxVideos;
|
|
68
|
+
if (isSeller) log(` 商家用户,视频采集数: ${effectiveMaxVideos}`);
|
|
69
|
+
const videoList = await collectVideos(page, username, effectiveMaxVideos, log);
|
|
70
|
+
const videoArray = videoList ? [...videoList.values()] : [];
|
|
71
|
+
result.collectedVideos = videoArray.length;
|
|
72
|
+
|
|
73
|
+
if (videoArray.length <= 0) {
|
|
74
|
+
result.processed = true;
|
|
75
|
+
result.noVideo = true;
|
|
76
|
+
const pageError = await detectPageError(page);
|
|
77
|
+
if (pageError) {
|
|
78
|
+
result.restricted = true;
|
|
79
|
+
log(` @${username} 页面受限(${pageError}),标记跳过`);
|
|
80
|
+
} else {
|
|
81
|
+
log(` @${username} 没有视频,标记已处理`);
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 从第一个视频获取 locationCreated
|
|
87
|
+
let locationCreated = null;
|
|
88
|
+
if (videoArray.length > 0) {
|
|
89
|
+
const firstVideo = videoArray[0];
|
|
90
|
+
const firstVideoUrl = firstVideo.href.startsWith('http')
|
|
91
|
+
? firstVideo.href
|
|
92
|
+
: `https://www.tiktok.com${firstVideo.href}`;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
locationCreated = await extractVideoLocation(firstVideoUrl);
|
|
96
|
+
} catch (e) {
|
|
97
|
+
log(` 获取视频国家失败: ${e.message}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
result.locationCreated = locationCreated || null;
|
|
102
|
+
log(` 国家: ${result.locationCreated || '未知'}`);
|
|
103
|
+
|
|
104
|
+
// 国家筛选
|
|
105
|
+
const locationList = (location || 'ES').split(',').map(s => s.trim().toUpperCase());
|
|
106
|
+
const isTargetLocation = locationList.includes(result.locationCreated?.toUpperCase?.() || result.locationCreated);
|
|
107
|
+
|
|
108
|
+
if (isTargetLocation) {
|
|
109
|
+
result.keepFollow = true;
|
|
110
|
+
log(` 国家匹配,获取关注/粉丝...`);
|
|
111
|
+
|
|
112
|
+
// 提取关注/粉丝
|
|
113
|
+
if (enableFollow) {
|
|
114
|
+
const loggedIn = await isLoggedIn(page);
|
|
115
|
+
if (!loggedIn) {
|
|
116
|
+
log(' [跳过] 获取关注/粉丝:未登录,请先登录 TikTok');
|
|
117
|
+
result.hasFollowData = false;
|
|
118
|
+
result.discoveredFollowing = [];
|
|
119
|
+
result.discoveredFollowers = [];
|
|
120
|
+
} else {
|
|
121
|
+
try {
|
|
122
|
+
const effectiveMaxFollowing = isSeller ? globalMaxFollowing : maxFollowing;
|
|
123
|
+
const effectiveMaxFollowers = isSeller ? globalMaxFollowers : maxFollowers;
|
|
124
|
+
if (isSeller) log(` 商家用户,关注采集: ${effectiveMaxFollowing}, 粉丝采集: ${effectiveMaxFollowers}`);
|
|
125
|
+
const { following, followers } = await extractFollowAndFollowers(
|
|
126
|
+
page, { maxFollowing: effectiveMaxFollowing, maxFollowers: effectiveMaxFollowers, log }
|
|
127
|
+
);
|
|
128
|
+
result.discoveredFollowing = following || [];
|
|
129
|
+
result.discoveredFollowers = followers || [];
|
|
130
|
+
result.hasFollowData = true;
|
|
131
|
+
log(` 关注: ${result.discoveredFollowing.length}, 粉丝: ${result.discoveredFollowers.length}`);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
log(` 关注/粉丝提取失败: ${e.message}`);
|
|
134
|
+
result.hasFollowData = false;
|
|
135
|
+
result.discoveredFollowing = [];
|
|
136
|
+
result.discoveredFollowers = [];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 携带视频列表供登记
|
|
142
|
+
result.videoList = videoArray;
|
|
143
|
+
} else {
|
|
144
|
+
// 国家不匹配
|
|
145
|
+
result.keepFollow = false;
|
|
146
|
+
result.discoveredFollowing = [];
|
|
147
|
+
result.discoveredFollowers = [];
|
|
148
|
+
result.hasFollowData = false;
|
|
149
|
+
log(` 国家不匹配,跳过`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
result.processed = true;
|
|
153
|
+
} catch (e) {
|
|
154
|
+
result.error = e.message;
|
|
155
|
+
result.errorStack = e.stack || '';
|
|
156
|
+
log(` [错误] ${e.message}`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export { processExplore };
|
|
@@ -1,114 +1,114 @@
|
|
|
1
|
-
export async function detectCaptcha(page) {
|
|
2
|
-
return page.evaluate(() => {
|
|
3
|
-
const container = document.querySelector('.captcha-verify-container');
|
|
4
|
-
if (!container) return null;
|
|
5
|
-
|
|
6
|
-
const r = container.getBoundingClientRect();
|
|
7
|
-
return {
|
|
8
|
-
exists: true,
|
|
9
|
-
visible: container.offsetParent !== null,
|
|
10
|
-
rect: {
|
|
11
|
-
x: Math.round(r.x),
|
|
12
|
-
y: Math.round(r.y),
|
|
13
|
-
w: Math.round(r.width),
|
|
14
|
-
h: Math.round(r.height),
|
|
15
|
-
},
|
|
16
|
-
};
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export async function closeCaptcha(page) {
|
|
21
|
-
return page.evaluate(() => {
|
|
22
|
-
const closeBtn = document.getElementById('captcha_close_button');
|
|
23
|
-
if (!closeBtn) return { success: false, reason: 'close button not found' };
|
|
24
|
-
|
|
25
|
-
closeBtn.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
|
|
26
|
-
closeBtn.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
|
|
27
|
-
closeBtn.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
28
|
-
|
|
29
|
-
return { success: true };
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export async function handleCaptcha(page, options = {}) {
|
|
34
|
-
const { waitMs = 2000 } = options;
|
|
35
|
-
|
|
36
|
-
const captcha = await detectCaptcha(page);
|
|
37
|
-
if (!captcha) return { detected: false, closed: false };
|
|
38
|
-
|
|
39
|
-
await new Promise(r => setTimeout(r, waitMs));
|
|
40
|
-
|
|
41
|
-
const result = await closeCaptcha(page);
|
|
42
|
-
if (!result.success) return { detected: true, closed: false, reason: result.reason };
|
|
43
|
-
|
|
44
|
-
await new Promise(r => setTimeout(r, 1000));
|
|
45
|
-
|
|
46
|
-
const stillThere = await detectCaptcha(page);
|
|
47
|
-
return { detected: true, closed: !stillThere };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export async function getIncognitoPage(browser, url, options = {}) {
|
|
51
|
-
const { waitMs = 3000 } = options;
|
|
52
|
-
const context = await browser.newContext();
|
|
53
|
-
const page = await context.newPage();
|
|
54
|
-
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
|
|
55
|
-
await new Promise(r => setTimeout(r, waitMs));
|
|
56
|
-
return { page, context };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export async function waitAndGetCaptcha(page, options = {}) {
|
|
60
|
-
const { waitMs = 180000, pollInterval = 5000, log } = options;
|
|
61
|
-
|
|
62
|
-
const captcha = await detectCaptcha(page);
|
|
63
|
-
if (!captcha) return { detected: false, resolved: false, waited: 0 };
|
|
64
|
-
|
|
65
|
-
if (log) log(' 检测到验证码,等待用户手动输入...');
|
|
66
|
-
|
|
67
|
-
const startTime = Date.now();
|
|
68
|
-
const deadline = startTime + waitMs;
|
|
69
|
-
|
|
70
|
-
while (Date.now() < deadline) {
|
|
71
|
-
await new Promise(r => setTimeout(r, pollInterval));
|
|
72
|
-
const remaining = await detectCaptcha(page);
|
|
73
|
-
if (!remaining) {
|
|
74
|
-
const waited = Math.round((Date.now() - startTime) / 1000);
|
|
75
|
-
if (log) log(` 验证码已解决(等待 ${waited}s)`);
|
|
76
|
-
return { detected: true, resolved: true, waited };
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const waited = Math.round(waitMs / 1000);
|
|
81
|
-
if (log) log(` 验证码等待超时(${waited}s),继续执行`);
|
|
82
|
-
|
|
83
|
-
// 超时后尝试关闭验证码弹窗
|
|
84
|
-
await closeCaptcha(page);
|
|
85
|
-
await new Promise(r => setTimeout(r, 1000));
|
|
86
|
-
|
|
87
|
-
return { detected: true, resolved: false, waited };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export async function safeClickComment(page, options = {}) {
|
|
91
|
-
const { waitMs = 3000 } = options;
|
|
92
|
-
|
|
93
|
-
// 点击评论
|
|
94
|
-
await page.evaluate(() => {
|
|
95
|
-
const all = document.querySelectorAll('button');
|
|
96
|
-
for (const el of all) {
|
|
97
|
-
if (/^评论$/.test(el.textContent?.trim()) && el.offsetParent !== null && el.getBoundingClientRect().width > 0) {
|
|
98
|
-
el.click();
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
await new Promise(r => setTimeout(r, waitMs));
|
|
105
|
-
|
|
106
|
-
// 检测并关闭验证码
|
|
107
|
-
const captcha = await detectCaptcha(page);
|
|
108
|
-
if (captcha) {
|
|
109
|
-
const result = await handleCaptcha(page);
|
|
110
|
-
return { clicked: true, captchaDetected: true, captchaClosed: result.closed };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return { clicked: true, captchaDetected: false, captchaClosed: false };
|
|
114
|
-
}
|
|
1
|
+
export async function detectCaptcha(page) {
|
|
2
|
+
return page.evaluate(() => {
|
|
3
|
+
const container = document.querySelector('.captcha-verify-container');
|
|
4
|
+
if (!container) return null;
|
|
5
|
+
|
|
6
|
+
const r = container.getBoundingClientRect();
|
|
7
|
+
return {
|
|
8
|
+
exists: true,
|
|
9
|
+
visible: container.offsetParent !== null,
|
|
10
|
+
rect: {
|
|
11
|
+
x: Math.round(r.x),
|
|
12
|
+
y: Math.round(r.y),
|
|
13
|
+
w: Math.round(r.width),
|
|
14
|
+
h: Math.round(r.height),
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function closeCaptcha(page) {
|
|
21
|
+
return page.evaluate(() => {
|
|
22
|
+
const closeBtn = document.getElementById('captcha_close_button');
|
|
23
|
+
if (!closeBtn) return { success: false, reason: 'close button not found' };
|
|
24
|
+
|
|
25
|
+
closeBtn.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
|
|
26
|
+
closeBtn.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
|
|
27
|
+
closeBtn.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
28
|
+
|
|
29
|
+
return { success: true };
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function handleCaptcha(page, options = {}) {
|
|
34
|
+
const { waitMs = 2000 } = options;
|
|
35
|
+
|
|
36
|
+
const captcha = await detectCaptcha(page);
|
|
37
|
+
if (!captcha) return { detected: false, closed: false };
|
|
38
|
+
|
|
39
|
+
await new Promise(r => setTimeout(r, waitMs));
|
|
40
|
+
|
|
41
|
+
const result = await closeCaptcha(page);
|
|
42
|
+
if (!result.success) return { detected: true, closed: false, reason: result.reason };
|
|
43
|
+
|
|
44
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
45
|
+
|
|
46
|
+
const stillThere = await detectCaptcha(page);
|
|
47
|
+
return { detected: true, closed: !stillThere };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function getIncognitoPage(browser, url, options = {}) {
|
|
51
|
+
const { waitMs = 3000 } = options;
|
|
52
|
+
const context = await browser.newContext();
|
|
53
|
+
const page = await context.newPage();
|
|
54
|
+
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
|
|
55
|
+
await new Promise(r => setTimeout(r, waitMs));
|
|
56
|
+
return { page, context };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function waitAndGetCaptcha(page, options = {}) {
|
|
60
|
+
const { waitMs = 180000, pollInterval = 5000, log } = options;
|
|
61
|
+
|
|
62
|
+
const captcha = await detectCaptcha(page);
|
|
63
|
+
if (!captcha) return { detected: false, resolved: false, waited: 0 };
|
|
64
|
+
|
|
65
|
+
if (log) log(' 检测到验证码,等待用户手动输入...');
|
|
66
|
+
|
|
67
|
+
const startTime = Date.now();
|
|
68
|
+
const deadline = startTime + waitMs;
|
|
69
|
+
|
|
70
|
+
while (Date.now() < deadline) {
|
|
71
|
+
await new Promise(r => setTimeout(r, pollInterval));
|
|
72
|
+
const remaining = await detectCaptcha(page);
|
|
73
|
+
if (!remaining) {
|
|
74
|
+
const waited = Math.round((Date.now() - startTime) / 1000);
|
|
75
|
+
if (log) log(` 验证码已解决(等待 ${waited}s)`);
|
|
76
|
+
return { detected: true, resolved: true, waited };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const waited = Math.round(waitMs / 1000);
|
|
81
|
+
if (log) log(` 验证码等待超时(${waited}s),继续执行`);
|
|
82
|
+
|
|
83
|
+
// 超时后尝试关闭验证码弹窗
|
|
84
|
+
await closeCaptcha(page);
|
|
85
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
86
|
+
|
|
87
|
+
return { detected: true, resolved: false, waited };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function safeClickComment(page, options = {}) {
|
|
91
|
+
const { waitMs = 3000 } = options;
|
|
92
|
+
|
|
93
|
+
// 点击评论
|
|
94
|
+
await page.evaluate(() => {
|
|
95
|
+
const all = document.querySelectorAll('button');
|
|
96
|
+
for (const el of all) {
|
|
97
|
+
if (/^评论$/.test(el.textContent?.trim()) && el.offsetParent !== null && el.getBoundingClientRect().width > 0) {
|
|
98
|
+
el.click();
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
await new Promise(r => setTimeout(r, waitMs));
|
|
105
|
+
|
|
106
|
+
// 检测并关闭验证码
|
|
107
|
+
const captcha = await detectCaptcha(page);
|
|
108
|
+
if (captcha) {
|
|
109
|
+
const result = await handleCaptcha(page);
|
|
110
|
+
return { clicked: true, captchaDetected: true, captchaClosed: result.closed };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { clicked: true, captchaDetected: false, captchaClosed: false };
|
|
114
|
+
}
|