dsh-fish-soul 0.18.0 → 0.18.2
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 +1 -1
- package/lib/channels/vendor/douyin-bridge/browser.mjs +70 -10
- package/lib/channels/vendor/douyin-bridge/douyin-comment.js +56 -19
- package/lib/douyin-browse.js +36 -19
- package/lib/douyin-social.js +381 -103
- package/lib/gateway-media.js +13 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -559,7 +559,16 @@ export class Browser {
|
|
|
559
559
|
})
|
|
560
560
|
}
|
|
561
561
|
|
|
562
|
-
/**
|
|
562
|
+
/**
|
|
563
|
+
* 在页面里执行 JS,返回结果
|
|
564
|
+
*
|
|
565
|
+
* @param {string} expression
|
|
566
|
+
* @param {object} opts
|
|
567
|
+
* @param {boolean} opts.awaitPromise
|
|
568
|
+
* @param {number} opts.timeoutMs 这次 eval 的上限(默认 60 秒 = send 的默认值)。
|
|
569
|
+
* 页面 renderer 卡死但 CDP 连接还在时,Runtime.evaluate **永不回包** ——
|
|
570
|
+
* 不给短超时时一把浏览器锁能被拖住好几分钟。
|
|
571
|
+
*/
|
|
563
572
|
async eval(expression, opts = {}) {
|
|
564
573
|
// 🔴 **绝不在这里启动浏览器**。
|
|
565
574
|
// 以前是 `if (!this.ready) await this.launch()` —— 而后台累积器每 2 秒
|
|
@@ -567,12 +576,13 @@ export class Browser {
|
|
|
567
576
|
// **窗口每隔 2 秒自己弹一次**。
|
|
568
577
|
// 要不要开浏览器只有一个地方能决定:桥的 ensureBrowser()。
|
|
569
578
|
if (!this.ready) throw new Error('浏览器没连上(连接已断),需要重新获取浏览器')
|
|
579
|
+
const timeoutMs = Number(opts.timeoutMs) > 0 ? Number(opts.timeoutMs) : 60000
|
|
570
580
|
const r = await this.send('Runtime.evaluate', {
|
|
571
581
|
expression,
|
|
572
582
|
returnByValue: true,
|
|
573
583
|
awaitPromise: opts.awaitPromise !== false,
|
|
574
584
|
userGesture: true,
|
|
575
|
-
}, this.sessionId)
|
|
585
|
+
}, this.sessionId, timeoutMs)
|
|
576
586
|
if (r.exceptionDetails) {
|
|
577
587
|
throw new Error(`页面 JS 报错:${r.exceptionDetails.exception?.description || r.exceptionDetails.text}`)
|
|
578
588
|
}
|
|
@@ -584,8 +594,13 @@ export class Browser {
|
|
|
584
594
|
*
|
|
585
595
|
* 🔴 Page.navigate 会**静默失败**(不报错,但页面停在 about:blank)。
|
|
586
596
|
* ⇒ 必须验证 location.href,不对就重试。
|
|
597
|
+
*
|
|
598
|
+
* @param {object} opts
|
|
599
|
+
* @param {number} opts.timeoutMs **整个导航**的上限(毫秒);不给 = 不设限(老行为)。
|
|
600
|
+
* 页面卡死时验证用的 location.href 读不回(Runtime.evaluate 永不回包)。
|
|
601
|
+
* 这个上限**包住整段导航**:重试间隔、导航后等待、成功后的 settle 都算在里面。
|
|
587
602
|
*/
|
|
588
|
-
async goto(url, { settleMs = 9000, retries = 2 } = {}) {
|
|
603
|
+
async goto(url, { settleMs = 9000, retries = 2, timeoutMs = 0 } = {}) {
|
|
589
604
|
// 🔴 同 eval():不在这里启动浏览器(见上面的说明)
|
|
590
605
|
if (!this.ready) throw new Error('浏览器没连上(连接已断),需要重新获取浏览器')
|
|
591
606
|
this.events = []
|
|
@@ -593,23 +608,65 @@ export class Browser {
|
|
|
593
608
|
this.seq = 0
|
|
594
609
|
this.messageResponses.clear()
|
|
595
610
|
|
|
611
|
+
// 🔴 整个导航的上限(给了才生效)。页面 renderer 卡死但 CDP 连接还在时,
|
|
612
|
+
// 验证用的 location.href 读不回(Runtime.evaluate 永不回包),
|
|
613
|
+
// 没有上限就是 60 秒 × 重试次数。
|
|
614
|
+
const bounded = Number(timeoutMs) > 0
|
|
615
|
+
const end = bounded ? Date.now() + Number(timeoutMs) : 0
|
|
616
|
+
/** 这次还能花多少毫秒;不设限时返回 Infinity(各步都按老行为走) */
|
|
617
|
+
const left = () => (bounded ? end - Date.now() : Infinity)
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* 睡一段,但不许睡过预算:睡 min(原值, 剩余)。
|
|
621
|
+
*
|
|
622
|
+
* 🔴 以前这几处是**裸 sleep** —— 导航请求本身有上限,等待却不管:
|
|
623
|
+
* 导航请求超时后还要再睡 800ms × 重试次数,导航成功后还要再睡 settle
|
|
624
|
+
* (settleMs 7000 时是 4500ms)才返回。
|
|
625
|
+
* openWork 把这个上限当整操作 deadline 的一部分 ⇒ 整操作能超过 30 秒
|
|
626
|
+
* (2026-11-15 独立审查打回的阻断项)。
|
|
627
|
+
*
|
|
628
|
+
* 不设限时(left() = Infinity)时就是原来的 sleep(ms),老行为完全不变。
|
|
629
|
+
*
|
|
630
|
+
* 🔴 true = **醒来时**仍在预算内。只看入睡前的 remain 会漏判:
|
|
631
|
+
* 定时器可能迟到(事件循环被占 / 系统休眠 / 后台标签页被节流),
|
|
632
|
+
* 入睡时预算够 ≠ 醒来时预算还在 ⇒ 睡完必须重新核对 left()。
|
|
633
|
+
*
|
|
634
|
+
* @returns {boolean} true = 睡够了且醒来时仍在预算内;false = 没睡够 / 醒来时已超预算
|
|
635
|
+
*/
|
|
636
|
+
const sleepWithin = async (ms) => {
|
|
637
|
+
const remain = left()
|
|
638
|
+
if (remain <= 0) return false
|
|
639
|
+
if (remain >= ms) {
|
|
640
|
+
await sleep(ms)
|
|
641
|
+
return left() > 0 // 睡醒后重新核对:定时器可能迟到
|
|
642
|
+
}
|
|
643
|
+
await sleep(remain)
|
|
644
|
+
return false
|
|
645
|
+
}
|
|
646
|
+
|
|
596
647
|
let lastHref = ''
|
|
597
648
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
649
|
+
if (bounded && left() <= 0) throw new Error(`导航超时(${timeoutMs}ms):${url}`)
|
|
650
|
+
// 这次导航能花多少:整操作预算的剩余时间(没给预算时 = 15 秒,老行为不变)
|
|
651
|
+
const navTimeout = Math.min(15000, left())
|
|
598
652
|
try {
|
|
599
653
|
// 导航卡死时不能无限等:CDP 的 Page.navigate 要等对话框被处理才会回包,
|
|
600
654
|
// 对话框一旦没人点,这里会挂满 send() 默认的 60 秒才报错,重试也跟着等。
|
|
601
655
|
// 导航单独给 15 秒上限(超时由 send 自己清 pending 和计时器,不会空挂),
|
|
602
656
|
// 超时当这次失败,走下面的重试 / 报错。
|
|
603
|
-
await this.send('Page.navigate', { url }, this.sessionId,
|
|
657
|
+
await this.send('Page.navigate', { url }, this.sessionId, navTimeout)
|
|
604
658
|
} catch (e) {
|
|
605
659
|
const timedOut = /超时/.test(e.message)
|
|
606
|
-
if (attempt >= retries) throw new Error(timedOut ?
|
|
607
|
-
await
|
|
660
|
+
if (attempt >= retries) throw new Error(timedOut ? `导航请求超时(${Math.round(navTimeout)}ms):页面可能被原生确认框挡住` : `导航请求失败:${e.message}`)
|
|
661
|
+
await sleepWithin(800)
|
|
608
662
|
continue
|
|
609
663
|
}
|
|
610
664
|
|
|
611
|
-
|
|
612
|
-
|
|
665
|
+
// 🔴 导航后的首次等待也要看结果:睡醒时已过 deadline ⇒ 别再往下走
|
|
666
|
+
// (再 eval 只会白等,页面多半也是半成品)。
|
|
667
|
+
const waited = await sleepWithin(Math.min(2500, Math.max(800, settleMs / 3)))
|
|
668
|
+
if (bounded && !(waited && left() > 0)) throw new Error(`导航超时(${timeoutMs}ms):导航后等待超预算,${url}`)
|
|
669
|
+
lastHref = await this.eval('location.href', bounded ? { timeoutMs: Math.max(1, left()) } : {}).catch(() => '')
|
|
613
670
|
|
|
614
671
|
const ok = lastHref
|
|
615
672
|
&& lastHref !== 'about:blank'
|
|
@@ -617,12 +674,15 @@ export class Browser {
|
|
|
617
674
|
&& lastHref !== 'chrome://newtab/'
|
|
618
675
|
|
|
619
676
|
if (ok) {
|
|
620
|
-
|
|
677
|
+
// 🔴 成功返回前查 deadline:settle 没睡完 / 睡醒时已过 deadline,
|
|
678
|
+
// 都不能当作成功(页面可能还没渲染完,调用方会拿着半成品继续)。
|
|
679
|
+
const settled = await sleepWithin(Math.max(0, settleMs - 2500))
|
|
680
|
+
if (bounded && !(settled && left() > 0)) throw new Error(`导航超时(${timeoutMs}ms):settle 没睡完,${url}`)
|
|
621
681
|
return { url: lastHref, attempt }
|
|
622
682
|
}
|
|
623
683
|
if (attempt < retries) {
|
|
624
684
|
this.log(`导航没生效(还在 ${lastHref || 'about:blank'}),重试…`)
|
|
625
|
-
await
|
|
685
|
+
await sleepWithin(1000)
|
|
626
686
|
}
|
|
627
687
|
}
|
|
628
688
|
|
|
@@ -27,7 +27,21 @@
|
|
|
27
27
|
// `commentBridgeReady()` 比的是版本号,号没变就认为「已装好」,于是线上一直跑旧代码。
|
|
28
28
|
// 2026-11-08 就踩过:改了 collect 的端点,验证时抓到的请求还是旧端点。
|
|
29
29
|
// v2:收藏改走 aweme/collect(原来猜的 commit/collect 是 404 Janus)
|
|
30
|
-
|
|
30
|
+
// v5:postComment 的本地校验(缺作品 id / 正文图片都空)改成 NOT_ATTEMPTED —— 请求根本没发出去
|
|
31
|
+
export const COMMENT_BRIDGE_VERSION = 5;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 写操作状态(页面桥和 Node 侧共用同一套值)。
|
|
35
|
+
*
|
|
36
|
+
* NOT_ATTEMPTED —— 写操作**根本没发出去**(桥没装上 / 图没传上去):
|
|
37
|
+
* 调用方可以退回 DOM 点击重来。
|
|
38
|
+
* ATTEMPTED_UNKNOWN —— 写操作**可能已经发出去了**(超时 / 断连 / 抖音没回评论 id):
|
|
39
|
+
* 调用方绝不能再发一次(再发就是重复评论)。
|
|
40
|
+
*
|
|
41
|
+
* ⚠️ 靠状态字段判断,不要拿错误文本匹配 —— 文本会变,状态不会。
|
|
42
|
+
*/
|
|
43
|
+
export const WRITE_NOT_ATTEMPTED = 'NOT_ATTEMPTED';
|
|
44
|
+
export const WRITE_ATTEMPTED_UNKNOWN = 'ATTEMPTED_UNKNOWN';
|
|
31
45
|
|
|
32
46
|
/**
|
|
33
47
|
* 页面内脚本。挂在 `window.__fishComment` 上。
|
|
@@ -43,6 +57,9 @@ export const COMMENT_BRIDGE_SOURCE = String.raw`
|
|
|
43
57
|
|
|
44
58
|
const ok = (o) => Object.assign({ ok: true }, o);
|
|
45
59
|
const bad = (e, extra) => Object.assign({ ok: false, error: String((e && e.message) || e) }, extra || {});
|
|
60
|
+
// 写操作状态(和 Node 侧同名)
|
|
61
|
+
const WRITE_NOT_ATTEMPTED = '${WRITE_NOT_ATTEMPTED}';
|
|
62
|
+
const WRITE_ATTEMPTED_UNKNOWN = '${WRITE_ATTEMPTED_UNKNOWN}';
|
|
46
63
|
|
|
47
64
|
/** 页面加载时刻 —— celltime 两个字段都从它算起(实测抖音自己也是这么算的)。 */
|
|
48
65
|
const PAGE_LOAD_TIME = Date.now();
|
|
@@ -277,13 +294,17 @@ export const COMMENT_BRIDGE_SOURCE = String.raw`
|
|
|
277
294
|
* @param {Array} [o.images] [{uri,width,height,format}]
|
|
278
295
|
*/
|
|
279
296
|
async postComment(o) {
|
|
297
|
+
// ⚠️ 分两段:
|
|
298
|
+
// · 本地校验没过 → 请求**根本没发出去**(NOT_ATTEMPTED),调用方可以退回 DOM;
|
|
299
|
+
// · 请求已经发出去了 → 后面的一切(超时 / 断连 / 抖音没回评论 id)都算
|
|
300
|
+
// 「可能已经发出去了」(ATTEMPTED_UNKNOWN),调用方绝不能再发一次。
|
|
280
301
|
try {
|
|
281
302
|
const opt = o || {};
|
|
282
303
|
const awemeId = String(opt.awemeId || '');
|
|
283
|
-
if (!awemeId) return { ok: false, error: '没给作品 id' };
|
|
304
|
+
if (!awemeId) return { ok: false, writeState: WRITE_NOT_ATTEMPTED, error: '没给作品 id' };
|
|
284
305
|
const images = Array.isArray(opt.images) ? opt.images : [];
|
|
285
306
|
const text = String(opt.text || '');
|
|
286
|
-
if (!text && !images.length) return { ok: false, error: '正文和图片都是空的' };
|
|
307
|
+
if (!text && !images.length) return { ok: false, writeState: WRITE_NOT_ATTEMPTED, error: '正文和图片都是空的' };
|
|
287
308
|
|
|
288
309
|
const now = Date.now();
|
|
289
310
|
const sendCelltime = String(Math.floor((now - PAGE_LOAD_TIME) / 1000) * 1000);
|
|
@@ -317,12 +338,13 @@ export const COMMENT_BRIDGE_SOURCE = String.raw`
|
|
|
317
338
|
const uifid = cookie('UIFID') || cookie('UIFID_TEMP');
|
|
318
339
|
if (uifid) extra.uifid = uifid;
|
|
319
340
|
const r = await api('/aweme/v1/web/comment/publish/', extra, b.toString());
|
|
320
|
-
|
|
341
|
+
// 请求已经发出去了(可能服务端已经落库),后面的结果一律「不明」
|
|
342
|
+
if (!r.ok) return Object.assign({ writeState: WRITE_ATTEMPTED_UNKNOWN }, r);
|
|
321
343
|
const cid = r.data && r.data.comment && r.data.comment.cid;
|
|
322
|
-
if (!cid) return { ok: false, error: '抖音没回评论 id', raw: r.data };
|
|
344
|
+
if (!cid) return { ok: false, writeState: WRITE_ATTEMPTED_UNKNOWN, error: '抖音没回评论 id', raw: r.data };
|
|
323
345
|
// cid 是 19 位整数,JS Number 只能精确到 16 位 —— JSON 里若是数字必须 toFixed(0) 保精度
|
|
324
346
|
return ok({ cid: typeof cid === 'number' ? cid.toFixed(0) : String(cid) });
|
|
325
|
-
} catch (e) { return
|
|
347
|
+
} catch (e) { return { ok: false, writeState: WRITE_ATTEMPTED_UNKNOWN, error: String((e && e.message) || e) }; }
|
|
326
348
|
},
|
|
327
349
|
|
|
328
350
|
/**
|
|
@@ -420,30 +442,30 @@ export const COMMENT_BRIDGE_SOURCE = String.raw`
|
|
|
420
442
|
* 在当前页面装上桥(不依赖 preload —— 当前页立刻能用)。
|
|
421
443
|
* 返回 { ok, error? }。
|
|
422
444
|
*/
|
|
423
|
-
export async function installCommentBridge(browser) {
|
|
445
|
+
export async function installCommentBridge(browser, { timeoutMs } = {}) {
|
|
424
446
|
try {
|
|
425
447
|
browser.addPreloadScript(COMMENT_BRIDGE_SOURCE);
|
|
426
448
|
} catch { /* 老版本 Browser 没这方法 */ }
|
|
427
449
|
try {
|
|
428
|
-
await browser.send('Page.addScriptToEvaluateOnNewDocument', { source: COMMENT_BRIDGE_SOURCE }, browser.sessionId);
|
|
450
|
+
await browser.send('Page.addScriptToEvaluateOnNewDocument', { source: COMMENT_BRIDGE_SOURCE }, browser.sessionId, timeoutMs);
|
|
429
451
|
} catch { /* 装了 preload 就够 */ }
|
|
430
452
|
try {
|
|
431
|
-
await browser.eval(COMMENT_BRIDGE_SOURCE);
|
|
453
|
+
await browser.eval(COMMENT_BRIDGE_SOURCE, { timeoutMs });
|
|
432
454
|
} catch (error) {
|
|
433
455
|
return { ok: false, error: String(error instanceof Error ? error.message : error) };
|
|
434
456
|
}
|
|
435
457
|
// ⚠️ eval 没抛异常 ≠ 桥真的装上了:页面如果不是真抖音(比如测试用的本地假站),
|
|
436
458
|
// eval 会静静地什么都没做。所以装完回头验一次版本号。
|
|
437
|
-
if (!(await commentBridgeReady(browser))) {
|
|
459
|
+
if (!(await commentBridgeReady(browser, { timeoutMs }))) {
|
|
438
460
|
return { ok: false, error: '装完验不到桥(页面里没有 window.__fishComment)' };
|
|
439
461
|
}
|
|
440
462
|
return { ok: true };
|
|
441
463
|
}
|
|
442
464
|
|
|
443
465
|
/** 桥是否在位。 */
|
|
444
|
-
export async function commentBridgeReady(browser) {
|
|
466
|
+
export async function commentBridgeReady(browser, { timeoutMs } = {}) {
|
|
445
467
|
try {
|
|
446
|
-
const v = await browser.eval('window.__fishComment ? window.__fishComment.version : 0');
|
|
468
|
+
const v = await browser.eval('window.__fishComment ? window.__fishComment.version : 0', { timeoutMs });
|
|
447
469
|
return Number(v) === COMMENT_BRIDGE_VERSION;
|
|
448
470
|
} catch {
|
|
449
471
|
return false;
|
|
@@ -455,21 +477,36 @@ export async function commentBridgeReady(browser) {
|
|
|
455
477
|
* @param {import('./browser.mjs').Browser} browser
|
|
456
478
|
* @param {string} method 桥上的方法名
|
|
457
479
|
* @param {object} args 参数
|
|
458
|
-
* @
|
|
480
|
+
* @param {object} [opts]
|
|
481
|
+
* @param {boolean} [opts.write] 这是写操作吗?写操作的结果不明(超时 / 断连 / 页面没回结果)
|
|
482
|
+
* 一律标成 ATTEMPTED_UNKNOWN —— 调用方绝不能再发一次。
|
|
483
|
+
* @param {number} [opts.timeoutMs] 这次桥调用的上限(毫秒)。不给 = eval 的默认 60 秒。
|
|
484
|
+
* 页面卡死时 Runtime.evaluate 永不回包,不给上限就是 60 秒。
|
|
485
|
+
* @returns {Promise<{ok: boolean, error?: string, writeState?: string}>}
|
|
459
486
|
*/
|
|
460
|
-
export async function callBridge(browser, method, args = {}) {
|
|
487
|
+
export async function callBridge(browser, method, args = {}, opts = {}) {
|
|
488
|
+
const write = opts.write === true;
|
|
461
489
|
const expr = `window.__fishComment && window.__fishComment.${method}
|
|
462
490
|
? window.__fishComment.${method}(${JSON.stringify(args)})
|
|
463
|
-
: Promise.resolve({ ok: false, error: '桥没装上(window.__fishComment 不存在)' })`;
|
|
491
|
+
: Promise.resolve({ ok: false, writeState: '${WRITE_NOT_ATTEMPTED}', error: '桥没装上(window.__fishComment 不存在)' })`;
|
|
464
492
|
let r;
|
|
465
493
|
try {
|
|
466
|
-
r = await browser.eval(expr);
|
|
494
|
+
r = await browser.eval(expr, { timeoutMs: opts.timeoutMs });
|
|
467
495
|
} catch (error) {
|
|
468
496
|
// CDP 断了 / 页面崩了 —— 抛给调用方没意义,统一转成 ok:false
|
|
469
|
-
|
|
497
|
+
const out = { ok: false, error: '桥调用失败:' + String(error instanceof Error ? error.message : error) };
|
|
498
|
+
// 写操作:CDP 断了不代表没发出去(请求可能已经发出去了,只是结果没回来)
|
|
499
|
+
if (write) out.writeState = WRITE_ATTEMPTED_UNKNOWN;
|
|
500
|
+
return out;
|
|
501
|
+
}
|
|
502
|
+
if (r && typeof r === 'object' && typeof r.ok === 'boolean') {
|
|
503
|
+
// 写操作:页面侧没给状态就按「可能已经发出去了」处理(宁可这次没回,也别重复发)
|
|
504
|
+
if (write && r.ok === false && !r.writeState) return Object.assign({}, r, { writeState: WRITE_ATTEMPTED_UNKNOWN });
|
|
505
|
+
return r;
|
|
470
506
|
}
|
|
471
|
-
|
|
472
|
-
|
|
507
|
+
const out = { ok: false, error: '桥调用没返回结果:' + String(JSON.stringify(r) ?? r).slice(0, 200) };
|
|
508
|
+
if (write) out.writeState = WRITE_ATTEMPTED_UNKNOWN;
|
|
509
|
+
return out;
|
|
473
510
|
}
|
|
474
511
|
|
|
475
512
|
/**
|
package/lib/douyin-browse.js
CHANGED
|
@@ -103,8 +103,9 @@ export class DouyinBrowser {
|
|
|
103
103
|
* @param {[number, number]} [deps.dwellMs] 推荐页每条看多久(真实毫秒)
|
|
104
104
|
* @param {(opts: {browser: object, id: string, commentLimit?: number}) => Promise<object>} [deps.readDetail]
|
|
105
105
|
* @param {import('./douyin-http.js').DouyinHttp} [deps.http] 纯 HTTP 客户端(http 模式用)
|
|
106
|
+
* @param {object} [deps.timing] 页面操作的时间上限(eval / send / next 的总预算)
|
|
106
107
|
*/
|
|
107
|
-
constructor({ db, clock, host, getConfig, callLlm, persona, worker, log, dwellMs = [5000, 20000], readDetail = detail, settleMs = 8000, fetchImpl, share, note, http, frames }) {
|
|
108
|
+
constructor({ db, clock, host, getConfig, callLlm, persona, worker, log, dwellMs = [5000, 20000], readDetail = detail, settleMs = 8000, fetchImpl, share, note, http, frames, timing = {} }) {
|
|
108
109
|
this.share = share ?? null;
|
|
109
110
|
this.note = note ?? null;
|
|
110
111
|
this.framesOf = frames ?? null; // 抽九宫格看画面(可缺:没有就只读文字)
|
|
@@ -124,6 +125,7 @@ export class DouyinBrowser {
|
|
|
124
125
|
this.running = false;
|
|
125
126
|
this.httpBroken = false; // 这轮试过纯 HTTP 不行 → 退回浏览器
|
|
126
127
|
this.described = new Map();
|
|
128
|
+
this.tm = { evalTimeoutMs: 12_000, nextBudgetMs: 30_000, ...timing }; // 页面卡死时不能挂死或霸住浏览器锁
|
|
127
129
|
}
|
|
128
130
|
|
|
129
131
|
cfg() {
|
|
@@ -215,32 +217,47 @@ export class DouyinBrowser {
|
|
|
215
217
|
return lo + Math.floor(Math.random() * Math.max(0, hi - lo));
|
|
216
218
|
}
|
|
217
219
|
|
|
218
|
-
async activeVideo(b) {
|
|
219
|
-
const r = await b.eval(ACTIVE_VIDEO_JS).catch(() => null);
|
|
220
|
+
async activeVideo(b, timeoutMs = this.tm.evalTimeoutMs) {
|
|
221
|
+
const r = await b.eval(ACTIVE_VIDEO_JS, { timeoutMs }).catch(() => null);
|
|
220
222
|
return r && r.ok ? r : null;
|
|
221
223
|
}
|
|
222
224
|
|
|
223
225
|
/** 划到下一条:先点"下一条"箭头,不行按 ↓,再不行滚轮;确认 id 变了。 */
|
|
224
226
|
async next(b, prevId) {
|
|
227
|
+
// 页面卡死时 eval 永不回包 ⇒ 每次 eval 都有短超时,整轮还有总预算
|
|
228
|
+
//(只给单次超时不够:8 轮 × 60 秒照样霸着浏览器独占锁)。
|
|
229
|
+
const end = Date.now() + this.tm.nextBudgetMs;
|
|
230
|
+
const left = () => end - Date.now();
|
|
231
|
+
const timeout = () => Math.max(1, Math.min(this.tm.evalTimeoutMs, left()));
|
|
232
|
+
// 预算耗尽后**不能再发起** eval / send / 睡眠:max(1,…) 只防 0,不是通行证。
|
|
233
|
+
// 否则点「下一条」失败后,键盘 / 滚轮分支会各在预算之外再等一次超时,照样霸锁。
|
|
234
|
+
const expired = () => left() <= 0;
|
|
225
235
|
const changed = async () => {
|
|
226
236
|
for (let i = 0; i < 8; i++) {
|
|
227
|
-
|
|
228
|
-
|
|
237
|
+
if (expired()) return false;
|
|
238
|
+
await sleep(Math.min(400, Math.max(0, left())));
|
|
239
|
+
if (expired()) return false;
|
|
240
|
+
const v = await this.activeVideo(b, timeout());
|
|
229
241
|
if (v && v.id && v.id !== prevId) return true;
|
|
230
242
|
}
|
|
231
243
|
return false;
|
|
232
244
|
};
|
|
233
|
-
if (
|
|
245
|
+
if (expired()) return false;
|
|
246
|
+
if (await b.eval(CLICK_NEXT_JS, { timeoutMs: timeout() }).catch(() => false) && await changed()) return true;
|
|
247
|
+
if (expired()) return false;
|
|
234
248
|
try {
|
|
235
249
|
for (const type of ['keyDown', 'keyUp']) {
|
|
236
|
-
|
|
250
|
+
if (expired()) return false;
|
|
251
|
+
await b.send('Input.dispatchKeyEvent', { type, key: 'ArrowDown', code: 'ArrowDown', windowsVirtualKeyCode: 40, nativeVirtualKeyCode: 40 }, b.sessionId, timeout());
|
|
237
252
|
}
|
|
238
253
|
if (await changed()) return true;
|
|
239
254
|
} catch { /* 换滚轮 */ }
|
|
255
|
+
if (expired()) return false;
|
|
240
256
|
try {
|
|
241
|
-
const m = await b.send('Page.getLayoutMetrics', {}, b.sessionId);
|
|
257
|
+
const m = await b.send('Page.getLayoutMetrics', {}, b.sessionId, timeout());
|
|
242
258
|
const vp = m?.cssVisualViewport ?? {};
|
|
243
|
-
|
|
259
|
+
if (expired()) return false;
|
|
260
|
+
await b.send('Input.dispatchMouseEvent', { type: 'mouseWheel', x: Math.round((vp.clientWidth ?? 800) / 2), y: Math.round((vp.clientHeight ?? 600) / 2), deltaX: 0, deltaY: 600 }, b.sessionId, timeout());
|
|
244
261
|
if (await changed()) return true;
|
|
245
262
|
} catch { /* 划不动 */ }
|
|
246
263
|
return false;
|
|
@@ -257,13 +274,13 @@ export class DouyinBrowser {
|
|
|
257
274
|
const r = el.getBoundingClientRect();
|
|
258
275
|
if (r.width < 2 || r.height < 2) return null;
|
|
259
276
|
return { x: r.x + r.width / 2, y: r.y + r.height / 2, w: r.width, h: r.height };
|
|
260
|
-
})()
|
|
277
|
+
})()`, { timeoutMs: this.tm.evalTimeoutMs }).catch(() => null);
|
|
261
278
|
if (!pos || typeof pos.x !== 'number') return false;
|
|
262
|
-
const m = await b.send('Page.getLayoutMetrics', {}, b.sessionId).catch(() => null);
|
|
279
|
+
const m = await b.send('Page.getLayoutMetrics', {}, b.sessionId, this.tm.evalTimeoutMs).catch(() => null);
|
|
263
280
|
const vp = m?.cssVisualViewport ?? m?.cssLayoutViewport ?? {};
|
|
264
281
|
if (pos.x < 0 || pos.y < 0 || pos.x > (vp.clientWidth ?? 0) || pos.y > (vp.clientHeight ?? 0)) return false;
|
|
265
282
|
for (const type of ['mouseMoved', 'mousePressed', 'mouseReleased']) {
|
|
266
|
-
await b.send('Input.dispatchMouseEvent', { type, x: Math.round(pos.x), y: Math.round(pos.y), button: 'left', clickCount: type === 'mouseMoved' ? 0 : 1 }, b.sessionId);
|
|
283
|
+
await b.send('Input.dispatchMouseEvent', { type, x: Math.round(pos.x), y: Math.round(pos.y), button: 'left', clickCount: type === 'mouseMoved' ? 0 : 1 }, b.sessionId, this.tm.evalTimeoutMs);
|
|
267
284
|
}
|
|
268
285
|
await sleep(3000);
|
|
269
286
|
return await this.activeVideo(b) !== null;
|
|
@@ -445,8 +462,8 @@ export class DouyinBrowser {
|
|
|
445
462
|
const b = this.host?.running === true ? this.host?.browser : null;
|
|
446
463
|
if (b && typeof b.eval === 'function') {
|
|
447
464
|
try {
|
|
448
|
-
if (!(await commentBridgeReady(b))) await installCommentBridge(b);
|
|
449
|
-
const r = await callBridge(b, 'collect', { awemeId: v.id, collect: true });
|
|
465
|
+
if (!(await commentBridgeReady(b, { timeoutMs: this.tm.evalTimeoutMs }))) await installCommentBridge(b, { timeoutMs: this.tm.evalTimeoutMs });
|
|
466
|
+
const r = await callBridge(b, 'collect', { awemeId: v.id, collect: true }, { timeoutMs: this.tm.evalTimeoutMs });
|
|
450
467
|
if (r?.ok === true) {
|
|
451
468
|
collected = r.collected !== false;
|
|
452
469
|
done = true;
|
|
@@ -520,8 +537,8 @@ export class DouyinBrowser {
|
|
|
520
537
|
// 两条都走会让同一条视频被赞两次(一个走接口、一个走 UI)。
|
|
521
538
|
let bridgeRan = false;
|
|
522
539
|
try {
|
|
523
|
-
if (!(await commentBridgeReady(b))) await installCommentBridge(b);
|
|
524
|
-
const r = await callBridge(b, 'digg', { awemeId: v.id, diggType: 1 });
|
|
540
|
+
if (!(await commentBridgeReady(b, { timeoutMs: this.tm.evalTimeoutMs }))) await installCommentBridge(b, { timeoutMs: this.tm.evalTimeoutMs });
|
|
541
|
+
const r = await callBridge(b, 'digg', { awemeId: v.id, diggType: 1 }, { timeoutMs: this.tm.evalTimeoutMs });
|
|
525
542
|
// 这两种「失败」说明的是**这个页面用不了桥**(不是"没赞上"),要退回点按钮:
|
|
526
543
|
// - 桥没装上(window.__fishComment 不存在)
|
|
527
544
|
// - 桥发出了请求但拿回的不是 JSON(页面不是真抖音,比如测试用的本地假站)
|
|
@@ -540,7 +557,7 @@ export class DouyinBrowser {
|
|
|
540
557
|
this.log('warn', `[fish-soul] 抖音点赞接口桥失败:${errText(error)},退回点按钮`);
|
|
541
558
|
}
|
|
542
559
|
if (!bridgeRan) {
|
|
543
|
-
try { liked = await b.eval(CLICK_LIKE_JS) === true; } catch { liked = false; }
|
|
560
|
+
try { liked = await b.eval(CLICK_LIKE_JS, { timeoutMs: this.tm.evalTimeoutMs }) === true; } catch { liked = false; }
|
|
544
561
|
}
|
|
545
562
|
}
|
|
546
563
|
// 收藏:和点赞同一族(commit/* 写接口,必须页面内发)。
|
|
@@ -548,8 +565,8 @@ export class DouyinBrowser {
|
|
|
548
565
|
let collected = false;
|
|
549
566
|
if (wantCollect) {
|
|
550
567
|
try {
|
|
551
|
-
if (!(await commentBridgeReady(b))) await installCommentBridge(b);
|
|
552
|
-
const r = await callBridge(b, 'collect', { awemeId: v.id, collect: true });
|
|
568
|
+
if (!(await commentBridgeReady(b, { timeoutMs: this.tm.evalTimeoutMs }))) await installCommentBridge(b, { timeoutMs: this.tm.evalTimeoutMs });
|
|
569
|
+
const r = await callBridge(b, 'collect', { awemeId: v.id, collect: true }, { timeoutMs: this.tm.evalTimeoutMs });
|
|
553
570
|
if (r?.ok === true) {
|
|
554
571
|
collected = r.collected !== false;
|
|
555
572
|
if (r.confirmed === false) this.log('info', '[fish-soul] 抖音收藏提交了(返回里没有 collect_stat,按成功算)');
|