@cloud411716/fancy-webnovel 4.6.0 → 4.7.0
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
|
@@ -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');
|
|
@@ -162,6 +162,18 @@ async function main() {
|
|
|
162
162
|
'--no-first-run',
|
|
163
163
|
'--no-default-browser-check',
|
|
164
164
|
'--disable-blink-features=AutomationControlled',
|
|
165
|
+
// 这是自动化专用 profile:关闭 Chrome 自己的更新/组件/后台网络检查,
|
|
166
|
+
// 避免右上角弹出“无法更新 Chrome”的提示。
|
|
167
|
+
'--disable-background-networking',
|
|
168
|
+
'--disable-component-update',
|
|
169
|
+
'--disable-default-apps',
|
|
170
|
+
'--disable-sync',
|
|
171
|
+
'--disable-client-side-phishing-detection',
|
|
172
|
+
'--safebrowsing-disable-auto-update',
|
|
173
|
+
'--metrics-recording-only',
|
|
174
|
+
// 外部协议(douyin://、snssdk1128:// 等)交给页面脚本拦截;
|
|
175
|
+
// 这个 feature 名称在部分版本存在,不存在时 Chrome 会忽略。
|
|
176
|
+
'--disable-features=Translate,MediaRouter,OptimizationHints,ChromeWhatsNewUI,ExternalProtocolDialog',
|
|
165
177
|
'--window-size=1400,1000',
|
|
166
178
|
'--lang=zh-CN',
|
|
167
179
|
];
|