This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathsearch.js
147 lines (121 loc) · 4.19 KB
/
search.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Api urls
const ProxyApi = "https://proxy.techzbots1.workers.dev/?u=";
const searchapi = "/search/";
// Api Server Manager
const AvailableServers = ["https://api100.anime-dex.workers.dev"];
function getApiServer() {
return AvailableServers[Math.floor(Math.random() * AvailableServers.length)];
}
// Usefull functions
async function getJson(path, errCount = 0) {
const ApiServer = getApiServer();
let url = ApiServer + path;
if (errCount > 2) {
throw `Too many errors while fetching ${url}`;
}
if (errCount > 0) {
// Retry fetch using proxy
console.log("Retrying fetch using proxy");
url = ProxyApi + url;
}
try {
const _url_of_site = new URL(window.location.href);
const referer = _url_of_site.origin;
const response = await fetch(url, { headers: { referer: referer } });
return await response.json();
} catch (errors) {
console.error(errors);
return getJson(path, errCount + 1);
}
}
async function RefreshLazyLoader() {
const imageObserver = new IntersectionObserver((entries, imgObserver) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
}
});
});
const arr = document.querySelectorAll("img.lzy_img");
arr.forEach((v) => {
imageObserver.observe(v);
});
}
function sentenceCase(str) {
if (str === null || str === "") return false;
else str = str.toString();
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
let hasNextPage = true;
// Search function to get anime from gogo
async function SearchAnime(query, page = 1) {
const data = await getJson(searchapi + query + "?page=" + page);
const animes = data["results"];
const contentdiv = document.getElementById("latest2");
const loader = document.getElementById("load");
let html = "";
if (animes.length == 0) {
throw "No results found";
}
for (let i = 0; i < animes.length; i++) {
const anime = animes[i];
if (anime["title"].toLowerCase().includes("dub")) {
anime["subOrDub"] = "DUB";
} else {
anime["subOrDub"] = "SUB";
}
html += `<a href="./anime.html?anime_id=${anime["id"]
}"><div class="poster la-anime"> <div id="shadow1" class="shadow"> <div class="dubb">${anime[
"subOrDub"
].toUpperCase()}</div></div><div id="shadow2" class="shadow"> <img class="lzy_img" src="./static/loading1.gif" data-src="${anime["img"]
}"> </div><div class="la-details"> <h3>${sentenceCase(
anime["title"]
)}</h3> <div id="extra"> <span>${anime["releaseDate"]
}</span> </div></div></div></a>`;
}
contentdiv.innerHTML += html;
loader.style.display = "none";
contentdiv.style.display = "block";
return data["hasNextPage"];
}
const params = new URLSearchParams(window.location.search);
const query = params.get("query");
let page = 1;
if (query == null) {
window.location.replace("./index.html");
}
document.getElementById("latest").innerHTML = `Search Results: ${query}`;
// Load more results on scroll
window.addEventListener("scroll", () => {
if (
window.scrollY + window.innerHeight >=
document.documentElement.scrollHeight
) {
if (hasNextPage == true) {
SearchAnime(query, page).then((data) => {
hasNextPage = data;
page += 1;
RefreshLazyLoader();
console.log("Search animes loaded");
});
}
}
});
async function loadData() {
try {
const data = await SearchAnime(query, page);
hasNextPage = data;
page += 1;
RefreshLazyLoader();
console.log("Search animes loaded");
} catch (err) {
document.getElementById("main-section").style.display = "none";
document.getElementById("error-page").style.display = "block";
document.getElementById("error-desc").innerHTML = err;
console.error(err);
}
}
loadData();