-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl.ts
36 lines (34 loc) · 880 Bytes
/
url.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
export enum UrlType {
Gallery,
MPV,
Single,
}
export type ParsedUrl = {
type: UrlType;
gid: number;
token: string;
index: number | undefined;
};
export function parseUrl(url: string) {
const u = new URL(url, "https://e-hentai.org/");
if (u.hostname != "e-hentai.org" && u.hostname != "exhentai.org") {
return undefined;
}
let re = u.pathname.match(/s\/([^\/]+)\/(\d+)-(\d+)/);
if (re != null) {
return {
type: UrlType.Single,
gid: parseInt(re[2]),
token: re[1],
index: parseInt(re[3]),
} as ParsedUrl;
}
re = u.pathname.match(/(g|mpv)\/(\d+)\/([^\/]+)/);
if (re != null) {
return {
type: re[1] == "g" ? UrlType.Gallery : UrlType.MPV,
gid: parseInt(re[2]),
token: re[3],
} as ParsedUrl;
}
}