This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
128 lines (116 loc) · 3.34 KB
/
util.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const baseUrl = "https://nhentai.net";
const madiaUrl = "https://i.nhentai.net";
let isProxyOn = false;
let proxy = "https://secret-ocean-49799.herokuapp.com/";
async function apiRequest(url, errorMessage) {
return fetch(url)
.then((response) => {
if (response.status == 404 || response.status == 403) return errorMessage;
return response.json();
})
.catch((err) => {
throw err;
});
}
async function htmlRequest(url, errorMessage) {
return fetch(url)
.then((response) => {
if (response.status == 404 || response.status == 403) return errorMessage;
return response.text();
})
.catch((err) => {
throw err;
});
}
function getPageLinks(doujin) {
return [...Array(doujin.num_pages)].map(
(_, i) => `${madiaUrl}/galleries/${doujin.media_id}/${i + 1}.jpg`
);
}
function getDoujinFromHTMLElement(doujin) {
const id = doujin
.querySelector(".cover")
.getAttribute("href")
.match(/(?<=\/g\/).+(?=\/)/);
const tags = doujin.getAttribute("data-tags");
const title = doujin.querySelector(".caption").innerHTML;
const thumb = doujin.querySelector(".cover > img");
const language = tags.includes("6346")
? "japanese"
: tags.includes("12227")
? "english"
: tags.includes("29963")
? "chinese"
: undefined;
return {
id: id[0],
title,
language,
thumbnail: {
s:
thumb.getAttribute("data-src") ||
thumb.getAttribute("src").replace(/^\/\//, "https://"),
w: thumb.getAttribute("width"),
h: thumb.getAttribute("height"),
},
};
}
function getUrl() {
return (isProxyOn ? proxy : "") + baseUrl;
}
class Request {
constructor(props) {
isProxyOn = props.isProxyOn ?? isProxyOn;
proxy = props.urlProxy ?? proxy;
}
async GetDoujin(id) {
const galleryUrl = `${getUrl()}/api/gallery/${id}`;
return apiRequest(galleryUrl, "Doujin not found").then((doujin) => {
const page_links = getPageLinks(doujin);
return { ...doujin, page_links };
});
}
async GetRelated(id) {
const relatedUrl = `${getUrl()}/api/gallery/${id}/related`;
return apiRequest(relatedUrl, "No related found").then((data) =>
data.result.map((doujin) => {
const page_links = getPageLinks(doujin);
return { ...doujin, page_links };
})
);
}
async GetRandom() {
const randomUrl = `${getUrl()}/random`;
return htmlRequest(randomUrl).then((html) => {
const page = document.createElement("html");
page.innerHTML = html;
return page.querySelector("#gallery_id").textContent.slice(1);
});
}
async GetPopularNow() {
const result = [];
htmlRequest(getUrl(), "test").then((html) => {
const page = document.createElement("html");
page.innerHTML = html;
page.querySelectorAll(".index-popular .gallery").forEach((doujin) => {
result.push(getDoujinFromHTMLElement(doujin));
});
});
return result;
}
async GetNew() {
const result = [];
htmlRequest(getUrl(), "test").then((html) => {
const page = document.createElement("html");
page.innerHTML = html;
page
.querySelectorAll(".index-container .gallery")
.forEach((doujin, index) => {
if (index < 5) return;
result.push(getDoujinFromHTMLElement(doujin));
});
});
return result;
}
}
export default Request;