-
Notifications
You must be signed in to change notification settings - Fork 0
/
nekopost-new-chapter.ts
159 lines (146 loc) · 4.51 KB
/
nekopost-new-chapter.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
(() => {
let isPageLoading = true;
const containerElSelector =
"div.grid.grid-cols-3.md\\:grid-cols-4.lg\\:grid-cols-5.xl\\:grid-cols-6.gap-0.first\\:mt-0.mb-4";
function ready(fn: () => void): void {
if (document.readyState != "loading") {
fn();
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
// get chapter from string
// example: Ch.5.5 - special chapter => 5.5
// example: Ch.6.5,6,7 - special chapter => 6
function getChapter(str: string): number {
let chapter = 0;
const regex = /Ch\.?\s?(\d+\.?\d*)/g;
const match = regex.exec(str);
if (match) {
chapter = parseFloat(match[1]);
}
return chapter;
}
function createStyle() {
const style = document.createElement("style");
style.innerHTML = `
.link-to-chapter>a:visited {
color: rgb(16 185 129);
}
`;
document.head.appendChild(style);
}
function loadMore() {
const loadMoreButton = document.querySelector<HTMLButtonElement>(
".w-full.rounded-md.border-b-2"
);
if (loadMoreButton) {
if (
loadMoreButton.getBoundingClientRect().top - 500 < window.innerHeight &&
loadMoreButton.getBoundingClientRect().height > 0 &&
!isPageLoading
) {
isPageLoading = true;
loadMoreButton.click();
}
}
}
function addTagA() {
const chapterEls = document.querySelectorAll<HTMLSpanElement>(
".cursor-pointer.text-gray-300.text-xs.xl\\:px-2.leading-5.text-ellipsis.overflow-hidden.h-4:not(.link-to-chapter)"
);
chapterEls.forEach((el) => {
const originalText = el.innerText;
const chapter = getChapter(el.innerText);
const projectUrl: string = el.closest("a")?.href || "";
const tagA = document.createElement("a");
tagA.href = `${projectUrl}/${chapter}`;
tagA.innerText = originalText;
tagA.onclick = () => {
window.location.href = tagA.href;
return false;
};
el.innerHTML = "";
el.appendChild(tagA);
el.classList.add("link-to-chapter");
});
return chapterEls;
}
function addEventWindowScroll() {
window.addEventListener("scroll", () => {
loadMore();
});
}
function changeMenu() {
const menus = document.querySelectorAll("#sidebar-menu a.waves-effect");
menus.forEach((el) => {
el.addEventListener("click", () => {
setTimeout(() => {
addEventWindowScroll();
}, 500);
});
});
}
function handlePageLoading() {
// Check if tag A is present and non-empty
const tagALength = addTagA().length;
// If tag A is found, page loading is considered completed
if (tagALength) {
isPageLoading = false;
}
// Load additional content
loadMore();
}
function updateHttpLinksToHttps() {
// Select all anchor elements with href starting with 'http://'
const httpLinks =
document.querySelectorAll<HTMLAnchorElement>('a[href^="http://"]');
// Loop through each element and replace 'http://' with 'https://' in the href attribute
httpLinks.forEach((anchorElement) => {
anchorElement.href = anchorElement.href.replace("http://", "https://");
});
}
function initializeContentObserver() {
const containerEl =
document.querySelector<HTMLDivElement>(containerElSelector);
if (containerEl) {
const observer = new MutationObserver(() => {
handlePageLoading();
updateHttpLinksToHttps();
});
observer.observe(containerEl, {
childList: true,
subtree: true,
});
handlePageLoading();
updateHttpLinksToHttps();
}
}
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Waits until a container element matching the specified selector is found in the document.
* Resolves when the container element is found.
*/
async function waitForContainerElement() {
// Continuously loop until the container element is found
for (;;) {
// Attempt to find the container element in the document
const containerElement = document.querySelector(containerElSelector);
// If the container element is found, exit the loop
if (containerElement) {
break;
}
// Wait for a short period before attempting again to avoid CPU usage
await sleep(100);
}
}
ready(async () => {
await waitForContainerElement();
createStyle();
initializeContentObserver();
addEventWindowScroll();
changeMenu();
});
})();