@blankgrimoire/runtime 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +59 -2
- package/dist/css.d.ts +10 -0
- package/dist/css.js +21 -0
- package/dist/dom.d.ts +39 -0
- package/dist/dom.js +95 -0
- package/dist/engine.d.ts +35 -0
- package/dist/engine.js +311 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/prelude.d.ts +27 -0
- package/dist/prelude.js +96 -0
- package/dist/runtime.js +790 -0
- package/dist/strategies.d.ts +49 -0
- package/dist/strategies.js +282 -0
- package/package.json +36 -3
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,790 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var BlankGrimoireRuntime = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
__apiVersion: () => __apiVersion,
|
|
25
|
+
__http: () => __http,
|
|
26
|
+
__nativeInvoke: () => __nativeInvoke,
|
|
27
|
+
__parseHTML: () => __parseHTML,
|
|
28
|
+
__timerFire: () => __timerFire,
|
|
29
|
+
clearTimeout: () => clearTimeout,
|
|
30
|
+
console: () => console,
|
|
31
|
+
dispatch: () => dispatch,
|
|
32
|
+
extract: () => extract,
|
|
33
|
+
extractAjax: () => extractAjax,
|
|
34
|
+
extractChapters: () => extractChapters,
|
|
35
|
+
extractCss: () => extractCss,
|
|
36
|
+
extractDetails: () => extractDetails,
|
|
37
|
+
extractHydration: () => extractHydration,
|
|
38
|
+
extractPages: () => extractPages,
|
|
39
|
+
extractScriptJSON: () => extractScriptJSON,
|
|
40
|
+
extractSearch: () => extractSearch,
|
|
41
|
+
normalizeContentRating: () => normalizeContentRating,
|
|
42
|
+
normalizeStatus: () => normalizeStatus,
|
|
43
|
+
parseJSONText: () => parseJSONText,
|
|
44
|
+
readPath: () => readPath,
|
|
45
|
+
resolveSelectors: () => resolveSelectors,
|
|
46
|
+
runStrategy: () => runStrategy,
|
|
47
|
+
setTimeout: () => setTimeout,
|
|
48
|
+
toPages: () => toPages
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// ../types/dist/version.js
|
|
52
|
+
var API_VERSION = 1;
|
|
53
|
+
|
|
54
|
+
// src/prelude.ts
|
|
55
|
+
var __apiVersion = API_VERSION;
|
|
56
|
+
function nativeHost() {
|
|
57
|
+
return globalThis.__native;
|
|
58
|
+
}
|
|
59
|
+
var timers = /* @__PURE__ */ new Map();
|
|
60
|
+
var timerHandles = /* @__PURE__ */ new Map();
|
|
61
|
+
var timerIdCounter = 0;
|
|
62
|
+
function setTimeout(cb, ms) {
|
|
63
|
+
const id = ++timerIdCounter;
|
|
64
|
+
timers.set(id, cb);
|
|
65
|
+
const host = nativeHost();
|
|
66
|
+
if (host && typeof host.setTimeout === "function") {
|
|
67
|
+
const handle = host.setTimeout(() => __timerFire(id), ms);
|
|
68
|
+
timerHandles.set(id, handle);
|
|
69
|
+
}
|
|
70
|
+
return id;
|
|
71
|
+
}
|
|
72
|
+
function clearTimeout(id) {
|
|
73
|
+
timers.delete(id);
|
|
74
|
+
const host = nativeHost();
|
|
75
|
+
const handle = timerHandles.get(id);
|
|
76
|
+
if (handle !== void 0 && host && typeof host.clearTimeout === "function") {
|
|
77
|
+
host.clearTimeout(handle);
|
|
78
|
+
}
|
|
79
|
+
timerHandles.delete(id);
|
|
80
|
+
}
|
|
81
|
+
function __timerFire(id) {
|
|
82
|
+
const cb = timers.get(id);
|
|
83
|
+
if (cb) {
|
|
84
|
+
timers.delete(id);
|
|
85
|
+
timerHandles.delete(id);
|
|
86
|
+
cb();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function __nativeInvoke(fn, ...args) {
|
|
90
|
+
const host = nativeHost();
|
|
91
|
+
if (!host || typeof host[fn] !== "function") {
|
|
92
|
+
throw new Error(`Host function not implemented: ${fn}`);
|
|
93
|
+
}
|
|
94
|
+
return host[fn](...args);
|
|
95
|
+
}
|
|
96
|
+
function __http(request) {
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
const host = nativeHost();
|
|
99
|
+
if (!host || typeof host.request !== "function") {
|
|
100
|
+
reject(new Error("Host function not implemented: request"));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
Promise.resolve(host.request(request)).then(resolve, reject);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
reject(error);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function __parseHTML(html) {
|
|
111
|
+
const host = nativeHost();
|
|
112
|
+
if (host && typeof host.parseHTML === "function") {
|
|
113
|
+
return host.parseHTML(html);
|
|
114
|
+
}
|
|
115
|
+
const document = globalThis.document;
|
|
116
|
+
if (document?.implementation?.createHTMLDocument) {
|
|
117
|
+
const parsed = document.implementation.createHTMLDocument("");
|
|
118
|
+
if (parsed.documentElement) {
|
|
119
|
+
parsed.documentElement.innerHTML = html;
|
|
120
|
+
}
|
|
121
|
+
return parsed;
|
|
122
|
+
}
|
|
123
|
+
throw new Error("Host function not implemented: parseHTML");
|
|
124
|
+
}
|
|
125
|
+
var console = {
|
|
126
|
+
log: (...args) => {
|
|
127
|
+
nativeHost()?.log?.(...args);
|
|
128
|
+
},
|
|
129
|
+
error: (...args) => {
|
|
130
|
+
nativeHost()?.error?.(...args);
|
|
131
|
+
},
|
|
132
|
+
warn: (...args) => {
|
|
133
|
+
nativeHost()?.warn?.(...args);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/dom.ts
|
|
138
|
+
function queryAll(root, selector) {
|
|
139
|
+
return Array.from(root.querySelectorAll(selector));
|
|
140
|
+
}
|
|
141
|
+
function textOf(element) {
|
|
142
|
+
return (element?.textContent ?? "").trim();
|
|
143
|
+
}
|
|
144
|
+
function attr(element, name) {
|
|
145
|
+
const value = element.getAttribute(name);
|
|
146
|
+
return value !== null && value.trim() !== "" ? value.trim() : null;
|
|
147
|
+
}
|
|
148
|
+
var ABSOLUTE_URL = /^[A-Za-z][A-Za-z0-9+.-]*:/;
|
|
149
|
+
var BASE_URL = /^([A-Za-z][A-Za-z0-9+.-]*:)(\/\/[^/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?$/;
|
|
150
|
+
function normalizePath(path) {
|
|
151
|
+
const segments = [];
|
|
152
|
+
for (const segment of path.split("/")) {
|
|
153
|
+
if (segment === "" || segment === ".") {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (segment === "..") {
|
|
157
|
+
segments.pop();
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
segments.push(segment);
|
|
161
|
+
}
|
|
162
|
+
const leading = path.startsWith("/") ? "/" : "";
|
|
163
|
+
const trailing = path.endsWith("/") && segments.length > 0 ? "/" : "";
|
|
164
|
+
return `${leading}${segments.join("/")}${trailing}`;
|
|
165
|
+
}
|
|
166
|
+
function resolveRelativeURL(value, baseURL) {
|
|
167
|
+
if (ABSOLUTE_URL.test(value)) {
|
|
168
|
+
return value;
|
|
169
|
+
}
|
|
170
|
+
if (value.startsWith("//")) {
|
|
171
|
+
const scheme2 = /^[A-Za-z][A-Za-z0-9+.-]*:/.exec(baseURL)?.[0] ?? "";
|
|
172
|
+
return `${scheme2}${value}`;
|
|
173
|
+
}
|
|
174
|
+
const base = BASE_URL.exec(baseURL);
|
|
175
|
+
if (!base) {
|
|
176
|
+
return value;
|
|
177
|
+
}
|
|
178
|
+
const scheme = base[1] ?? "";
|
|
179
|
+
const authority = base[2] ?? "";
|
|
180
|
+
const basePath = base[3] ?? "";
|
|
181
|
+
const baseQuery = base[4] ?? "";
|
|
182
|
+
const hashIndex = value.indexOf("#");
|
|
183
|
+
const hash = hashIndex >= 0 ? value.slice(hashIndex) : "";
|
|
184
|
+
const withoutHash = hashIndex >= 0 ? value.slice(0, hashIndex) : value;
|
|
185
|
+
const queryIndex = withoutHash.indexOf("?");
|
|
186
|
+
const query = queryIndex >= 0 ? withoutHash.slice(queryIndex) : "";
|
|
187
|
+
const path = queryIndex >= 0 ? withoutHash.slice(0, queryIndex) : withoutHash;
|
|
188
|
+
if (path === "") {
|
|
189
|
+
return `${scheme}${authority}${basePath}${query || baseQuery}${hash}`;
|
|
190
|
+
}
|
|
191
|
+
let resolvedPath;
|
|
192
|
+
if (path.startsWith("/")) {
|
|
193
|
+
resolvedPath = normalizePath(path);
|
|
194
|
+
} else {
|
|
195
|
+
const directory = basePath.slice(0, basePath.lastIndexOf("/") + 1) || "/";
|
|
196
|
+
resolvedPath = normalizePath(`${directory}${path}`);
|
|
197
|
+
}
|
|
198
|
+
return `${scheme}${authority}${resolvedPath}${query}${hash}`;
|
|
199
|
+
}
|
|
200
|
+
function resolveURL(value, baseURL) {
|
|
201
|
+
const input = value.trim();
|
|
202
|
+
if (input === "") {
|
|
203
|
+
return baseURL;
|
|
204
|
+
}
|
|
205
|
+
if (typeof URL === "function") {
|
|
206
|
+
try {
|
|
207
|
+
return new URL(input, baseURL).toString();
|
|
208
|
+
} catch {
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return resolveRelativeURL(input, baseURL);
|
|
212
|
+
}
|
|
213
|
+
function firstAttribute(element, names) {
|
|
214
|
+
for (const name of names) {
|
|
215
|
+
const value = attr(element, name);
|
|
216
|
+
if (value) {
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/css.ts
|
|
224
|
+
function extractCss(strategy, document, baseURL) {
|
|
225
|
+
const pages = [];
|
|
226
|
+
for (const element of queryAll(document, strategy.image)) {
|
|
227
|
+
const value = firstAttribute(element, strategy.srcAttributes);
|
|
228
|
+
if (!value) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const page = {
|
|
232
|
+
index: pages.length,
|
|
233
|
+
imageURL: resolveURL(value, baseURL)
|
|
234
|
+
};
|
|
235
|
+
if (strategy.headers) {
|
|
236
|
+
page.headers = { ...strategy.headers };
|
|
237
|
+
}
|
|
238
|
+
pages.push(page);
|
|
239
|
+
}
|
|
240
|
+
return pages;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// src/strategies.ts
|
|
244
|
+
var MAX_AJAX_DEPTH = 5;
|
|
245
|
+
function base64Decode(input) {
|
|
246
|
+
try {
|
|
247
|
+
if (typeof atob !== "undefined") {
|
|
248
|
+
return atob(input);
|
|
249
|
+
}
|
|
250
|
+
const buffer = globalThis.Buffer;
|
|
251
|
+
if (buffer) {
|
|
252
|
+
return buffer.from(input, "base64").toString("utf-8");
|
|
253
|
+
}
|
|
254
|
+
throw new Error("no base64 decoder available");
|
|
255
|
+
} catch {
|
|
256
|
+
return input;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function parseJSONText(text) {
|
|
260
|
+
const trimmed = text.trim();
|
|
261
|
+
try {
|
|
262
|
+
return JSON.parse(trimmed);
|
|
263
|
+
} catch {
|
|
264
|
+
}
|
|
265
|
+
const start = trimmed.search(/[[{]/);
|
|
266
|
+
if (start === -1) {
|
|
267
|
+
return void 0;
|
|
268
|
+
}
|
|
269
|
+
const stack = [];
|
|
270
|
+
let inString = false;
|
|
271
|
+
let escaped = false;
|
|
272
|
+
for (let index = start; index < trimmed.length; index += 1) {
|
|
273
|
+
const character = trimmed[index];
|
|
274
|
+
if (character === void 0) {
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
if (inString) {
|
|
278
|
+
if (escaped) {
|
|
279
|
+
escaped = false;
|
|
280
|
+
} else if (character === "\\") {
|
|
281
|
+
escaped = true;
|
|
282
|
+
} else if (character === '"') {
|
|
283
|
+
inString = false;
|
|
284
|
+
}
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (character === '"') {
|
|
288
|
+
inString = true;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (character === "{" || character === "[") {
|
|
292
|
+
stack.push(character);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
if (character === "}" || character === "]") {
|
|
296
|
+
const open = stack.pop();
|
|
297
|
+
if (open === void 0 || open === "{" && character !== "}" || open === "[" && character !== "]") {
|
|
298
|
+
return void 0;
|
|
299
|
+
}
|
|
300
|
+
if (stack.length === 0) {
|
|
301
|
+
try {
|
|
302
|
+
return JSON.parse(trimmed.slice(start, index + 1));
|
|
303
|
+
} catch {
|
|
304
|
+
return void 0;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return void 0;
|
|
310
|
+
}
|
|
311
|
+
function readPath(value, path) {
|
|
312
|
+
if (path.trim() === "") {
|
|
313
|
+
return value;
|
|
314
|
+
}
|
|
315
|
+
let current = value;
|
|
316
|
+
for (const rawPart of path.split(".")) {
|
|
317
|
+
const match = /^([^[\]]*)(?:\[(\d+)\])?$/.exec(rawPart);
|
|
318
|
+
if (!match) {
|
|
319
|
+
return void 0;
|
|
320
|
+
}
|
|
321
|
+
const key = match[1] ?? "";
|
|
322
|
+
if (key !== "") {
|
|
323
|
+
current = current?.[key];
|
|
324
|
+
}
|
|
325
|
+
if (match[2] !== void 0) {
|
|
326
|
+
current = current?.[Number(match[2])];
|
|
327
|
+
}
|
|
328
|
+
if (current === void 0 || current === null) {
|
|
329
|
+
return void 0;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return current;
|
|
333
|
+
}
|
|
334
|
+
function pageURLFromItem(item) {
|
|
335
|
+
if (typeof item === "string") {
|
|
336
|
+
return item.trim() === "" ? null : item;
|
|
337
|
+
}
|
|
338
|
+
if (item && typeof item === "object") {
|
|
339
|
+
const record = item;
|
|
340
|
+
for (const key of ["imageURL", "url", "src", "image"]) {
|
|
341
|
+
const value = record[key];
|
|
342
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
343
|
+
return value;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
function toPages(payload, baseURL, headers) {
|
|
350
|
+
const candidates = Array.isArray(payload) ? payload : [payload];
|
|
351
|
+
const pages = [];
|
|
352
|
+
for (const candidate of candidates) {
|
|
353
|
+
const value = pageURLFromItem(candidate);
|
|
354
|
+
if (!value) {
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
const page = {
|
|
358
|
+
index: pages.length,
|
|
359
|
+
imageURL: resolveURL(value, baseURL)
|
|
360
|
+
};
|
|
361
|
+
if (headers) {
|
|
362
|
+
page.headers = { ...headers };
|
|
363
|
+
}
|
|
364
|
+
pages.push(page);
|
|
365
|
+
}
|
|
366
|
+
return pages;
|
|
367
|
+
}
|
|
368
|
+
async function extractScriptJSON(strategy, document, baseURL, parseHTML) {
|
|
369
|
+
const regex = new RegExp(strategy.pattern);
|
|
370
|
+
for (const script of queryAll(document, "script")) {
|
|
371
|
+
const text = textOf(script);
|
|
372
|
+
const src = attr(script, "src");
|
|
373
|
+
let content = "";
|
|
374
|
+
if (text !== "" && regex.test(text)) {
|
|
375
|
+
content = text;
|
|
376
|
+
} else if (src && regex.test(src)) {
|
|
377
|
+
try {
|
|
378
|
+
const response = await __http({ url: resolveURL(src, baseURL), method: "GET" });
|
|
379
|
+
if (response.status < 200 || response.status >= 300) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
content = response.body;
|
|
383
|
+
} catch {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
} else {
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
try {
|
|
390
|
+
const decoded = strategy.base64 ? base64Decode(content) : content;
|
|
391
|
+
const parsed = parseJSONText(decoded);
|
|
392
|
+
if (parsed === void 0) {
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const payload = readPath(parsed, strategy.path);
|
|
396
|
+
if (payload === void 0 || payload === null) {
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
const pages = toPages(payload, baseURL);
|
|
400
|
+
if (pages.length > 0) {
|
|
401
|
+
return pages;
|
|
402
|
+
}
|
|
403
|
+
} catch {
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return [];
|
|
407
|
+
}
|
|
408
|
+
function hydrationPayload(strategy, document, windowObj) {
|
|
409
|
+
if (windowObj) {
|
|
410
|
+
const value = strategy.framework === "next" ? windowObj.__NEXT_DATA__ : windowObj.__NUXT__;
|
|
411
|
+
if (value !== void 0) {
|
|
412
|
+
return value;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
const selectors = strategy.framework === "next" ? ["#__NEXT_DATA__"] : ["#__NUXT_DATA__", "#__NUXT__"];
|
|
416
|
+
for (const selector of selectors) {
|
|
417
|
+
const element = document.querySelector(selector);
|
|
418
|
+
const text = textOf(element);
|
|
419
|
+
if (text !== "") {
|
|
420
|
+
const parsed = parseJSONText(text);
|
|
421
|
+
if (parsed !== void 0) {
|
|
422
|
+
return parsed;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return void 0;
|
|
427
|
+
}
|
|
428
|
+
function extractHydration(strategy, document, baseURL, windowObj) {
|
|
429
|
+
const payload = hydrationPayload(strategy, document, windowObj);
|
|
430
|
+
if (payload === void 0) {
|
|
431
|
+
return [];
|
|
432
|
+
}
|
|
433
|
+
let value = payload;
|
|
434
|
+
if (strategy.path) {
|
|
435
|
+
value = readPath(payload, strategy.path);
|
|
436
|
+
}
|
|
437
|
+
if (value === void 0 || value === null) {
|
|
438
|
+
return [];
|
|
439
|
+
}
|
|
440
|
+
return toPages(value, baseURL);
|
|
441
|
+
}
|
|
442
|
+
async function extractAjax(strategy, document, environment, depth = 0) {
|
|
443
|
+
if (depth >= MAX_AJAX_DEPTH) {
|
|
444
|
+
return [];
|
|
445
|
+
}
|
|
446
|
+
const url = resolveURL(strategy.path, environment.baseURL);
|
|
447
|
+
const request = {
|
|
448
|
+
url,
|
|
449
|
+
method: strategy.method
|
|
450
|
+
};
|
|
451
|
+
if (strategy.body !== void 0) {
|
|
452
|
+
request.body = strategy.body;
|
|
453
|
+
}
|
|
454
|
+
let response;
|
|
455
|
+
try {
|
|
456
|
+
response = await __http(request);
|
|
457
|
+
} catch {
|
|
458
|
+
return [];
|
|
459
|
+
}
|
|
460
|
+
if (response.status < 200 || response.status >= 300) {
|
|
461
|
+
return [];
|
|
462
|
+
}
|
|
463
|
+
let parsed;
|
|
464
|
+
try {
|
|
465
|
+
parsed = environment.parseHTML ? environment.parseHTML(response.body) : __parseHTML(response.body);
|
|
466
|
+
} catch {
|
|
467
|
+
return [];
|
|
468
|
+
}
|
|
469
|
+
return runStrategy(strategy.then, parsed, environment, depth + 1);
|
|
470
|
+
}
|
|
471
|
+
async function runStrategy(strategy, document, environment, depth = 0) {
|
|
472
|
+
if (!strategy || typeof strategy !== "object") {
|
|
473
|
+
return [];
|
|
474
|
+
}
|
|
475
|
+
const kind = strategy.kind;
|
|
476
|
+
switch (kind) {
|
|
477
|
+
case "css":
|
|
478
|
+
return extractCss(strategy, document, environment.baseURL);
|
|
479
|
+
case "scriptJSON":
|
|
480
|
+
return extractScriptJSON(
|
|
481
|
+
strategy,
|
|
482
|
+
document,
|
|
483
|
+
environment.baseURL,
|
|
484
|
+
environment.parseHTML
|
|
485
|
+
);
|
|
486
|
+
case "hydration":
|
|
487
|
+
return extractHydration(
|
|
488
|
+
strategy,
|
|
489
|
+
document,
|
|
490
|
+
environment.baseURL,
|
|
491
|
+
environment.window
|
|
492
|
+
);
|
|
493
|
+
case "ajax":
|
|
494
|
+
return extractAjax(strategy, document, environment, depth);
|
|
495
|
+
default:
|
|
496
|
+
return [];
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// src/engine.ts
|
|
501
|
+
function resolveSelectors(base, overrides) {
|
|
502
|
+
const resolved = {
|
|
503
|
+
search: { ...base.search, ...overrides.search },
|
|
504
|
+
details: { ...base.details, ...overrides.details },
|
|
505
|
+
chapters: { ...base.chapters, ...overrides.chapters },
|
|
506
|
+
pages: { strategies: overrides.pages?.strategies ?? base.pages.strategies }
|
|
507
|
+
};
|
|
508
|
+
const attribution = overrides.attribution ? { ...base.attribution, ...overrides.attribution } : base.attribution;
|
|
509
|
+
if (attribution) {
|
|
510
|
+
resolved.attribution = attribution;
|
|
511
|
+
}
|
|
512
|
+
return resolved;
|
|
513
|
+
}
|
|
514
|
+
function selectorsFor(runtime) {
|
|
515
|
+
return resolveSelectors(runtime.adapter.selectors, runtime.context.overrides);
|
|
516
|
+
}
|
|
517
|
+
function readText(root, selector) {
|
|
518
|
+
if (!selector) {
|
|
519
|
+
return void 0;
|
|
520
|
+
}
|
|
521
|
+
const value = textOf(root.querySelector(selector));
|
|
522
|
+
return value === "" ? void 0 : value;
|
|
523
|
+
}
|
|
524
|
+
function readImageURL(root, selector, baseURL) {
|
|
525
|
+
if (!selector) {
|
|
526
|
+
return void 0;
|
|
527
|
+
}
|
|
528
|
+
const element = root.querySelector(selector);
|
|
529
|
+
if (!element) {
|
|
530
|
+
return void 0;
|
|
531
|
+
}
|
|
532
|
+
const image = element.querySelector("img") ?? element;
|
|
533
|
+
const value = firstAttribute(image, ["data-src", "src", "data-original"]);
|
|
534
|
+
return value ? resolveURL(value, baseURL) : void 0;
|
|
535
|
+
}
|
|
536
|
+
function splitList(value) {
|
|
537
|
+
if (!value) {
|
|
538
|
+
return void 0;
|
|
539
|
+
}
|
|
540
|
+
const items = value.split(/[,|\n]/).map((item) => item.trim()).filter((item) => item !== "");
|
|
541
|
+
return items.length > 0 ? items : void 0;
|
|
542
|
+
}
|
|
543
|
+
function normalizeStatus(value) {
|
|
544
|
+
const text = value.trim().toLowerCase();
|
|
545
|
+
if (text === "") {
|
|
546
|
+
return "unknown";
|
|
547
|
+
}
|
|
548
|
+
if (/(ongoing|current|publishing|in progress)/.test(text)) {
|
|
549
|
+
return "ongoing";
|
|
550
|
+
}
|
|
551
|
+
if (/(completed|complete|finished|ended)/.test(text)) {
|
|
552
|
+
return "completed";
|
|
553
|
+
}
|
|
554
|
+
if (/(hiatus|on hold|paused)/.test(text)) {
|
|
555
|
+
return "hiatus";
|
|
556
|
+
}
|
|
557
|
+
if (/(cancelled|canceled|dropped|axed)/.test(text)) {
|
|
558
|
+
return "cancelled";
|
|
559
|
+
}
|
|
560
|
+
return "unknown";
|
|
561
|
+
}
|
|
562
|
+
function normalizeContentRating(value) {
|
|
563
|
+
const text = value.trim().toLowerCase();
|
|
564
|
+
if (text === "") {
|
|
565
|
+
return "unknown";
|
|
566
|
+
}
|
|
567
|
+
if (/(safe|all ages|general)/.test(text)) {
|
|
568
|
+
return "safe";
|
|
569
|
+
}
|
|
570
|
+
if (/(suggestive|teen|mature teen)/.test(text)) {
|
|
571
|
+
return "suggestive";
|
|
572
|
+
}
|
|
573
|
+
if (/(mature|adult|explicit|18\+)/.test(text)) {
|
|
574
|
+
return "mature";
|
|
575
|
+
}
|
|
576
|
+
return "unknown";
|
|
577
|
+
}
|
|
578
|
+
function parseChapterNumber(value) {
|
|
579
|
+
if (!value) {
|
|
580
|
+
return void 0;
|
|
581
|
+
}
|
|
582
|
+
const match = /(\d+(?:\.\d+)?)/.exec(value);
|
|
583
|
+
if (!match || match[1] === void 0) {
|
|
584
|
+
return void 0;
|
|
585
|
+
}
|
|
586
|
+
const number = Number(match[1]);
|
|
587
|
+
return Number.isFinite(number) ? number : void 0;
|
|
588
|
+
}
|
|
589
|
+
async function extractSearch(runtime, query) {
|
|
590
|
+
const hook = runtime.adapter.hooks?.search;
|
|
591
|
+
if (hook) {
|
|
592
|
+
return hook(runtime.context, query);
|
|
593
|
+
}
|
|
594
|
+
const selectors = selectorsFor(runtime).search;
|
|
595
|
+
const items = [];
|
|
596
|
+
for (const element of queryAll(runtime.document, selectors.item)) {
|
|
597
|
+
const title = textOf(element.querySelector(selectors.title));
|
|
598
|
+
const link = element.querySelector(selectors.link);
|
|
599
|
+
const href = link ? attr(link, "href") : null;
|
|
600
|
+
if (title === "" || !href) {
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
const summary = {
|
|
604
|
+
key: resolveURL(href, runtime.context.baseURL),
|
|
605
|
+
title
|
|
606
|
+
};
|
|
607
|
+
const cover = readImageURL(element, selectors.cover, runtime.context.baseURL);
|
|
608
|
+
if (cover) {
|
|
609
|
+
summary.coverURL = cover;
|
|
610
|
+
}
|
|
611
|
+
items.push(summary);
|
|
612
|
+
}
|
|
613
|
+
const hasNextPage = selectors.nextPage ? runtime.document.querySelector(selectors.nextPage) !== null : false;
|
|
614
|
+
return { items, hasNextPage };
|
|
615
|
+
}
|
|
616
|
+
async function extractDetails(runtime, key) {
|
|
617
|
+
const hook = runtime.adapter.hooks?.getMangaDetails;
|
|
618
|
+
if (hook) {
|
|
619
|
+
return hook(runtime.context, key);
|
|
620
|
+
}
|
|
621
|
+
const selectors = selectorsFor(runtime);
|
|
622
|
+
const detailsSelectors = selectors.details;
|
|
623
|
+
const document = runtime.document;
|
|
624
|
+
const baseURL = runtime.context.baseURL;
|
|
625
|
+
const details = {
|
|
626
|
+
key,
|
|
627
|
+
title: readText(document, detailsSelectors.title) ?? key,
|
|
628
|
+
status: normalizeStatus(readText(document, detailsSelectors.status) ?? ""),
|
|
629
|
+
contentRating: normalizeContentRating(
|
|
630
|
+
readText(document, detailsSelectors.contentRating) ?? ""
|
|
631
|
+
)
|
|
632
|
+
};
|
|
633
|
+
const coverURL = readImageURL(document, detailsSelectors.cover, baseURL);
|
|
634
|
+
if (coverURL) {
|
|
635
|
+
details.coverURL = coverURL;
|
|
636
|
+
}
|
|
637
|
+
const optionalText = [
|
|
638
|
+
["synopsis", readText(document, detailsSelectors.synopsis)]
|
|
639
|
+
];
|
|
640
|
+
for (const [field, value] of optionalText) {
|
|
641
|
+
if (value) {
|
|
642
|
+
details[field] = value;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
const altTitles = splitList(readText(document, detailsSelectors.altTitles));
|
|
646
|
+
if (altTitles) {
|
|
647
|
+
details.altTitles = altTitles;
|
|
648
|
+
}
|
|
649
|
+
const authors = splitList(readText(document, detailsSelectors.authors));
|
|
650
|
+
if (authors) {
|
|
651
|
+
details.authors = authors;
|
|
652
|
+
}
|
|
653
|
+
const artists = splitList(readText(document, detailsSelectors.artists));
|
|
654
|
+
if (artists) {
|
|
655
|
+
details.artists = artists;
|
|
656
|
+
}
|
|
657
|
+
const tags = splitList(readText(document, detailsSelectors.tags));
|
|
658
|
+
if (tags) {
|
|
659
|
+
details.tags = tags;
|
|
660
|
+
}
|
|
661
|
+
const attributionSelectors = selectors.attribution;
|
|
662
|
+
const logoURL = readImageURL(document, attributionSelectors?.logo, baseURL);
|
|
663
|
+
const credits = readText(document, attributionSelectors?.credits);
|
|
664
|
+
if (logoURL || credits) {
|
|
665
|
+
const attribution = {
|
|
666
|
+
siteName: runtime.context.siteName,
|
|
667
|
+
siteURL: baseURL
|
|
668
|
+
};
|
|
669
|
+
if (logoURL) {
|
|
670
|
+
attribution.logoURL = logoURL;
|
|
671
|
+
}
|
|
672
|
+
if (credits) {
|
|
673
|
+
attribution.credits = credits;
|
|
674
|
+
}
|
|
675
|
+
details.attribution = attribution;
|
|
676
|
+
}
|
|
677
|
+
return details;
|
|
678
|
+
}
|
|
679
|
+
async function extractChapters(runtime, key) {
|
|
680
|
+
const hook = runtime.adapter.hooks?.getChapters;
|
|
681
|
+
if (hook) {
|
|
682
|
+
return hook(runtime.context, key);
|
|
683
|
+
}
|
|
684
|
+
const selectors = selectorsFor(runtime).chapters;
|
|
685
|
+
const baseURL = runtime.context.baseURL;
|
|
686
|
+
const chapters = [];
|
|
687
|
+
for (const element of queryAll(runtime.document, selectors.item)) {
|
|
688
|
+
const link = element.querySelector(selectors.link);
|
|
689
|
+
const href = link ? attr(link, "href") : null;
|
|
690
|
+
if (!href) {
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
const title = selectors.title ? readText(element, selectors.title) : textOf(link) || void 0;
|
|
694
|
+
const chapter = { key: resolveURL(href, baseURL) };
|
|
695
|
+
if (title) {
|
|
696
|
+
chapter.title = title;
|
|
697
|
+
}
|
|
698
|
+
const number = parseChapterNumber(title);
|
|
699
|
+
if (number !== void 0) {
|
|
700
|
+
chapter.number = number;
|
|
701
|
+
}
|
|
702
|
+
const dateText = readText(element, selectors.date);
|
|
703
|
+
if (dateText) {
|
|
704
|
+
const time = Date.parse(dateText);
|
|
705
|
+
if (!Number.isNaN(time)) {
|
|
706
|
+
chapter.publishedAt = new Date(time).toISOString();
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
chapters.push(chapter);
|
|
710
|
+
}
|
|
711
|
+
const numeric = chapters.every((chapter) => chapter.number !== void 0);
|
|
712
|
+
if (numeric) {
|
|
713
|
+
chapters.sort((a, b) => {
|
|
714
|
+
const left = a.number ?? 0;
|
|
715
|
+
const right = b.number ?? 0;
|
|
716
|
+
return selectors.order === "asc" ? left - right : right - left;
|
|
717
|
+
});
|
|
718
|
+
} else if (selectors.order === "asc") {
|
|
719
|
+
chapters.reverse();
|
|
720
|
+
}
|
|
721
|
+
return chapters;
|
|
722
|
+
}
|
|
723
|
+
async function extractPages(runtime, keys) {
|
|
724
|
+
const hook = runtime.adapter.hooks?.getPages;
|
|
725
|
+
if (hook) {
|
|
726
|
+
return hook(runtime.context, keys.mangaKey, keys.chapterKey);
|
|
727
|
+
}
|
|
728
|
+
const selectors = selectorsFor(runtime);
|
|
729
|
+
const environment = { baseURL: runtime.context.baseURL };
|
|
730
|
+
if (runtime.window) {
|
|
731
|
+
environment.window = runtime.window;
|
|
732
|
+
}
|
|
733
|
+
if (runtime.parseHTML) {
|
|
734
|
+
environment.parseHTML = runtime.parseHTML;
|
|
735
|
+
}
|
|
736
|
+
for (const strategy of selectors.pages.strategies) {
|
|
737
|
+
const pages = await runStrategy(strategy, runtime.document, environment);
|
|
738
|
+
if (pages.length > 0) {
|
|
739
|
+
return pages;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
return [];
|
|
743
|
+
}
|
|
744
|
+
var extract = extractPages;
|
|
745
|
+
function asRecord(value) {
|
|
746
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
747
|
+
}
|
|
748
|
+
function toJSON(value) {
|
|
749
|
+
return JSON.stringify(value) ?? "null";
|
|
750
|
+
}
|
|
751
|
+
async function dispatch(runtime, method, argsJSON) {
|
|
752
|
+
const args = asRecord(argsJSON.trim() === "" ? {} : JSON.parse(argsJSON));
|
|
753
|
+
switch (method) {
|
|
754
|
+
case "search": {
|
|
755
|
+
const filters = asRecord(args.filters);
|
|
756
|
+
const filterRecord = {};
|
|
757
|
+
for (const [name, value] of Object.entries(filters)) {
|
|
758
|
+
filterRecord[name] = String(value);
|
|
759
|
+
}
|
|
760
|
+
const query = {
|
|
761
|
+
page: typeof args.page === "number" ? args.page : 1
|
|
762
|
+
};
|
|
763
|
+
if (typeof args.text === "string") {
|
|
764
|
+
query.text = args.text;
|
|
765
|
+
}
|
|
766
|
+
if (Object.keys(filterRecord).length > 0) {
|
|
767
|
+
query.filters = filterRecord;
|
|
768
|
+
}
|
|
769
|
+
return toJSON(await extractSearch(runtime, query));
|
|
770
|
+
}
|
|
771
|
+
case "details":
|
|
772
|
+
case "getMangaDetails":
|
|
773
|
+
return toJSON(await extractDetails(runtime, String(args.key ?? "")));
|
|
774
|
+
case "chapters":
|
|
775
|
+
case "getChapters":
|
|
776
|
+
return toJSON(await extractChapters(runtime, String(args.key ?? "")));
|
|
777
|
+
case "pages":
|
|
778
|
+
case "getPages":
|
|
779
|
+
return toJSON(
|
|
780
|
+
await extractPages(runtime, {
|
|
781
|
+
mangaKey: String(args.mangaKey ?? ""),
|
|
782
|
+
chapterKey: String(args.chapterKey ?? "")
|
|
783
|
+
})
|
|
784
|
+
);
|
|
785
|
+
default:
|
|
786
|
+
throw new Error(`Unknown adapter method: ${method}`);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
return __toCommonJS(index_exports);
|
|
790
|
+
})();
|