-
Notifications
You must be signed in to change notification settings - Fork 12
/
platform.ts
77 lines (63 loc) · 2.06 KB
/
platform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { GalleryMeta } from "../download/gallery-meta";
import ImageNode from "../img-node";
import { Chapter } from "../page-fetcher";
export type OriginMeta = {
url: string,
title?: string,
href?: string,
}
export interface Matcher<P> {
name(): string;
/**
* step 0: in some site, the gallery is divided into chapters
*/
fetchChapters(): Promise<Chapter[]>;
/**
* step 1: fetch page source
*/
fetchPagesSource(chapter: Chapter): AsyncGenerator<P>;
/**
* step 2: parse img nodes from page source
*/
parseImgNodes(page: P, chapterID?: number): Promise<ImageNode[] | never>;
/**
* step 3: fetch origin img url from every single image node's href
*/
fetchOriginMeta(node: ImageNode, retry: boolean, chapterID?: number): Promise<OriginMeta>;
galleryMeta(doc: Document, chapter?: Chapter): GalleryMeta;
title(doc: Document): string;
workURLs(): RegExp[];
processData(data: Uint8Array, contentType: string, url: string): Promise<[Uint8Array, string]>;
headers(): Record<string, string>;
}
export abstract class BaseMatcher<P> implements Matcher<P> {
async fetchChapters(): Promise<Chapter[]> {
return [{
id: 1,
title: "Default",
source: window.location.href,
queue: [],
}];
}
abstract name(): string;
abstract fetchPagesSource(source: Chapter): AsyncGenerator<P>;
abstract parseImgNodes(page: P, chapterID?: number): Promise<ImageNode[]>;
abstract fetchOriginMeta(node: ImageNode, retry: boolean, chapterID?: number): Promise<OriginMeta>;
title(doc: Document): string {
const meta = this.galleryMeta(doc);
return meta.originTitle || meta.title || "unknown";
}
galleryMeta(doc: Document, _chapter?: Chapter): GalleryMeta {
return new GalleryMeta(window.location.href, doc.title || "unknown");
}
abstract workURL(): RegExp;
workURLs(): RegExp[] {
return [this.workURL()];
}
async processData(data: Uint8Array, contentType: string, _url: string): Promise<[Uint8Array, string]> {
return [data, contentType];
}
headers(): Record<string, string> {
return {};
}
}