@cloud411716/fancy-webnovel 4.6.0 → 4.7.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud411716/fancy-webnovel",
3
- "version": "4.6.0",
3
+ "version": "4.7.1",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -8,7 +8,7 @@
8
8
  const fs = require('fs');
9
9
  const path = require('path');
10
10
  const {
11
- log, parseArgs, connect, restoreCookies, saveCookies,
11
+ log, parseArgs, connect, installExternalProtocolGuard, restoreCookies, saveCookies,
12
12
  gateResult, printJson, fail, ensureDir,
13
13
  } = require('./lib.cjs');
14
14
 
@@ -145,6 +145,7 @@ async function main() {
145
145
  try {
146
146
  await restoreCookies(context, loginState);
147
147
  page = await context.newPage();
148
+ await installExternalProtocolGuard(page);
148
149
  await page.goto(noteUrl, { waitUntil: 'domcontentloaded', timeout: 60000 });
149
150
  await page.waitForTimeout(4500);
150
151
 
@@ -71,6 +71,62 @@ async function connect(cdp) {
71
71
  return { browser, context };
72
72
  }
73
73
 
74
+ /**
75
+ * 阻止页面把浏览器导航到 douyin://、snssdk1128:// 等外部协议。
76
+ *
77
+ * Linux 上 Chrome 遇到未注册的协议会调用 xdg-open,并弹出
78
+ * “要打开 xdg-open 吗?”。自动化 profile 不需要这些外链,直接在页面侧
79
+ * 拦截点击 / window.open / location.assign/replace。
80
+ */
81
+ async function installExternalProtocolGuard(page) {
82
+ await page.addInitScript(() => {
83
+ const allowed = new Set(['http:', 'https:', 'about:', 'data:', 'blob:', 'file:', 'chrome:', 'devtools:', 'ws:', 'wss:']);
84
+ const isExternal = (value) => {
85
+ try {
86
+ const url = new URL(String(value == null ? '' : value), location.href);
87
+ return !allowed.has(url.protocol);
88
+ } catch {
89
+ return false;
90
+ }
91
+ };
92
+ try {
93
+ const originalOpen = window.open;
94
+ window.open = function (url, ...args) {
95
+ if (isExternal(url)) return null;
96
+ return originalOpen.call(this, url, ...args);
97
+ };
98
+ } catch { /* ignore */ }
99
+ try {
100
+ document.addEventListener('click', (event) => {
101
+ const anchor = event.target && event.target.closest ? event.target.closest('a[href]') : null;
102
+ if (anchor && isExternal(anchor.getAttribute('href'))) {
103
+ event.preventDefault();
104
+ event.stopPropagation();
105
+ }
106
+ }, true);
107
+ } catch { /* ignore */ }
108
+ try {
109
+ const originalClick = HTMLAnchorElement.prototype.click;
110
+ HTMLAnchorElement.prototype.click = function () {
111
+ if (isExternal(this.getAttribute('href'))) return;
112
+ return originalClick.apply(this, arguments);
113
+ };
114
+ } catch { /* ignore */ }
115
+ try {
116
+ const originalAssign = Location.prototype.assign;
117
+ const originalReplace = Location.prototype.replace;
118
+ Location.prototype.assign = function (url) {
119
+ if (isExternal(url)) return;
120
+ return originalAssign.call(this, url);
121
+ };
122
+ Location.prototype.replace = function (url) {
123
+ if (isExternal(url)) return;
124
+ return originalReplace.call(this, url);
125
+ };
126
+ } catch { /* ignore */ }
127
+ });
128
+ }
129
+
74
130
  async function restoreCookies(context, loginStateFile) {
75
131
  const data = readJson(loginStateFile);
76
132
  const cookies = Array.isArray(data?.cookies) ? data.cookies : [];
@@ -187,6 +243,7 @@ module.exports = {
187
243
  readJson,
188
244
  writeJsonAtomic,
189
245
  connect,
246
+ installExternalProtocolGuard,
190
247
  restoreCookies,
191
248
  saveCookies,
192
249
  detectGate,
@@ -10,7 +10,7 @@
10
10
  const fs = require('fs');
11
11
  const path = require('path');
12
12
  const {
13
- log, parseArgs, connect, restoreCookies, saveCookies,
13
+ log, parseArgs, connect, installExternalProtocolGuard, restoreCookies, saveCookies,
14
14
  gateResult, printJson, fail, ensureDir,
15
15
  } = require('./lib.cjs');
16
16
 
@@ -88,6 +88,7 @@ async function main() {
88
88
  try {
89
89
  await restoreCookies(context, loginState);
90
90
  page = await context.newPage();
91
+ await installExternalProtocolGuard(page);
91
92
  await page.goto(account, { waitUntil: 'domcontentloaded', timeout: 60000 });
92
93
  await page.waitForTimeout(3500);
93
94
 
@@ -108,6 +109,24 @@ async function main() {
108
109
  return;
109
110
  }
110
111
 
112
+ // 作品列表没加载出来(常见于登录态失效 / 风控 / 页面服务异常)。
113
+ // 不能返回 ok:true + notes:[],否则上层会当成“这个账号没有作品”,
114
+ // 用户只会看到空跑。这里按需要登录/验证处理,让上层弹登录提示。
115
+ if (!cards.length) {
116
+ keepPage = true;
117
+ await saveCookies(context, loginState, page.url());
118
+ const bodyHead = await page.evaluate(() => (document.body && document.body.innerText ? document.body.innerText : '').slice(0, 400));
119
+ printJson({
120
+ ok: true,
121
+ login_required: true,
122
+ reason: 'works_not_loaded',
123
+ url: page.url(),
124
+ title: await page.title(),
125
+ body_head: bodyHead,
126
+ });
127
+ return;
128
+ }
129
+
111
130
  await saveCookies(context, loginState, page.url());
112
131
  const cardsFile = path.join(outDir, 'profile_cards.json');
113
132
  fs.writeFileSync(cardsFile, JSON.stringify(cards, null, 2) + '\n', 'utf8');
@@ -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
  }
@@ -162,6 +168,18 @@ async function main() {
162
168
  '--no-first-run',
163
169
  '--no-default-browser-check',
164
170
  '--disable-blink-features=AutomationControlled',
171
+ // 这是自动化专用 profile:关闭 Chrome 自己的更新/组件/后台网络检查,
172
+ // 避免右上角弹出“无法更新 Chrome”的提示。
173
+ '--disable-background-networking',
174
+ '--disable-component-update',
175
+ '--disable-default-apps',
176
+ '--disable-sync',
177
+ '--disable-client-side-phishing-detection',
178
+ '--safebrowsing-disable-auto-update',
179
+ '--metrics-recording-only',
180
+ // 外部协议(douyin://、snssdk1128:// 等)交给页面脚本拦截;
181
+ // 这个 feature 名称在部分版本存在,不存在时 Chrome 会忽略。
182
+ '--disable-features=Translate,MediaRouter,OptimizationHints,ChromeWhatsNewUI,ExternalProtocolDialog',
165
183
  '--window-size=1400,1000',
166
184
  '--lang=zh-CN',
167
185
  ];
@@ -186,6 +204,7 @@ async function main() {
186
204
  port,
187
205
  profile,
188
206
  mode,
207
+ launch_version: LAUNCH_VERSION,
189
208
  started_at: new Date().toISOString(),
190
209
  }, 0o600);
191
210