@cloud411716/fancy-webnovel 4.7.0 → 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.0",
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());
@@ -19,6 +19,10 @@ const { spawn } = require('child_process');
19
19
  const crypto = require('crypto');
20
20
  const { parseArgs, ensureDir, readJson, writeJsonAtomic, printJson, fail, log } = require('./lib.cjs');
21
21
 
22
+ // 启动参数版本:更新过 Chrome flags 后,旧的常驻 Chrome 不能继续复用,
23
+ // 否则用户会一直看到旧行为(更新提示 / xdg-open 弹窗)。
24
+ const LAUNCH_VERSION = 2;
25
+
22
26
  function getVersion(port, timeout = 1500) {
23
27
  return new Promise((resolve) => {
24
28
  const req = http.get({ host: '127.0.0.1', port, path: '/json/version', timeout }, (res) => {
@@ -132,7 +136,8 @@ async function main() {
132
136
  if (existing && Number(existing.port) > 0) {
133
137
  const version = await getVersion(Number(existing.port));
134
138
  const existingMode = existing.mode || 'headed';
135
- if (version.ok && existingMode === mode) {
139
+ const sameLaunch = Number(existing.launch_version) === LAUNCH_VERSION;
140
+ if (version.ok && existingMode === mode && sameLaunch) {
136
141
  const port = Number(existing.port);
137
142
  printJson({
138
143
  ok: true,
@@ -146,7 +151,8 @@ async function main() {
146
151
  return;
147
152
  }
148
153
  if (version.ok) {
149
- log('[chrome] mode switch ' + existingMode + ' -> ' + mode + ', closing old browser');
154
+ log('[chrome] restarting old browser (mode ' + existingMode + ' -> ' + mode
155
+ + ', launch ' + (existing.launch_version || 0) + ' -> ' + LAUNCH_VERSION + ')');
150
156
  await closeChrome(Number(existing.port));
151
157
  }
152
158
  }
@@ -198,6 +204,7 @@ async function main() {
198
204
  port,
199
205
  profile,
200
206
  mode,
207
+ launch_version: LAUNCH_VERSION,
201
208
  started_at: new Date().toISOString(),
202
209
  }, 0o600);
203
210