@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 BlankGrimoire contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,60 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @blankgrimoire/runtime
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Declarative extraction engine for BlankGrimoire theme adapters. It runs in
|
|
4
|
+
JavaScriptCore on iOS, in Node for testing, and inside the signed bundle
|
|
5
|
+
built by `@blankgrimoire/cli`.
|
|
6
|
+
|
|
7
|
+
## Host contract
|
|
8
|
+
|
|
9
|
+
The runtime never assumes a global DOM. The host provides:
|
|
10
|
+
|
|
11
|
+
- `DocumentLike`: `querySelector`, `querySelectorAll`, `textContent`,
|
|
12
|
+
`getAttribute`; HTML is parsed by the host through
|
|
13
|
+
`__native.parseHTML(html)`.
|
|
14
|
+
- `__native.request(HttpRequest)` for HTTP, plus optional timers and logging.
|
|
15
|
+
- A `SiteContext` with `baseURL`, `siteName` and user selector overrides.
|
|
16
|
+
|
|
17
|
+
## Dispatching calls
|
|
18
|
+
|
|
19
|
+
The host calls one JSON entry point:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { dispatch } from '@blankgrimoire/runtime';
|
|
23
|
+
|
|
24
|
+
const result = await dispatch(runtime, 'search', JSON.stringify({ page: 1 }));
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`runtime` contains the adapter, `SiteContext`, the parsed `document`, and
|
|
28
|
+
optional `window` / `parseHTML` hooks. The result is always a JSON string.
|
|
29
|
+
|
|
30
|
+
Methods: `search`, `details`, `chapters`, `pages` (the `getMangaDetails`,
|
|
31
|
+
`getChapters` and `getPages` aliases are also accepted).
|
|
32
|
+
|
|
33
|
+
## Extraction
|
|
34
|
+
|
|
35
|
+
- Search, details and chapters are extracted declaratively from
|
|
36
|
+
`ThemeSelectors`. TypeScript hooks take precedence when present.
|
|
37
|
+
- Pages use ordered strategies (`css`, `scriptJSON`, `hydration`, `ajax`);
|
|
38
|
+
the first strategy that returns at least one page wins.
|
|
39
|
+
- User overrides are deep-partial and merge field by field without replacing
|
|
40
|
+
sibling selector fields.
|
|
41
|
+
- Status and content-rating text are normalised to the closed contract unions,
|
|
42
|
+
falling back to `unknown`.
|
|
43
|
+
- Relative URLs are resolved by the runtime itself; JavaScriptCore does not
|
|
44
|
+
need to provide a global `URL` constructor.
|
|
45
|
+
|
|
46
|
+
## Packaging
|
|
47
|
+
|
|
48
|
+
- `dist/index.js` — ESM entry with type declarations for Node tooling.
|
|
49
|
+
- `dist/runtime.js` — single IIFE exposing the `BlankGrimoireRuntime` global
|
|
50
|
+
for JavaScriptCore. `__apiVersion` equals `API_VERSION` (`1`).
|
|
51
|
+
|
|
52
|
+
## Development
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pnpm build # declarations + ESM + IIFE bundle
|
|
56
|
+
pnpm typecheck # src and tests
|
|
57
|
+
pnpm test
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
License: MIT — © BlankGrimoire contributors.
|
package/dist/css.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Page } from '@blankgrimoire/types';
|
|
2
|
+
import type { DocumentLike } from './dom.js';
|
|
3
|
+
export interface CssStrategy {
|
|
4
|
+
kind: 'css';
|
|
5
|
+
image: string;
|
|
6
|
+
srcAttributes: string[];
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
/** Extract every page image matching the selector, preserving document order. */
|
|
10
|
+
export declare function extractCss(strategy: CssStrategy, document: DocumentLike, baseURL: string): Page[];
|
package/dist/css.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
import { firstAttribute, queryAll, resolveURL } from './dom.js';
|
|
3
|
+
/** Extract every page image matching the selector, preserving document order. */
|
|
4
|
+
export function extractCss(strategy, document, baseURL) {
|
|
5
|
+
const pages = [];
|
|
6
|
+
for (const element of queryAll(document, strategy.image)) {
|
|
7
|
+
const value = firstAttribute(element, strategy.srcAttributes);
|
|
8
|
+
if (!value) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
const page = {
|
|
12
|
+
index: pages.length,
|
|
13
|
+
imageURL: resolveURL(value, baseURL),
|
|
14
|
+
};
|
|
15
|
+
if (strategy.headers) {
|
|
16
|
+
page.headers = { ...strategy.headers };
|
|
17
|
+
}
|
|
18
|
+
pages.push(page);
|
|
19
|
+
}
|
|
20
|
+
return pages;
|
|
21
|
+
}
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal DOM surface used by the runtime. JavaScriptCore does not provide a
|
|
3
|
+
* DOM, so the host parses HTML through `__native.parseHTML` and passes a
|
|
4
|
+
* structural implementation of these interfaces. Node tests use jsdom.
|
|
5
|
+
*/
|
|
6
|
+
export interface ElementLike {
|
|
7
|
+
getAttribute(name: string): string | null;
|
|
8
|
+
readonly textContent: string | null;
|
|
9
|
+
querySelector(selector: string): ElementLike | null;
|
|
10
|
+
querySelectorAll(selector: string): ArrayLike<ElementLike>;
|
|
11
|
+
innerHTML?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface DocumentLike {
|
|
14
|
+
querySelector(selector: string): ElementLike | null;
|
|
15
|
+
querySelectorAll(selector: string): ArrayLike<ElementLike>;
|
|
16
|
+
documentElement?: ElementLike | null;
|
|
17
|
+
implementation?: {
|
|
18
|
+
createHTMLDocument(title?: string): DocumentLike;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** The subset of the host `window` used by hydration strategies. */
|
|
22
|
+
export interface WindowLike {
|
|
23
|
+
__NEXT_DATA__?: unknown;
|
|
24
|
+
__NUXT__?: unknown;
|
|
25
|
+
}
|
|
26
|
+
export type ParseHTML = (html: string) => DocumentLike;
|
|
27
|
+
export declare function queryAll(root: DocumentLike | ElementLike, selector: string): ElementLike[];
|
|
28
|
+
export declare function textOf(element: ElementLike | null | undefined): string;
|
|
29
|
+
export declare function attr(element: ElementLike, name: string): string | null;
|
|
30
|
+
/**
|
|
31
|
+
* Resolve a relative URL without relying on the global `URL` constructor,
|
|
32
|
+
* which JavaScriptCore does not guarantee. The native implementation is
|
|
33
|
+
* preferred when available for full WHATWG semantics.
|
|
34
|
+
*/
|
|
35
|
+
export declare function resolveRelativeURL(value: string, baseURL: string): string;
|
|
36
|
+
/** Resolve a relative URL against the site base URL. */
|
|
37
|
+
export declare function resolveURL(value: string, baseURL: string): string;
|
|
38
|
+
/** Read the first non-empty attribute among `names`. */
|
|
39
|
+
export declare function firstAttribute(element: ElementLike, names: string[]): string | null;
|
package/dist/dom.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
export function queryAll(root, selector) {
|
|
3
|
+
return Array.from(root.querySelectorAll(selector));
|
|
4
|
+
}
|
|
5
|
+
export function textOf(element) {
|
|
6
|
+
return (element?.textContent ?? '').trim();
|
|
7
|
+
}
|
|
8
|
+
export function attr(element, name) {
|
|
9
|
+
const value = element.getAttribute(name);
|
|
10
|
+
return value !== null && value.trim() !== '' ? value.trim() : null;
|
|
11
|
+
}
|
|
12
|
+
const ABSOLUTE_URL = /^[A-Za-z][A-Za-z0-9+.-]*:/;
|
|
13
|
+
const BASE_URL = /^([A-Za-z][A-Za-z0-9+.-]*:)(\/\/[^/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?$/;
|
|
14
|
+
function normalizePath(path) {
|
|
15
|
+
const segments = [];
|
|
16
|
+
for (const segment of path.split('/')) {
|
|
17
|
+
if (segment === '' || segment === '.') {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (segment === '..') {
|
|
21
|
+
segments.pop();
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
segments.push(segment);
|
|
25
|
+
}
|
|
26
|
+
const leading = path.startsWith('/') ? '/' : '';
|
|
27
|
+
const trailing = path.endsWith('/') && segments.length > 0 ? '/' : '';
|
|
28
|
+
return `${leading}${segments.join('/')}${trailing}`;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Resolve a relative URL without relying on the global `URL` constructor,
|
|
32
|
+
* which JavaScriptCore does not guarantee. The native implementation is
|
|
33
|
+
* preferred when available for full WHATWG semantics.
|
|
34
|
+
*/
|
|
35
|
+
export function resolveRelativeURL(value, baseURL) {
|
|
36
|
+
if (ABSOLUTE_URL.test(value)) {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
if (value.startsWith('//')) {
|
|
40
|
+
const scheme = /^[A-Za-z][A-Za-z0-9+.-]*:/.exec(baseURL)?.[0] ?? '';
|
|
41
|
+
return `${scheme}${value}`;
|
|
42
|
+
}
|
|
43
|
+
const base = BASE_URL.exec(baseURL);
|
|
44
|
+
if (!base) {
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
const scheme = base[1] ?? '';
|
|
48
|
+
const authority = base[2] ?? '';
|
|
49
|
+
const basePath = base[3] ?? '';
|
|
50
|
+
const baseQuery = base[4] ?? '';
|
|
51
|
+
const hashIndex = value.indexOf('#');
|
|
52
|
+
const hash = hashIndex >= 0 ? value.slice(hashIndex) : '';
|
|
53
|
+
const withoutHash = hashIndex >= 0 ? value.slice(0, hashIndex) : value;
|
|
54
|
+
const queryIndex = withoutHash.indexOf('?');
|
|
55
|
+
const query = queryIndex >= 0 ? withoutHash.slice(queryIndex) : '';
|
|
56
|
+
const path = queryIndex >= 0 ? withoutHash.slice(0, queryIndex) : withoutHash;
|
|
57
|
+
if (path === '') {
|
|
58
|
+
return `${scheme}${authority}${basePath}${query || baseQuery}${hash}`;
|
|
59
|
+
}
|
|
60
|
+
let resolvedPath;
|
|
61
|
+
if (path.startsWith('/')) {
|
|
62
|
+
resolvedPath = normalizePath(path);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const directory = basePath.slice(0, basePath.lastIndexOf('/') + 1) || '/';
|
|
66
|
+
resolvedPath = normalizePath(`${directory}${path}`);
|
|
67
|
+
}
|
|
68
|
+
return `${scheme}${authority}${resolvedPath}${query}${hash}`;
|
|
69
|
+
}
|
|
70
|
+
/** Resolve a relative URL against the site base URL. */
|
|
71
|
+
export function resolveURL(value, baseURL) {
|
|
72
|
+
const input = value.trim();
|
|
73
|
+
if (input === '') {
|
|
74
|
+
return baseURL;
|
|
75
|
+
}
|
|
76
|
+
if (typeof URL === 'function') {
|
|
77
|
+
try {
|
|
78
|
+
return new URL(input, baseURL).toString();
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// Fall through to the self-contained resolver.
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return resolveRelativeURL(input, baseURL);
|
|
85
|
+
}
|
|
86
|
+
/** Read the first non-empty attribute among `names`. */
|
|
87
|
+
export function firstAttribute(element, names) {
|
|
88
|
+
for (const name of names) {
|
|
89
|
+
const value = attr(element, name);
|
|
90
|
+
if (value) {
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Chapter, ContentRating, MangaDetails, MangaStatus, MangaSummary, Page, PagedResult, SearchQuery, SiteContext, ThemeAdapter, ThemeSelectors, ThemeSelectorsOverrides } from '@blankgrimoire/types';
|
|
2
|
+
import type { DocumentLike, ParseHTML, WindowLike } from './dom.js';
|
|
3
|
+
export interface ThemeRuntimeOptions {
|
|
4
|
+
adapter: ThemeAdapter;
|
|
5
|
+
context: SiteContext;
|
|
6
|
+
document: DocumentLike;
|
|
7
|
+
window?: WindowLike;
|
|
8
|
+
parseHTML?: ParseHTML;
|
|
9
|
+
}
|
|
10
|
+
export type ExtractionResult = Page[];
|
|
11
|
+
export interface PageKeys {
|
|
12
|
+
mangaKey: string;
|
|
13
|
+
chapterKey: string;
|
|
14
|
+
}
|
|
15
|
+
/** Merge field-by-field user overrides into the adapter selectors. */
|
|
16
|
+
export declare function resolveSelectors(base: ThemeSelectors, overrides: ThemeSelectorsOverrides): ThemeSelectors;
|
|
17
|
+
/** Normalise site text into the closed `MangaStatus` union. */
|
|
18
|
+
export declare function normalizeStatus(value: string): MangaStatus;
|
|
19
|
+
/** Normalise site text into the closed `ContentRating` union. */
|
|
20
|
+
export declare function normalizeContentRating(value: string): ContentRating;
|
|
21
|
+
/** Search a site. Hooks take precedence over declarative selectors. */
|
|
22
|
+
export declare function extractSearch(runtime: ThemeRuntimeOptions, query: SearchQuery): Promise<PagedResult<MangaSummary>>;
|
|
23
|
+
/** Read series details. Hooks take precedence over declarative selectors. */
|
|
24
|
+
export declare function extractDetails(runtime: ThemeRuntimeOptions, key: string): Promise<MangaDetails>;
|
|
25
|
+
/** Read a chapter list. Hooks take precedence over declarative selectors. */
|
|
26
|
+
export declare function extractChapters(runtime: ThemeRuntimeOptions, key: string): Promise<Chapter[]>;
|
|
27
|
+
/** Extract chapter pages. Hooks take precedence over declarative strategies. */
|
|
28
|
+
export declare function extractPages(runtime: ThemeRuntimeOptions, keys: PageKeys): Promise<Page[]>;
|
|
29
|
+
/** Backwards-compatible name for page extraction. */
|
|
30
|
+
export declare const extract: typeof extractPages;
|
|
31
|
+
/**
|
|
32
|
+
* JSON dispatcher used by the host. Every exchange value is a JSON string.
|
|
33
|
+
* Method aliases match the `ThemeHooks` names.
|
|
34
|
+
*/
|
|
35
|
+
export declare function dispatch(runtime: ThemeRuntimeOptions, method: string, argsJSON: string): Promise<string>;
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
import { attr, firstAttribute, queryAll, resolveURL, textOf } from './dom.js';
|
|
3
|
+
import { runStrategy } from './strategies.js';
|
|
4
|
+
/** Merge field-by-field user overrides into the adapter selectors. */
|
|
5
|
+
export function resolveSelectors(base, overrides) {
|
|
6
|
+
const resolved = {
|
|
7
|
+
search: { ...base.search, ...overrides.search },
|
|
8
|
+
details: { ...base.details, ...overrides.details },
|
|
9
|
+
chapters: { ...base.chapters, ...overrides.chapters },
|
|
10
|
+
pages: { strategies: overrides.pages?.strategies ?? base.pages.strategies },
|
|
11
|
+
};
|
|
12
|
+
const attribution = overrides.attribution
|
|
13
|
+
? { ...base.attribution, ...overrides.attribution }
|
|
14
|
+
: base.attribution;
|
|
15
|
+
if (attribution) {
|
|
16
|
+
resolved.attribution = attribution;
|
|
17
|
+
}
|
|
18
|
+
return resolved;
|
|
19
|
+
}
|
|
20
|
+
function selectorsFor(runtime) {
|
|
21
|
+
return resolveSelectors(runtime.adapter.selectors, runtime.context.overrides);
|
|
22
|
+
}
|
|
23
|
+
function readText(root, selector) {
|
|
24
|
+
if (!selector) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
const value = textOf(root.querySelector(selector));
|
|
28
|
+
return value === '' ? undefined : value;
|
|
29
|
+
}
|
|
30
|
+
function readImageURL(root, selector, baseURL) {
|
|
31
|
+
if (!selector) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
const element = root.querySelector(selector);
|
|
35
|
+
if (!element) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const image = element.querySelector('img') ?? element;
|
|
39
|
+
const value = firstAttribute(image, ['data-src', 'src', 'data-original']);
|
|
40
|
+
return value ? resolveURL(value, baseURL) : undefined;
|
|
41
|
+
}
|
|
42
|
+
function splitList(value) {
|
|
43
|
+
if (!value) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
const items = value
|
|
47
|
+
.split(/[,|\n]/)
|
|
48
|
+
.map((item) => item.trim())
|
|
49
|
+
.filter((item) => item !== '');
|
|
50
|
+
return items.length > 0 ? items : undefined;
|
|
51
|
+
}
|
|
52
|
+
/** Normalise site text into the closed `MangaStatus` union. */
|
|
53
|
+
export function normalizeStatus(value) {
|
|
54
|
+
const text = value.trim().toLowerCase();
|
|
55
|
+
if (text === '') {
|
|
56
|
+
return 'unknown';
|
|
57
|
+
}
|
|
58
|
+
if (/(ongoing|current|publishing|in progress)/.test(text)) {
|
|
59
|
+
return 'ongoing';
|
|
60
|
+
}
|
|
61
|
+
if (/(completed|complete|finished|ended)/.test(text)) {
|
|
62
|
+
return 'completed';
|
|
63
|
+
}
|
|
64
|
+
if (/(hiatus|on hold|paused)/.test(text)) {
|
|
65
|
+
return 'hiatus';
|
|
66
|
+
}
|
|
67
|
+
if (/(cancelled|canceled|dropped|axed)/.test(text)) {
|
|
68
|
+
return 'cancelled';
|
|
69
|
+
}
|
|
70
|
+
return 'unknown';
|
|
71
|
+
}
|
|
72
|
+
/** Normalise site text into the closed `ContentRating` union. */
|
|
73
|
+
export function normalizeContentRating(value) {
|
|
74
|
+
const text = value.trim().toLowerCase();
|
|
75
|
+
if (text === '') {
|
|
76
|
+
return 'unknown';
|
|
77
|
+
}
|
|
78
|
+
if (/(safe|all ages|general)/.test(text)) {
|
|
79
|
+
return 'safe';
|
|
80
|
+
}
|
|
81
|
+
if (/(suggestive|teen|mature teen)/.test(text)) {
|
|
82
|
+
return 'suggestive';
|
|
83
|
+
}
|
|
84
|
+
if (/(mature|adult|explicit|18\+)/.test(text)) {
|
|
85
|
+
return 'mature';
|
|
86
|
+
}
|
|
87
|
+
return 'unknown';
|
|
88
|
+
}
|
|
89
|
+
function parseChapterNumber(value) {
|
|
90
|
+
if (!value) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
const match = /(\d+(?:\.\d+)?)/.exec(value);
|
|
94
|
+
if (!match || match[1] === undefined) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
const number = Number(match[1]);
|
|
98
|
+
return Number.isFinite(number) ? number : undefined;
|
|
99
|
+
}
|
|
100
|
+
/** Search a site. Hooks take precedence over declarative selectors. */
|
|
101
|
+
export async function extractSearch(runtime, query) {
|
|
102
|
+
const hook = runtime.adapter.hooks?.search;
|
|
103
|
+
if (hook) {
|
|
104
|
+
return hook(runtime.context, query);
|
|
105
|
+
}
|
|
106
|
+
const selectors = selectorsFor(runtime).search;
|
|
107
|
+
const items = [];
|
|
108
|
+
for (const element of queryAll(runtime.document, selectors.item)) {
|
|
109
|
+
const title = textOf(element.querySelector(selectors.title));
|
|
110
|
+
const link = element.querySelector(selectors.link);
|
|
111
|
+
const href = link ? attr(link, 'href') : null;
|
|
112
|
+
if (title === '' || !href) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const summary = {
|
|
116
|
+
key: resolveURL(href, runtime.context.baseURL),
|
|
117
|
+
title,
|
|
118
|
+
};
|
|
119
|
+
const cover = readImageURL(element, selectors.cover, runtime.context.baseURL);
|
|
120
|
+
if (cover) {
|
|
121
|
+
summary.coverURL = cover;
|
|
122
|
+
}
|
|
123
|
+
items.push(summary);
|
|
124
|
+
}
|
|
125
|
+
const hasNextPage = selectors.nextPage
|
|
126
|
+
? runtime.document.querySelector(selectors.nextPage) !== null
|
|
127
|
+
: false;
|
|
128
|
+
return { items, hasNextPage };
|
|
129
|
+
}
|
|
130
|
+
/** Read series details. Hooks take precedence over declarative selectors. */
|
|
131
|
+
export async function extractDetails(runtime, key) {
|
|
132
|
+
const hook = runtime.adapter.hooks?.getMangaDetails;
|
|
133
|
+
if (hook) {
|
|
134
|
+
return hook(runtime.context, key);
|
|
135
|
+
}
|
|
136
|
+
const selectors = selectorsFor(runtime);
|
|
137
|
+
const detailsSelectors = selectors.details;
|
|
138
|
+
const document = runtime.document;
|
|
139
|
+
const baseURL = runtime.context.baseURL;
|
|
140
|
+
const details = {
|
|
141
|
+
key,
|
|
142
|
+
title: readText(document, detailsSelectors.title) ?? key,
|
|
143
|
+
status: normalizeStatus(readText(document, detailsSelectors.status) ?? ''),
|
|
144
|
+
contentRating: normalizeContentRating(readText(document, detailsSelectors.contentRating) ?? ''),
|
|
145
|
+
};
|
|
146
|
+
const coverURL = readImageURL(document, detailsSelectors.cover, baseURL);
|
|
147
|
+
if (coverURL) {
|
|
148
|
+
details.coverURL = coverURL;
|
|
149
|
+
}
|
|
150
|
+
const optionalText = [
|
|
151
|
+
['synopsis', readText(document, detailsSelectors.synopsis)],
|
|
152
|
+
];
|
|
153
|
+
for (const [field, value] of optionalText) {
|
|
154
|
+
if (value) {
|
|
155
|
+
details[field] = value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const altTitles = splitList(readText(document, detailsSelectors.altTitles));
|
|
159
|
+
if (altTitles) {
|
|
160
|
+
details.altTitles = altTitles;
|
|
161
|
+
}
|
|
162
|
+
const authors = splitList(readText(document, detailsSelectors.authors));
|
|
163
|
+
if (authors) {
|
|
164
|
+
details.authors = authors;
|
|
165
|
+
}
|
|
166
|
+
const artists = splitList(readText(document, detailsSelectors.artists));
|
|
167
|
+
if (artists) {
|
|
168
|
+
details.artists = artists;
|
|
169
|
+
}
|
|
170
|
+
const tags = splitList(readText(document, detailsSelectors.tags));
|
|
171
|
+
if (tags) {
|
|
172
|
+
details.tags = tags;
|
|
173
|
+
}
|
|
174
|
+
const attributionSelectors = selectors.attribution;
|
|
175
|
+
const logoURL = readImageURL(document, attributionSelectors?.logo, baseURL);
|
|
176
|
+
const credits = readText(document, attributionSelectors?.credits);
|
|
177
|
+
if (logoURL || credits) {
|
|
178
|
+
const attribution = {
|
|
179
|
+
siteName: runtime.context.siteName,
|
|
180
|
+
siteURL: baseURL,
|
|
181
|
+
};
|
|
182
|
+
if (logoURL) {
|
|
183
|
+
attribution.logoURL = logoURL;
|
|
184
|
+
}
|
|
185
|
+
if (credits) {
|
|
186
|
+
attribution.credits = credits;
|
|
187
|
+
}
|
|
188
|
+
details.attribution = attribution;
|
|
189
|
+
}
|
|
190
|
+
return details;
|
|
191
|
+
}
|
|
192
|
+
/** Read a chapter list. Hooks take precedence over declarative selectors. */
|
|
193
|
+
export async function extractChapters(runtime, key) {
|
|
194
|
+
const hook = runtime.adapter.hooks?.getChapters;
|
|
195
|
+
if (hook) {
|
|
196
|
+
return hook(runtime.context, key);
|
|
197
|
+
}
|
|
198
|
+
const selectors = selectorsFor(runtime).chapters;
|
|
199
|
+
const baseURL = runtime.context.baseURL;
|
|
200
|
+
const chapters = [];
|
|
201
|
+
for (const element of queryAll(runtime.document, selectors.item)) {
|
|
202
|
+
const link = element.querySelector(selectors.link);
|
|
203
|
+
const href = link ? attr(link, 'href') : null;
|
|
204
|
+
if (!href) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const title = selectors.title
|
|
208
|
+
? readText(element, selectors.title)
|
|
209
|
+
: textOf(link) || undefined;
|
|
210
|
+
const chapter = { key: resolveURL(href, baseURL) };
|
|
211
|
+
if (title) {
|
|
212
|
+
chapter.title = title;
|
|
213
|
+
}
|
|
214
|
+
const number = parseChapterNumber(title);
|
|
215
|
+
if (number !== undefined) {
|
|
216
|
+
chapter.number = number;
|
|
217
|
+
}
|
|
218
|
+
const dateText = readText(element, selectors.date);
|
|
219
|
+
if (dateText) {
|
|
220
|
+
const time = Date.parse(dateText);
|
|
221
|
+
if (!Number.isNaN(time)) {
|
|
222
|
+
chapter.publishedAt = new Date(time).toISOString();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
chapters.push(chapter);
|
|
226
|
+
}
|
|
227
|
+
const numeric = chapters.every((chapter) => chapter.number !== undefined);
|
|
228
|
+
if (numeric) {
|
|
229
|
+
chapters.sort((a, b) => {
|
|
230
|
+
const left = a.number ?? 0;
|
|
231
|
+
const right = b.number ?? 0;
|
|
232
|
+
return selectors.order === 'asc' ? left - right : right - left;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
else if (selectors.order === 'asc') {
|
|
236
|
+
chapters.reverse();
|
|
237
|
+
}
|
|
238
|
+
return chapters;
|
|
239
|
+
}
|
|
240
|
+
/** Extract chapter pages. Hooks take precedence over declarative strategies. */
|
|
241
|
+
export async function extractPages(runtime, keys) {
|
|
242
|
+
const hook = runtime.adapter.hooks?.getPages;
|
|
243
|
+
if (hook) {
|
|
244
|
+
return hook(runtime.context, keys.mangaKey, keys.chapterKey);
|
|
245
|
+
}
|
|
246
|
+
const selectors = selectorsFor(runtime);
|
|
247
|
+
const environment = { baseURL: runtime.context.baseURL };
|
|
248
|
+
if (runtime.window) {
|
|
249
|
+
environment.window = runtime.window;
|
|
250
|
+
}
|
|
251
|
+
if (runtime.parseHTML) {
|
|
252
|
+
environment.parseHTML = runtime.parseHTML;
|
|
253
|
+
}
|
|
254
|
+
for (const strategy of selectors.pages.strategies) {
|
|
255
|
+
const pages = await runStrategy(strategy, runtime.document, environment);
|
|
256
|
+
if (pages.length > 0) {
|
|
257
|
+
return pages;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return [];
|
|
261
|
+
}
|
|
262
|
+
/** Backwards-compatible name for page extraction. */
|
|
263
|
+
export const extract = extractPages;
|
|
264
|
+
function asRecord(value) {
|
|
265
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
266
|
+
? value
|
|
267
|
+
: {};
|
|
268
|
+
}
|
|
269
|
+
function toJSON(value) {
|
|
270
|
+
return JSON.stringify(value) ?? 'null';
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* JSON dispatcher used by the host. Every exchange value is a JSON string.
|
|
274
|
+
* Method aliases match the `ThemeHooks` names.
|
|
275
|
+
*/
|
|
276
|
+
export async function dispatch(runtime, method, argsJSON) {
|
|
277
|
+
const args = asRecord(argsJSON.trim() === '' ? {} : JSON.parse(argsJSON));
|
|
278
|
+
switch (method) {
|
|
279
|
+
case 'search': {
|
|
280
|
+
const filters = asRecord(args.filters);
|
|
281
|
+
const filterRecord = {};
|
|
282
|
+
for (const [name, value] of Object.entries(filters)) {
|
|
283
|
+
filterRecord[name] = String(value);
|
|
284
|
+
}
|
|
285
|
+
const query = {
|
|
286
|
+
page: typeof args.page === 'number' ? args.page : 1,
|
|
287
|
+
};
|
|
288
|
+
if (typeof args.text === 'string') {
|
|
289
|
+
query.text = args.text;
|
|
290
|
+
}
|
|
291
|
+
if (Object.keys(filterRecord).length > 0) {
|
|
292
|
+
query.filters = filterRecord;
|
|
293
|
+
}
|
|
294
|
+
return toJSON(await extractSearch(runtime, query));
|
|
295
|
+
}
|
|
296
|
+
case 'details':
|
|
297
|
+
case 'getMangaDetails':
|
|
298
|
+
return toJSON(await extractDetails(runtime, String(args.key ?? '')));
|
|
299
|
+
case 'chapters':
|
|
300
|
+
case 'getChapters':
|
|
301
|
+
return toJSON(await extractChapters(runtime, String(args.key ?? '')));
|
|
302
|
+
case 'pages':
|
|
303
|
+
case 'getPages':
|
|
304
|
+
return toJSON(await extractPages(runtime, {
|
|
305
|
+
mangaKey: String(args.mangaKey ?? ''),
|
|
306
|
+
chapterKey: String(args.chapterKey ?? ''),
|
|
307
|
+
}));
|
|
308
|
+
default:
|
|
309
|
+
throw new Error(`Unknown adapter method: ${method}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, type HttpRequest, type HttpResponse } from './prelude.js';
|
|
2
|
+
import { dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, type ExtractionResult, type PageKeys, type ThemeRuntimeOptions } from './engine.js';
|
|
3
|
+
import { extractCss, type CssStrategy } from './css.js';
|
|
4
|
+
import { extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, type AjaxStrategy, type HydrationStrategy, type ScriptJSONStrategy, type StrategyEnvironment } from './strategies.js';
|
|
5
|
+
import type { DocumentLike, ElementLike, ParseHTML, WindowLike } from './dom.js';
|
|
6
|
+
export { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, extractCss, extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, };
|
|
7
|
+
export type { HttpRequest, HttpResponse, ExtractionResult, PageKeys, ThemeRuntimeOptions, CssStrategy, ScriptJSONStrategy, HydrationStrategy, AjaxStrategy, StrategyEnvironment, DocumentLike, ElementLike, ParseHTML, WindowLike, };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
import { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, } from './prelude.js';
|
|
3
|
+
import { dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, } from './engine.js';
|
|
4
|
+
import { extractCss } from './css.js';
|
|
5
|
+
import { extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, } from './strategies.js';
|
|
6
|
+
export { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, extractCss, extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type HttpRequest, type HttpResponse } from '@blankgrimoire/types';
|
|
2
|
+
import type { DocumentLike } from './dom.js';
|
|
3
|
+
export type { HttpRequest, HttpResponse };
|
|
4
|
+
export declare const __apiVersion: 1;
|
|
5
|
+
/**
|
|
6
|
+
* Register a timer. When the host exposes `__native.setTimeout`, the callback
|
|
7
|
+
* is scheduled natively after `ms`; otherwise the host must call
|
|
8
|
+
* `__timerFire(id)`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function setTimeout(cb: () => void, ms: number): number;
|
|
11
|
+
export declare function clearTimeout(id: number): void;
|
|
12
|
+
export declare function __timerFire(id: number): void;
|
|
13
|
+
/** Call a native host function by name. */
|
|
14
|
+
export declare function __nativeInvoke(fn: string, ...args: unknown[]): unknown;
|
|
15
|
+
/** Perform a native HTTP request. */
|
|
16
|
+
export declare function __http(request: HttpRequest): Promise<HttpResponse>;
|
|
17
|
+
/**
|
|
18
|
+
* Parse HTML through the native bridge. In Node tests the host can inject a
|
|
19
|
+
* `parseHTML` implementation; jsdom documents are also accepted as a fallback.
|
|
20
|
+
*/
|
|
21
|
+
export declare function __parseHTML(html: string): DocumentLike;
|
|
22
|
+
declare const console: {
|
|
23
|
+
log: (...args: unknown[]) => void;
|
|
24
|
+
error: (...args: unknown[]) => void;
|
|
25
|
+
warn: (...args: unknown[]) => void;
|
|
26
|
+
};
|
|
27
|
+
export { console };
|