@cloud411716/fancy-webnovel 4.7.1 → 4.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud411716/fancy-webnovel",
3
- "version": "4.7.1",
3
+ "version": "4.7.2",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -9,7 +9,7 @@ const fs = require('fs');
9
9
  const path = require('path');
10
10
  const {
11
11
  log, parseArgs, connect, installExternalProtocolGuard, restoreCookies, saveCookies,
12
- gateResult, printJson, fail, ensureDir,
12
+ safeGateResult, printJson, fail, ensureDir,
13
13
  } = require('./lib.cjs');
14
14
 
15
15
  function noteId(url) {
@@ -149,7 +149,7 @@ async function main() {
149
149
  await page.goto(noteUrl, { waitUntil: 'domcontentloaded', timeout: 60000 });
150
150
  await page.waitForTimeout(4500);
151
151
 
152
- const firstGate = await gateResult(page, outDir);
152
+ const firstGate = await safeGateResult(page, outDir);
153
153
  if (firstGate) {
154
154
  keepPage = true;
155
155
  await saveCookies(context, loginState, page.url());
@@ -160,7 +160,7 @@ async function main() {
160
160
  const meta = await readMetadata(page);
161
161
  const screenshots = await captureSlides(page, context, outDir);
162
162
 
163
- const afterGate = await gateResult(page, outDir);
163
+ const afterGate = await safeGateResult(page, outDir);
164
164
  if (afterGate) {
165
165
  keepPage = true;
166
166
  await saveCookies(context, loginState, page.url());
@@ -223,6 +223,32 @@ async function gateResult(page, outDir, extra = {}) {
223
223
  };
224
224
  }
225
225
 
226
+ /**
227
+ * gateResult 的容错版本:页面在检测过程中跳转时,先等新页面加载,
228
+ * 再检测一次。仍然失败就按“需要登录/验证”返回,让上层弹登录提示,
229
+ * 而不是把一次导航抖动变成整篇作品采集失败。
230
+ */
231
+ async function safeGateResult(page, outDir, extra = {}) {
232
+ try {
233
+ return await gateResult(page, outDir, extra);
234
+ } catch (err) {
235
+ try {
236
+ await page.waitForLoadState('domcontentloaded', { timeout: 15000 });
237
+ } catch { /* page may be gone */ }
238
+ try {
239
+ return await gateResult(page, outDir, extra);
240
+ } catch (err2) {
241
+ return {
242
+ ok: true,
243
+ login_required: true,
244
+ reason: 'page_navigation',
245
+ error: err2 && err2.message ? err2.message : String(err2),
246
+ ...extra,
247
+ };
248
+ }
249
+ }
250
+ }
251
+
226
252
  function printJson(value) {
227
253
  process.stdout.write(JSON.stringify(value) + '\n');
228
254
  }
@@ -249,6 +275,7 @@ module.exports = {
249
275
  detectGate,
250
276
  saveGateScreenshot,
251
277
  gateResult,
278
+ safeGateResult,
252
279
  printJson,
253
280
  fail,
254
281
  };
@@ -11,7 +11,7 @@ const fs = require('fs');
11
11
  const path = require('path');
12
12
  const {
13
13
  log, parseArgs, connect, installExternalProtocolGuard, restoreCookies, saveCookies,
14
- gateResult, printJson, fail, ensureDir,
14
+ safeGateResult, printJson, fail, ensureDir,
15
15
  } = require('./lib.cjs');
16
16
 
17
17
  function normalize(url) {
@@ -92,7 +92,7 @@ async function main() {
92
92
  await page.goto(account, { waitUntil: 'domcontentloaded', timeout: 60000 });
93
93
  await page.waitForTimeout(3500);
94
94
 
95
- const firstGate = await gateResult(page, outDir);
95
+ const firstGate = await safeGateResult(page, outDir);
96
96
  if (firstGate) {
97
97
  keepPage = true;
98
98
  await saveCookies(context, loginState, page.url());
@@ -100,8 +100,32 @@ async function main() {
100
100
  return;
101
101
  }
102
102
 
103
- const cards = await collectCards(page);
104
- const afterGate = await gateResult(page, outDir);
103
+ let cards = [];
104
+ try {
105
+ cards = await collectCards(page);
106
+ } catch (err) {
107
+ // 页面在滚动/解析过程中跳转(登录页、验证页、风控页)时,
108
+ // page.evaluate 会报 Execution context was destroyed。
109
+ // 先等新页面稳定并检测登录门禁;检测不到也按需要登录处理,
110
+ // 让上层弹登录提示,而不是只报一句采集失败。
111
+ log('[scan] collectCards failed: ' + (err && err.message ? err.message : String(err)));
112
+ const gate = await safeGateResult(page, outDir, { reason: 'page_navigation' });
113
+ keepPage = true;
114
+ await saveCookies(context, loginState, page.url());
115
+ if (gate) {
116
+ printJson(gate);
117
+ } else {
118
+ printJson({
119
+ ok: true,
120
+ login_required: true,
121
+ reason: 'page_navigation',
122
+ error: err && err.message ? err.message : String(err),
123
+ url: page.url(),
124
+ });
125
+ }
126
+ return;
127
+ }
128
+ const afterGate = await safeGateResult(page, outDir);
105
129
  if (afterGate) {
106
130
  keepPage = true;
107
131
  await saveCookies(context, loginState, page.url());