@cloud411716/fancy-webnovel 4.21.0 → 4.22.1

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.
@@ -201,6 +201,7 @@ id / kind / subkind / name / tags / genres / note / source
201
201
  滚动、懒加载或页面动画截到残缺图片。
202
202
  - 图文作品的轮播可以混合图片页和视频页。视频页会下载该页短视频,15 秒以内每 0.2 秒采样、15–60 秒每 0.5 秒采样,
203
203
  再按稳定画面段选帧并拼成一张关键帧图交给 LLM;这样快速切换的画面和动态字幕/标语不会被封面漏掉。
204
+ - 图文轮播必须以页面计数器 `当前页/总页数` 为准:页面刚打开时 DOM 里可能只挂载 1–2 个 `.dySwiperSlide`,不能当作总页数。插件会以第一个 slide 所在的播放器容器为锚点,按 `X/Y` 文本定位计数器,并从计数器相邻元素取得上一页/下一页按钮,不依赖抖音会变化的哈希类名;逐页推进并等待当前页内容切换完成,确保 `note.json` 记录所有页。
204
205
  - 独立视频作品会先尝试下载原片并用 ffmpeg 抽帧;下载失败时退化为播放器元素截图。
205
206
  视频时长超过 1 分钟直接跳过;前 5 秒与网文创作无关也跳过。
206
207
  - 场景切换点本身常是过渡虚影。插件会在密集采样后用 ffmpeg blurdetect 给每帧打分,在每个稳定画面段的清晰中段选一帧,避免把文字尚未完全出现的模糊帧交给 LLM。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud411716/fancy-webnovel",
3
- "version": "4.21.0",
3
+ "version": "4.22.1",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -63,7 +63,9 @@ async function activeSlideInfo(page) {
63
63
  if (String(videoSrc).startsWith('//')) videoSrc = 'https:' + videoSrc;
64
64
  duration = Number.isFinite(video.duration) && video.duration > 0 ? video.duration : null;
65
65
  }
66
- const img = el.querySelector('img[src*="tplv-dy-aweme-images"], img[src*="aweme-images"]') || el.querySelector('img');
66
+ const img = el.querySelector('img[src*="tplv-dy-aweme-images"], img[src*="aweme-images"]');
67
+ const anyImg = el.querySelector('img');
68
+ const fallbackImg = anyImg && anyImg.src && !/douyinDefault/i.test(anyImg.src) ? anyImg.src : '';
67
69
  return {
68
70
  domIndex: active.index,
69
71
  x: Math.round(active.rect.x),
@@ -73,11 +75,77 @@ async function activeSlideInfo(page) {
73
75
  hasVideo: Boolean(videoSrc && !videoSrc.startsWith('blob:')),
74
76
  videoSrc,
75
77
  duration,
76
- imageSrc: img && img.src ? img.src : '',
78
+ // Never fall back to Douyin's placeholder image: if the real image has
79
+ // not loaded yet, the caller will wait or take an element screenshot.
80
+ imageSrc: img && img.src ? img.src : fallbackImg,
77
81
  };
78
82
  });
79
83
  }
80
84
 
85
+ async function readSlidePosition(page) {
86
+ return await page.evaluate(() => {
87
+ const center = (el) => {
88
+ if (!el) return null;
89
+ const rect = el.getBoundingClientRect();
90
+ if (rect.width <= 0 || rect.height <= 0) return null;
91
+ return {
92
+ x: Math.round(rect.x + rect.width / 2),
93
+ y: Math.round(rect.y + rect.height / 2),
94
+ };
95
+ };
96
+
97
+ // Anchor on the first slide's container instead of a hashed CSS class.
98
+ // Douyin's module class names change between builds, while the player
99
+ // container / focusPanel structure and the X/Y text are stable.
100
+ const slide = document.querySelector('.dySwiperSlide');
101
+ const roots = [
102
+ slide && slide.closest('[data-e2e="player-container"]'),
103
+ slide && slide.closest('.focusPanel'),
104
+ document.querySelector('[data-e2e="player-container"]'),
105
+ document.querySelector('[data-e2e="note-detail"]'),
106
+ document,
107
+ ].filter(Boolean);
108
+
109
+ const candidates = [];
110
+ for (const root of roots) {
111
+ for (const el of root.querySelectorAll('div,span')) {
112
+ const text = String(el.textContent || '').trim();
113
+ if (!/^\d{1,3}\s*\/\s*\d{1,3}$/.test(text)) continue;
114
+ const parts = text.split('/').map((part) => Number(part.trim()));
115
+ const current = parts[0];
116
+ const total = parts[1];
117
+ if (!Number.isFinite(current) || !Number.isFinite(total)) continue;
118
+ if (current <= 0 || total < current || total > 100) continue;
119
+ const spans = el.querySelectorAll('span');
120
+ if (spans.length < 2) continue;
121
+ candidates.push({ el, current, total, size: el.querySelectorAll('*').length });
122
+ }
123
+ if (candidates.length) break;
124
+ }
125
+ if (candidates.length) {
126
+ candidates.sort((a, b) => a.size - b.size);
127
+ const counter = candidates[0].el;
128
+ const next = counter.nextElementSibling || (counter.parentElement && counter.parentElement.lastElementChild);
129
+ const prev = counter.previousElementSibling || (counter.parentElement && counter.parentElement.firstElementChild);
130
+ return {
131
+ current: candidates[0].current,
132
+ total: candidates[0].total,
133
+ next: center(next && next !== counter ? next : null),
134
+ prev: center(prev && prev !== counter ? prev : null),
135
+ };
136
+ }
137
+
138
+ const matches = [...String(document.body && document.body.innerText || '')
139
+ .matchAll(/(?:^|\s)(\d{1,3})\s*\/\s*(\d{1,3})(?=\s|$)/g)];
140
+ for (let index = matches.length - 1; index >= 0; index -= 1) {
141
+ const current = Number(matches[index][1]);
142
+ const total = Number(matches[index][2]);
143
+ if (current > 0 && total >= current && total <= 100) return { current, total, next: null, prev: null };
144
+ }
145
+ return null;
146
+ });
147
+ }
148
+
81
149
  async function freezeSlideVideos(page) {
82
150
  await page.evaluate(() => {
83
151
  const pause = () => {
@@ -118,7 +186,15 @@ async function advanceSlide(page, current) {
118
186
  await page.mouse.up();
119
187
  }
120
188
 
121
- async function clickNextSlide(page) {
189
+ async function clickNextSlide(page, position = null) {
190
+ // Prefer the button next to the discovered X/Y counter. This survives
191
+ // hashed class-name changes.
192
+ if (position && position.next) {
193
+ try {
194
+ await page.mouse.click(position.next.x, position.next.y);
195
+ return true;
196
+ } catch { /* fall through */ }
197
+ }
122
198
  const byClass = page.locator('.odSlyAjv');
123
199
  const classCount = await byClass.count();
124
200
  log('[note] next buttons: class=' + classCount);
@@ -138,12 +214,22 @@ async function clickNextSlide(page) {
138
214
  return false;
139
215
  }
140
216
 
141
- async function waitActiveSlide(page) {
217
+ function sameSlide(a, b) {
218
+ if (!a || !b) return false;
219
+ if (a.domIndex !== b.domIndex) return false;
220
+ if (a.videoSrc && b.videoSrc && a.videoSrc !== b.videoSrc) return false;
221
+ if (a.imageSrc && b.imageSrc && a.imageSrc !== b.imageSrc) return false;
222
+ return true;
223
+ }
224
+
225
+ async function waitActiveSlide(page, previous = null) {
142
226
  let info = null;
143
- for (let attempt = 0; attempt < 8; attempt += 1) {
227
+ for (let attempt = 0; attempt < 12; attempt += 1) {
144
228
  info = await activeSlideInfo(page);
145
- if (info && (!info.hasVideo || info.duration)) return info;
146
- await page.waitForTimeout(450);
229
+ const changed = previous ? !sameSlide(info, previous) : true;
230
+ if (info && changed && info.hasVideo && info.duration) return info;
231
+ if (info && changed && !info.hasVideo && info.imageSrc) return info;
232
+ await page.waitForTimeout(300);
147
233
  }
148
234
  return info;
149
235
  }
@@ -281,21 +367,25 @@ async function captureSlides(page, context, outDir) {
281
367
  try {
282
368
  converter = await context.newPage();
283
369
  await freezeSlideVideos(page);
284
- let initialSlides = await page.evaluate(() => document.querySelectorAll('.dySwiperSlide').length);
370
+ const initialSlides = await page.evaluate(() => document.querySelectorAll('.dySwiperSlide').length);
285
371
  if (initialSlides === 0) {
286
372
  return await captureFallbackImages(page, context, converter, outDir);
287
373
  }
288
374
 
289
375
  let current = await waitActiveSlide(page);
376
+ const initialPosition = await readSlidePosition(page);
377
+ const expectedTotal = initialPosition && initialPosition.total > 0 ? initialPosition.total : null;
290
378
  let guard = 0;
291
- log('[note] carousel initial slides: ' + initialSlides + ', first domIndex: ' + (current && current.domIndex));
292
- while (current && guard < 40) {
379
+ log('[note] carousel initial slides: ' + initialSlides
380
+ + ', page: ' + (initialPosition ? initialPosition.current + '/' + initialPosition.total : 'unknown')
381
+ + ', first domIndex: ' + (current && current.domIndex));
382
+ while (current && guard < 60) {
293
383
  guard += 1;
294
- const domCount = await page.evaluate(() => document.querySelectorAll('.dySwiperSlide').length);
295
- if (domCount > initialSlides) initialSlides = domCount;
296
- log('[note] processing domIndex ' + current.domIndex + ' (slide ' + (slides.length + 1) + ')');
297
- if (!seen.has(current.domIndex)) {
298
- seen.add(current.domIndex);
384
+ const position = await readSlidePosition(page);
385
+ const pageNo = position && position.current > 0 ? position.current : current.domIndex + 1;
386
+ log('[note] processing page ' + pageNo + ' (slide ' + (slides.length + 1) + ')');
387
+ if (!seen.has(pageNo)) {
388
+ seen.add(pageNo);
299
389
  const slideNo = slides.length + 1;
300
390
  if (current.hasVideo) {
301
391
  const videoFile = path.join(outDir, 'slide_' + String(slideNo).padStart(2, '0') + '.mp4');
@@ -399,17 +489,25 @@ async function captureSlides(page, context, outDir) {
399
489
  }
400
490
 
401
491
  // 一次只前进一格。优先点轮播右侧箭头;视频播放时按钮偶尔会被
402
- // 播放器层吃掉,先暂停再重试,仍失败才退回拖拽。
403
- if (slides.length >= initialSlides) break;
492
+ // 播放器层吃掉,先暂停再重试,仍失败才退回拖拽。页面计数器比
493
+ // DOM 节点数可靠:note.json 必须覆盖计数器的全部页数。
494
+ if (expectedTotal && seen.size >= expectedTotal) break;
495
+ const beforePage = pageNo;
404
496
  let next = null;
405
497
  for (let press = 0; press < 3 && !next; press += 1) {
406
- const clicked = await clickNextSlide(page);
498
+ const currentPosition = await readSlidePosition(page);
499
+ const clicked = await clickNextSlide(page, currentPosition);
407
500
  log('[note] next click ' + press + ': ' + clicked);
408
501
  if (!clicked) await advanceSlide(page, current);
409
502
  for (let wait = 0; wait < 8; wait += 1) {
410
- await page.waitForTimeout(500);
503
+ await page.waitForTimeout(400);
411
504
  const info = await activeSlideInfo(page);
412
- if (info && info.domIndex !== current.domIndex) {
505
+ const after = await readSlidePosition(page);
506
+ if (after && after.current !== beforePage) {
507
+ next = info || current;
508
+ break;
509
+ }
510
+ if (!after && info && info.domIndex !== current.domIndex) {
413
511
  next = info;
414
512
  break;
415
513
  }
@@ -419,9 +517,15 @@ async function captureSlides(page, context, outDir) {
419
517
  await page.waitForTimeout(500);
420
518
  }
421
519
  }
422
- log('[note] next domIndex: ' + (next && next.domIndex) + ', seen: ' + [...seen].join(','));
423
- if (!next || seen.has(next.domIndex)) break;
424
- current = await waitActiveSlide(page);
520
+ const afterPosition = await readSlidePosition(page);
521
+ log('[note] next page: ' + (afterPosition ? afterPosition.current + '/' + afterPosition.total : 'unknown')
522
+ + ', seen: ' + [...seen].join(','));
523
+ if (!next) break;
524
+ if (afterPosition && beforePage && afterPosition.current <= beforePage) break;
525
+ current = await waitActiveSlide(page, current);
526
+ }
527
+ if (expectedTotal && seen.size < expectedTotal) {
528
+ log('[note] WARNING incomplete carousel: captured ' + seen.size + '/' + expectedTotal + ' pages');
425
529
  }
426
530
  } finally {
427
531
  await unfreezeSlideVideos(page);