@yyds-dudu/dsh-novel-reader 1.0.0 → 1.2.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/README.md +73 -8
- package/client.js +1023 -111
- package/docs/SOURCES.md +90 -0
- package/docs/screenshot-online-reading.png +0 -0
- package/docs/screenshot-online-search.png +0 -0
- package/docs/screenshot-online-shelf.png +0 -0
- package/docs/screenshot-sources.png +0 -0
- package/index.js +197 -2
- package/lib/community-sources.js +2090 -0
- package/lib/html.js +52 -0
- package/lib/http.js +62 -0
- package/lib/legado.js +345 -0
- package/lib/sources/custom.js +76 -0
- package/lib/sources/daizhige.js +68 -0
- package/lib/sources/index.js +34 -0
- package/lib/sources/wikisource.js +244 -0
- package/locale/en.json +118 -33
- package/locale/zh.json +114 -29
- package/package.json +11 -2
package/docs/SOURCES.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# 书源说明
|
|
2
|
+
|
|
3
|
+
## 内置书源(2 个,完全合规)
|
|
4
|
+
|
|
5
|
+
| 书源 | 内容 | 授权 |
|
|
6
|
+
|---|---|---|
|
|
7
|
+
| 殆知阁 | 中国古典文献(古籍、古典小说) | 站点公版内容 |
|
|
8
|
+
| 维基文库 | 四大名著与大量公版小说、诗文、史书 | 公版 / CC BY-SA 4.0 |
|
|
9
|
+
|
|
10
|
+
## 社区书源(50 个)
|
|
11
|
+
|
|
12
|
+
插件内置了 50 个社区书源,来自 GitHub 上的主流书源库(XIU2/Yuedu、luanmuc、Lcfyn、liufuyou 等),
|
|
13
|
+
经过去重、静态规则筛选、逐站实测后打包。按实测状态分为三档:
|
|
14
|
+
|
|
15
|
+
| 标记 | 分组 | 数量 | 含义 |
|
|
16
|
+
|---|---|---|---|
|
|
17
|
+
| ✔ | 社区验证 | 22 | **搜索 / 目录 / 正文三阶段实测全部通过**,可直接用 |
|
|
18
|
+
| △ | 社区候选 | 21 | 搜索能出结果,但目录或正文未通过(可能是临时故障) |
|
|
19
|
+
| ? | 社区未验证 | 7 | 站点疑似存活,未完成验证 |
|
|
20
|
+
|
|
21
|
+
### 关于可用率的实情
|
|
22
|
+
|
|
23
|
+
我实测了 **5277 个**社区书源(去重后),流程与结果:
|
|
24
|
+
|
|
25
|
+
| 阶段 | 数量 |
|
|
26
|
+
|---|---|
|
|
27
|
+
| 原始去重 | 5277 |
|
|
28
|
+
| 静态筛掉(含 `@js:` / `java.*` 等本插件不支持的规则) | -3026 |
|
|
29
|
+
| 静态通过 | 2160 |
|
|
30
|
+
| 搜索真的能出结果 | 52 |
|
|
31
|
+
| 目录 + 正文也通过 | 20 |
|
|
32
|
+
| 按站点去重 | 22 |
|
|
33
|
+
|
|
34
|
+
失败原因:网络不可达 482、超时 422、403 有 241、404 有 101、搜不到 825。
|
|
35
|
+
**社区书源真实可用率约 0.3%**——这个圈子站点关停换域名太快,仓库里的源没人清理。
|
|
36
|
+
|
|
37
|
+
所以那 22 个 ✔ 是真实可用的,而不是凑数。其余两档也一并内置,方便你用「体检」随时复查。
|
|
38
|
+
|
|
39
|
+
## 书源体检
|
|
40
|
+
|
|
41
|
+
「找书」面板右上角有 **🩺 体检** 按钮,点击后会对所有社区源跑一次
|
|
42
|
+
「搜索 → 目录 → 正文」实测,并把结果回写到下拉框的状态标记:
|
|
43
|
+
|
|
44
|
+
- ✔ 可用(三项全过)
|
|
45
|
+
- △ 部分可用(搜索能出结果,但目录/正文取不到)
|
|
46
|
+
- ? 未知/失效
|
|
47
|
+
|
|
48
|
+
体检 50 个源约需几分钟,建议隔一段时间跑一次,失效的源会自动标灰。
|
|
49
|
+
|
|
50
|
+
## 自己导入更多书源
|
|
51
|
+
|
|
52
|
+
「书架 → 找书 → 书源」里可以粘贴任意 **阅读3 / Legado 纯规则书源 JSON**。
|
|
53
|
+
|
|
54
|
+
### 兼容性判据(重要)
|
|
55
|
+
|
|
56
|
+
本插件只支持纯规则:
|
|
57
|
+
|
|
58
|
+
✅ 支持
|
|
59
|
+
- jsoup 默认写法:`class.x` / `id.x` / `tag.x`,带 `.N` 索引、`!N:M` 排除
|
|
60
|
+
- `@css:` CSS 选择器
|
|
61
|
+
- `@json:` / `$.path` JSONPath
|
|
62
|
+
- AllInOne 正则(`:` 开头,配 `$1` `$2`)
|
|
63
|
+
- `##正则##替换`
|
|
64
|
+
- `{{key}}` / `{{page}}` / `{{baseUrl}}`
|
|
65
|
+
- `&&` 合并、`||` 取首个非空
|
|
66
|
+
|
|
67
|
+
❌ 不支持(保存时会明确报错)
|
|
68
|
+
- `@js:` / `<js>...</js>`
|
|
69
|
+
- `java.*` 宿主调用
|
|
70
|
+
- `webJs` / WebView
|
|
71
|
+
- XPath(`@xpath:` 或 `/` 开头)
|
|
72
|
+
|
|
73
|
+
**快速判断**:把一个源 JSON 里搜一下有没有 `"@js:"`、`"<js"`、`"java."`、`"webJs"`——有就不兼容。
|
|
74
|
+
|
|
75
|
+
## 已知社区书源库
|
|
76
|
+
|
|
77
|
+
| 名称 | 地址 | 说明 |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| XIU2/Yuedu | https://github.com/XIU2/Yuedu | ⭐1.2万,GPL-3.0,22 个源中约 5 个纯规则 |
|
|
80
|
+
| aoaostar/legado | https://github.com/aoaostar/legado | ⭐8000+,书源合集 |
|
|
81
|
+
| Lcfyn/legado-sources | https://gitcode.com/Lcfyn/legado-sources | MIT,6600+ 源,需逐条过滤 |
|
|
82
|
+
| luanmuc/legado-book-sources | https://github.com/luanmuc/legado-book-sources | MIT,持续清理失效源 |
|
|
83
|
+
| Yiove 书源库 | https://shuyuan.yiove.com/complex | 活跃,量大,多为 JS 源 |
|
|
84
|
+
| YCK 源仓库 | https://www.yckceo.com/yuedu/shuyuan | 活跃,量大,多为 JS 源 |
|
|
85
|
+
|
|
86
|
+
## 合规提示
|
|
87
|
+
|
|
88
|
+
社区书源指向的绝大多数是**盗版小说站**,仅供个人学习与本地测试,
|
|
89
|
+
请勿传播或用于商业用途。发布/分享本插件时不应内置此类源;
|
|
90
|
+
插件默认同时提供殆知阁与维基文库两个合规源。
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/index.js
CHANGED
|
@@ -1,2 +1,197 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Host 半:注册两条同源路由,浏览器半通过 /api/dsh-novel-reader/* 调用。
|
|
3
|
+
* 这里只负责取干净文本(抓页面、解编码、抽正文),排版与阅读都在客户端。
|
|
4
|
+
*/
|
|
5
|
+
import { resolveSource, BUILTIN_SOURCES, communityList } from './lib/sources/index.js';
|
|
6
|
+
import { createLegadoSource, isLegadoSource } from './lib/legado.js';
|
|
7
|
+
|
|
8
|
+
const PREFIX = '/api/dsh-novel-reader';
|
|
9
|
+
const MAX_TEXT = 1_500_000;
|
|
10
|
+
|
|
11
|
+
function errorMessage(error) {
|
|
12
|
+
const raw = error instanceof Error ? error.message : String(error);
|
|
13
|
+
if (/aborted|timeout/i.test(raw)) return '请求超时或网络不可达';
|
|
14
|
+
if (/fetch failed|ENOTFOUND|ECONN|EAI_AGAIN|socket/i.test(raw)) return '网络不可达,请稍后再试';
|
|
15
|
+
return raw.slice(0, 300);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function fail(error) {
|
|
19
|
+
return Response.json({ ok: false, error: errorMessage(error) }, { status: 502 });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function readBody(request) {
|
|
23
|
+
try { return await request.json(); } catch { throw new Error('请求体不是合法 JSON'); }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 书源清单:内置源 + 社区源(社区只给元数据,规则留在宿主侧)。 */
|
|
27
|
+
async function handleSources() {
|
|
28
|
+
return Response.json({ ok: true, builtin: BUILTIN_SOURCES, community: communityList() });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 书源体检:对指定社区源跑一次「搜索 → 目录 → 正文」,回报实时可用性。
|
|
33
|
+
* 客户端可以一键刷新所有源的健康状态。
|
|
34
|
+
*/
|
|
35
|
+
async function handleHealth(request) {
|
|
36
|
+
try {
|
|
37
|
+
const body = await readBody(request);
|
|
38
|
+
const ids = Array.isArray(body.ids) ? body.ids.map(String) : [];
|
|
39
|
+
const query = String(body.query || '剑来').trim();
|
|
40
|
+
if (!ids.length) throw new Error('缺少要体检的书源 id');
|
|
41
|
+
const list = communityList();
|
|
42
|
+
const byId = new Map(list.map((s) => [s.id, s]));
|
|
43
|
+
const results = [];
|
|
44
|
+
// 串行体检,避免一次性打出几十个并发请求
|
|
45
|
+
for (const id of ids.slice(0, 60)) {
|
|
46
|
+
const meta = byId.get(id);
|
|
47
|
+
const rec = { id, name: meta ? meta.name : id, status: 'fail', search: 0, chapters: 0, contentLen: 0 };
|
|
48
|
+
if (!meta) { rec.error = '未知书源'; results.push(rec); continue; }
|
|
49
|
+
const withTimeout = (p, ms) => Promise.race([
|
|
50
|
+
p,
|
|
51
|
+
new Promise((_, rej) => setTimeout(() => rej(new Error('timeout')), ms)),
|
|
52
|
+
]);
|
|
53
|
+
try {
|
|
54
|
+
const src = resolveSource({ id });
|
|
55
|
+
const r = await withTimeout(src.search(query, 0), 12_000);
|
|
56
|
+
rec.search = (r.items || []).length;
|
|
57
|
+
if (rec.search) {
|
|
58
|
+
const toc = await withTimeout(src.chapters(r.items[0].url, { title: r.items[0].title }), 15_000);
|
|
59
|
+
rec.chapters = (toc.chapters || []).length;
|
|
60
|
+
if (rec.chapters) {
|
|
61
|
+
const c = await withTimeout(src.content(toc.chapters[Math.min(2, toc.chapters.length - 1)].url), 15_000);
|
|
62
|
+
rec.contentLen = String(c.text || '').length;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
rec.status = rec.search && rec.chapters && rec.contentLen > 200 ? 'ok' : (rec.search ? 'partial' : 'fail');
|
|
66
|
+
} catch (error) {
|
|
67
|
+
rec.error = errorMessage(error);
|
|
68
|
+
}
|
|
69
|
+
results.push(rec);
|
|
70
|
+
}
|
|
71
|
+
return Response.json({ ok: true, items: results });
|
|
72
|
+
} catch (error) { return fail(error); }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function handleSearch(request) {
|
|
76
|
+
try {
|
|
77
|
+
const body = await readBody(request);
|
|
78
|
+
const query = String(body.query || '').trim();
|
|
79
|
+
if (!query) throw new Error('搜索词不能为空');
|
|
80
|
+
const page = Math.max(0, Math.min(50, Number(body.page) || 0));
|
|
81
|
+
const result = await resolveSource(body.source).search(query, page);
|
|
82
|
+
return Response.json({ ok: true, ...result });
|
|
83
|
+
} catch (error) { return fail(error); }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function handleBook(request) {
|
|
87
|
+
try {
|
|
88
|
+
const body = await readBody(request);
|
|
89
|
+
const url = String(body.url || '').trim();
|
|
90
|
+
if (!url) throw new Error('缺少书籍地址');
|
|
91
|
+
const source = resolveSource(body.source);
|
|
92
|
+
if (typeof source.book !== 'function') throw new Error('这个书源不支持整本导入,请用在线阅读');
|
|
93
|
+
const result = await source.book(url);
|
|
94
|
+
const text = result.text.length > MAX_TEXT ? result.text.slice(0, MAX_TEXT) : result.text;
|
|
95
|
+
return Response.json({
|
|
96
|
+
ok: true,
|
|
97
|
+
title: result.title,
|
|
98
|
+
author: result.author || '',
|
|
99
|
+
text,
|
|
100
|
+
truncated: !!result.truncated || result.text.length > MAX_TEXT,
|
|
101
|
+
});
|
|
102
|
+
} catch (error) { return fail(error); }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 在线阅读:取章节目录(不抓正文)。hint 为搜索结果里的书名/作者,用于兜底标题。 */
|
|
106
|
+
async function handleChapters(request) {
|
|
107
|
+
try {
|
|
108
|
+
const body = await readBody(request);
|
|
109
|
+
const url = String(body.url || '').trim();
|
|
110
|
+
if (!url) throw new Error('缺少书籍地址');
|
|
111
|
+
const source = resolveSource(body.source);
|
|
112
|
+
if (typeof source.chapters !== 'function') throw new Error('这个书源不支持在线章节阅读');
|
|
113
|
+
const result = await source.chapters(url, body.hint || {});
|
|
114
|
+
return Response.json({
|
|
115
|
+
ok: true, title: result.title || '', author: result.author || '',
|
|
116
|
+
chapters: (result.chapters || []).slice(0, 10000),
|
|
117
|
+
});
|
|
118
|
+
} catch (error) { return fail(error); }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** 在线阅读:批量取正文(书源支持 contents 时走这条,显著减少请求数)。 */
|
|
122
|
+
async function handleContents(request) {
|
|
123
|
+
try {
|
|
124
|
+
const body = await readBody(request);
|
|
125
|
+
const urls = Array.isArray(body.urls) ? body.urls.map((u) => String(u || '').trim()).filter(Boolean) : [];
|
|
126
|
+
if (!urls.length) throw new Error('缺少章节地址');
|
|
127
|
+
const source = resolveSource(body.source);
|
|
128
|
+
if (typeof source.contents !== 'function') throw new Error('这个书源不支持批量取正文');
|
|
129
|
+
const map = await source.contents(urls.slice(0, 200));
|
|
130
|
+
const texts = {};
|
|
131
|
+
for (const [url, text] of Object.entries(map || {})) texts[url] = String(text || '').slice(0, MAX_TEXT);
|
|
132
|
+
return Response.json({ ok: true, texts });
|
|
133
|
+
} catch (error) { return fail(error); }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 在线阅读:取单章正文。 */
|
|
137
|
+
async function handleContent(request) {
|
|
138
|
+
try {
|
|
139
|
+
const body = await readBody(request);
|
|
140
|
+
const url = String(body.url || '').trim();
|
|
141
|
+
if (!url) throw new Error('缺少章节地址');
|
|
142
|
+
const source = resolveSource(body.source);
|
|
143
|
+
if (typeof source.content !== 'function') throw new Error('这个书源不支持在线章节阅读');
|
|
144
|
+
const result = await source.content(url);
|
|
145
|
+
const text = String(result.text || '').slice(0, MAX_TEXT);
|
|
146
|
+
return Response.json({ ok: true, title: result.title || '', text });
|
|
147
|
+
} catch (error) { return fail(error); }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 浏览器半通过 connection 的 fetch 通道访问同源路由;不声明 inject 会在构造时抛错,
|
|
151
|
+
// 导致整条插件条目不激活(客户端半也随之消失)。
|
|
152
|
+
export const inject = ['connection'];
|
|
153
|
+
|
|
154
|
+
export function apply(ctx) {
|
|
155
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
156
|
+
path: PREFIX + '/sources',
|
|
157
|
+
methods: ['POST'],
|
|
158
|
+
requestBody: 'buffered',
|
|
159
|
+
fetch: handleSources,
|
|
160
|
+
}), 'dsh-novel-reader: 书源清单路由');
|
|
161
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
162
|
+
path: PREFIX + '/health',
|
|
163
|
+
methods: ['POST'],
|
|
164
|
+
requestBody: 'buffered',
|
|
165
|
+
fetch: handleHealth,
|
|
166
|
+
}), 'dsh-novel-reader: 书源体检路由');
|
|
167
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
168
|
+
path: PREFIX + '/search',
|
|
169
|
+
methods: ['POST'],
|
|
170
|
+
requestBody: 'buffered',
|
|
171
|
+
fetch: handleSearch,
|
|
172
|
+
}), 'dsh-novel-reader: 搜索路由');
|
|
173
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
174
|
+
path: PREFIX + '/book',
|
|
175
|
+
methods: ['POST'],
|
|
176
|
+
requestBody: 'buffered',
|
|
177
|
+
fetch: handleBook,
|
|
178
|
+
}), 'dsh-novel-reader: 正文路由');
|
|
179
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
180
|
+
path: PREFIX + '/chapters',
|
|
181
|
+
methods: ['POST'],
|
|
182
|
+
requestBody: 'buffered',
|
|
183
|
+
fetch: handleChapters,
|
|
184
|
+
}), 'dsh-novel-reader: 章节目录路由');
|
|
185
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
186
|
+
path: PREFIX + '/contents',
|
|
187
|
+
methods: ['POST'],
|
|
188
|
+
requestBody: 'buffered',
|
|
189
|
+
fetch: handleContents,
|
|
190
|
+
}), 'dsh-novel-reader: 批量正文路由');
|
|
191
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
192
|
+
path: PREFIX + '/content',
|
|
193
|
+
methods: ['POST'],
|
|
194
|
+
requestBody: 'buffered',
|
|
195
|
+
fetch: handleContent,
|
|
196
|
+
}), 'dsh-novel-reader: 单章正文路由');
|
|
197
|
+
}
|