-
Notifications
You must be signed in to change notification settings - Fork 0
/
nekopost_infinite_scroll.ts
478 lines (459 loc) · 16.5 KB
/
nekopost_infinite_scroll.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
interface RootProjectInfo {
code: number;
desc: string;
projectInfo: ProjectInfo;
listCate: ListCate[];
listChapter: ListChapter[];
listProvider: ListProvider[];
listMedia?: unknown;
}
interface ListProvider {
userId: string;
displayName: string;
}
interface ListChapter {
chapterId: string;
chapterNo: string;
chapterName: string;
status: string;
publishDate: string;
createDate: string;
view: string;
ownerId: string;
providerName: string;
}
interface ListCate {
cateCode: string;
cateName: string;
cateLink: string;
}
interface ProjectInfo {
projectId: string;
projectName: string;
aliasName: string;
website: string;
author: string;
authorName: string;
artist: string;
artistName: string;
info: string;
status: string;
flgMature: string;
flgIntense: string;
flgViolent: string;
flgGlue: string;
flgReligion: string;
mainCategory: string;
goingType: string;
projectType: string;
readerGroup: string;
releaseDate: string;
updateDate: string;
views: string;
}
interface RootChapterInfo {
projectId: string;
chapterId: number;
chapterNo: string;
pageItem: PageItem[];
pageCount: number;
}
interface PageItem {
pageNo: number;
pageName?: string;
fileName?: string;
width: number;
height: number;
}
(() => {
let host = "";
let prototypeEl: HTMLElement | undefined | Node;
const splitUrl = window.location.pathname.split("/");
const projectId: string | number = splitUrl[splitUrl.length - 2];
let chapterNo: string | number = parseFloat(splitUrl[splitUrl.length - 1]);
const mangaPageSelector = ".w-full.border-0.border-white .mb-10";
function ready(fn: () => void): void {
if (document.readyState != "loading") {
fn();
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// get current date
// e.g. 2021-01-01 00:00L00 => 20211201000000
function currentDate() {
const now = new Date();
const year = now.getFullYear().toString();
const month = `0${(now.getMonth() + 1).toString()}`.slice(-2);
const day = `0${now.getDate()}`.slice(-2);
const hour = `0${now.getHours()}`.slice(-2);
const minute = `0${now.getMinutes()}`.slice(-2);
const second = `0${now.getSeconds()}`.slice(-2);
const millisecond = `00${now.getMilliseconds()}`.slice(-3);
return year + month + day + hour + minute + second + millisecond;
}
async function getProjectDetailFull(
projectId: string | number
): Promise<RootProjectInfo> {
const response = await fetch(
`https://uatapi.nekopost.net/frontAPI/getProjectInfo/${projectId}`
);
return response.json() as Promise<RootProjectInfo>;
}
async function getChapterDetails(
projectId: string | number,
chapterId: string | number,
date: string
): Promise<RootChapterInfo> {
const response = await fetch(
`${host}/collectManga/${projectId}/${chapterId}/${projectId}_${chapterId}.json?${date}`
);
return response.json() as Promise<RootChapterInfo>;
}
function clearPage() {
const els = document.querySelectorAll(
`${mangaPageSelector}:not(#mangaImages ${mangaPageSelector})`
);
els.forEach((el) => {
if (!document.querySelector("#mangaImages")?.contains(el)) {
el.remove();
}
});
}
function removeDisplayActiveClass(el: Element) {
el.classList.remove("display0", "display1", "display2", "display3");
}
function insertPage(
projectId: string | number,
chapterId: string | number,
pageName: string,
pageNo: string | number,
totalPages: string | number
) {
const cloneEl = prototypeEl?.cloneNode(true) as HTMLElement;
const appendEl = document.querySelector("#mangaImages");
const sizeEls = document.querySelectorAll(
".size-range li.svelte-fv9tk4"
);
let displayActive = 0;
for (let i = 0; i < sizeEls.length; i++) {
const el = sizeEls[i];
if (el.classList.contains("active")) {
displayActive = i;
}
}
if (cloneEl) {
const imgEl = cloneEl.querySelector("img");
const pageEl = cloneEl.querySelector<HTMLDivElement>(".text-2xl");
if (imgEl) {
imgEl.src = "";
imgEl.src = `${host}/collectManga/${projectId}/${chapterId}/${pageName}`;
imgEl.loading = "lazy";
removeDisplayActiveClass(imgEl);
imgEl.classList.add(`display${displayActive}`);
imgEl.style.minHeight = `250px`;
}
if (pageEl) {
pageEl.innerText = `Page No.${pageNo}/${totalPages}`;
}
if (appendEl) {
appendEl.appendChild(cloneEl);
}
}
}
function createElementPage() {
const parentEl = document.querySelector(mangaPageSelector)?.parentNode;
if (parentEl) {
const newItem = document.createElement("div");
newItem.id = "mangaImages";
parentEl.appendChild(newItem);
}
}
async function checkLoadState() {
if (document.querySelectorAll(mangaPageSelector).length > 0) {
return true;
}
await sleep(500);
console.log(1);
return false;
}
async function loadChapter(
chapterNo: string | number,
projectDetails: RootProjectInfo
) {
// get chapter id from chapter no
const chapterId = projectDetails.listChapter.find(
(chapter) => chapter.chapterNo === chapterNo
)?.chapterId;
if (chapterId) {
const projectId = projectDetails.projectInfo.projectId;
const chapterInfo = await getChapterDetails(
projectId,
chapterId,
currentDate()
);
chapterInfo.pageItem.sort((a, b) => a.pageNo - b.pageNo);
if (chapterInfo) {
const totalPages = chapterInfo.pageItem.length;
chapterInfo.pageItem.forEach((page, index) => {
insertPage(
projectId,
chapterId,
page.pageName || page.fileName || "",
index + 1,
totalPages
);
});
changeUrl(chapterNo);
}
}
}
function getNextChapterNo(
currentChapterNo: string | number,
projectDetails: RootProjectInfo
) {
const chapterList = projectDetails.listChapter;
// get index from chapter no
const currentIndex = chapterList.findIndex(
(chapter) => +chapter.chapterNo === +currentChapterNo
);
return currentIndex > 0 ? chapterList[currentIndex - 1].chapterNo : -1;
}
function onloadImages() {
function load() {
const lazyLoadImages =
document.querySelectorAll<HTMLImageElement>(
"#mangaImages img"
) || [];
const multipleLoad = 3;
// nodeList to array
const lazyLoadImagesArray = Array.from(lazyLoadImages);
// filter uncompleted image elements
const loadedImages = lazyLoadImagesArray.filter(
(image) => !image.complete
);
const loadedImagesArray = loadedImages.slice(0, multipleLoad);
loadedImagesArray.forEach((el) => {
el.loading = "eager";
});
}
function error(this: HTMLImageElement) {
setTimeout(() => {
const errorCount = +(this.dataset.errorCount || 0);
if (errorCount < 3) {
// push to error elements if not exist
const src = this.src;
this.src = src;
this.dataset.errorCount = (errorCount + 1).toString();
}
}, 1000);
load.call(this);
}
function addEvent(eventName: string, handler: EventListener) {
const rootEl = document.querySelector("#mangaImages");
rootEl?.addEventListener(
eventName,
function (this: HTMLElement, e) {
for (
let target = e.target as HTMLImageElement;
target && target != this;
target = target.parentNode as HTMLImageElement
) {
if (target.matches("img")) {
handler.call(target, e);
break;
}
}
},
true
);
}
addEvent("load", load);
addEvent("error", error);
}
function addScrollEvent(projectDetails: RootProjectInfo) {
const mangaImagesEl = document.querySelector("#mangaImages");
if (mangaImagesEl) {
let isLoading = false;
window.addEventListener("scroll", function eventScrollLoadPage() {
// if scroll near bottom of mangaImageEl
if (
mangaImagesEl.getBoundingClientRect().bottom -
window.innerHeight * 2 <=
0 &&
!isLoading
) {
isLoading = true;
chapterNo = getNextChapterNo(chapterNo, projectDetails);
if (chapterNo > 0) {
void (async () => {
await loadChapter(chapterNo, projectDetails);
isLoading = false;
})();
}
if (getNextChapterNo(chapterNo, projectDetails) === -1) {
document.querySelector("#loadAllChapterBtn")?.remove();
window.removeEventListener(
"scroll",
eventScrollLoadPage
);
}
}
});
}
}
function closeBtn() {
const btnEl: HTMLAnchorElement | null = document.querySelector(
".layout-helper.svelte-ixpqjn button"
);
const commentEl: HTMLAnchorElement | null = document.querySelector(
".layout-helper.svelte-ixpqjn a"
);
const cloneEl = btnEl?.cloneNode(true) as HTMLButtonElement;
if (cloneEl && commentEl) {
cloneEl.style.backgroundColor = "red";
cloneEl.innerText = "X";
cloneEl.addEventListener("click", () => {
window.close();
});
commentEl.remove();
// insert first child .layout-helper.svelte-ixpqjn
const parentEl = document.querySelector(
".layout-helper.svelte-ixpqjn"
);
if (parentEl) {
parentEl.insertBefore(cloneEl, parentEl.firstChild);
}
}
}
function getIncompleteImageEls() {
const imageEls =
document.querySelectorAll<HTMLImageElement>("#mangaImages img");
const imageElsArray = Array.from(imageEls);
return imageElsArray.filter((el) => !el.complete);
}
function loadAllChapterBtn(projectDetails: RootProjectInfo) {
const btnEl: HTMLAnchorElement | null = document.querySelector(
".layout-helper.svelte-ixpqjn button"
);
const cloneEl = btnEl?.cloneNode(true) as HTMLButtonElement;
if (cloneEl) {
cloneEl.style.backgroundColor = "green";
cloneEl.innerText = "All";
cloneEl.style.marginRight = "5px";
cloneEl.id = "loadAllChapterBtn";
cloneEl.addEventListener("click", loadAll);
if (getNextChapterNo(chapterNo, projectDetails) > -1) {
const parentEl = document.querySelector(
".layout-helper.svelte-ixpqjn"
);
parentEl?.insertBefore(cloneEl, parentEl.firstChild);
}
}
function loadAll() {
void (async () => {
while (chapterNo > 0) {
chapterNo = getNextChapterNo(chapterNo, projectDetails);
if (chapterNo > 0) {
await loadChapter(chapterNo, projectDetails);
} else {
break;
}
}
const imageEls = getIncompleteImageEls();
if (imageEls) {
imageEls[0].loading = "eager";
}
cloneEl.removeEventListener("click", loadAll);
cloneEl.remove();
})();
}
}
function changeUrl(chapter: string | number) {
const splitUrl = window.location.href.split("/");
const chapterStr = chapter.toString();
splitUrl[splitUrl.length - 1] = chapterStr;
const newUrl = splitUrl.join("/");
if (window.location.href !== newUrl) {
// change title to chapter
const titleEl = document.querySelector("title");
if (titleEl) {
// get text between "." and " "
const oldChapter = getTextBtw(titleEl.innerText, ".", " ");
// replace oldChapter to chapter
titleEl.innerText = titleEl.innerText.replace(
oldChapter,
chapterStr
);
}
window.history.replaceState({}, "", newUrl);
}
document.querySelectorAll("select").forEach((el) => {
el.value = chapterStr;
});
}
function addEventChangeSize() {
const els = document.querySelectorAll(".size-range li.svelte-fv9tk4");
for (let i = 0; i < els.length; i++) {
const el = els[i];
el.addEventListener("click", function () {
const size = window.innerHeight;
const images = document.querySelectorAll("#mangaImages img");
if (images[0]?.classList?.contains(`display${i}`)) return;
let imageBeforeChangePositionEl;
// get element near current position
for (const image of images) {
if (image.getBoundingClientRect().top + size > 0) {
imageBeforeChangePositionEl = image;
break;
}
}
images.forEach((imageEl) => {
removeDisplayActiveClass(imageEl);
imageEl.classList.add(`display${i}`);
});
// scroll to image near pervious position
if (imageBeforeChangePositionEl) {
window.scrollTo({
top:
imageBeforeChangePositionEl.getBoundingClientRect()
.top + window.scrollY,
behavior: "smooth",
});
}
});
}
}
function getTextBtw(str: string, str1: string, str2: string) {
const index1 = str.indexOf(str1);
const index2 = str.indexOf(str2);
return str.substring(index1 + str1.length, index2);
}
ready(() => {
void (async () => {
// addEventChangeSize();
const projectDetails = await getProjectDetailFull(projectId);
while (!(await checkLoadState()));
prototypeEl = document
.querySelector(mangaPageSelector)
?.cloneNode(true);
createElementPage();
// closeBtn();
// loadAllChapterBtn(projectDetails);
onloadImages();
host = new URL(
document.querySelector<HTMLImageElement>(
`${mangaPageSelector} img:not(#mangaImages ${mangaPageSelector})`
)?.src || "https://fs.nekopost.net/"
).origin;
await loadChapter(chapterNo.toString(), projectDetails);
clearPage();
if (getNextChapterNo(chapterNo, projectDetails) > -1) {
addScrollEvent(projectDetails);
}
})();
});
})();