const torrentRx = /(magnet:){1}|(^[A-F\d]{8,40}$){1}|(.*\.torrent){1}/i,
imageRx = /\.(jpeg|jpg|gif|png|webp)/
window.addEventListener("paste", async e => { //WAIT image lookup on paste, or add torrent on paste
let item = e.clipboardData.items[0];
if (item && item.type.indexOf("image") === 0) {
e.preventDefault();
let blob = item.getAsFile();
let reader = new FileReader();
reader.onload = e => {
traceAnime(e.target.result, "uri")
};
reader.readAsDataURL(blob);
} else if (item && item.type === "text/plain") {
item.getAsString(text => {
if (torrentRx.exec(text)) {
e.preventDefault();
search.value = ""
addTorrent(text);
} else if (imageRx.exec(text)) {
e.preventDefault();
search.value = ""
traceAnime(text)
}
})
} else if (item && item.type === "text/html") {
item.getAsString(text => {
let domparser = new DOMParser(),
doc = domparser.parseFromString(text, "text/html"),
img = doc.querySelectorAll("img")[0]
if (img) {
e.preventDefault();
search.value = ""
traceAnime(img.src)
}
})
}
})
function traceAnime(image, type) { //WAIT lookup logic
halfmoon.initStickyAlert({
content: `Looking Up Anime ${type == "uri" ? "" : `For ${image}`}`
})
let options,
url = `https://trace.moe/api/search?url=${image}`
if (type == "uri") {
options = {
method: "POST",
body: JSON.stringify({ image: image }),
headers: { "Content-Type": "application/json" },
},
url = "https://trace.moe/api/search"
}
fetch(url, options).then((res) => res.json())
.then(async (result) => {
if (result.docs[0].similarity >= 0.85) {
let res = await alRequest(result.docs[0].anilist_id, "SearchIDSingle")
viewAnime(res.data.Media)
} else {
console.log("no." + result.docs[0].similarity)
}
});
}
function searchBox() { // make searchbox behave nicely
search.placeholder = search.value
searchAnime(search.value)
search.value = ""
document.location.hash = "#browse"
}
//events
navNew.onclick = () => { releasesRss() }
navTrending.onclick = () => { searchAnime() }
navNowPlaying.onclick = () => { viewAnime(playerData.nowPlaying[0]) }
navList.onclick = async () => { //user watchlist
let browse = document.querySelector(".browse")
browse.textContent = '';
browse.appendChild(skeletonCard)
let res = await alRequest(alID, "UserLists"),
entries = res.data.MediaListCollection.lists[0].entries.concat(res.data.MediaListCollection.lists[1].entries),
frag = document.createDocumentFragment()
try {
entries.forEach(media => {
let template = cardCreator(media.media)
template.onclick = () => {
viewAnime(media.media)
}
frag.appendChild(template)
})
} catch (e) {
console.error(e)
}
browse.textContent = '';
browse.appendChild(frag)
}
//AL lookup logic
async function alRequest(searchName, method) {
let query,
variables = {
type: "ANIME",
sort: "TRENDING_DESC",
page: 1,
perPage: 50
},
options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
query: query,
variables: variables
})
},
queryObjects = `
id
title {
romaji
english
native
userPreferred
}
description(
asHtml: true
)
season
seasonYear
format
status
episodes
duration
averageScore
genres
coverImage {
extraLarge
medium
color
}
bannerImage
synonyms
nextAiringEpisode {
timeUntilAiring
episode
}
trailer {
id
site
}
streamingEpisodes {
title
thumbnail
}
relations {
edges {
relationType(version:2)
node {
id
title {
userPreferred
}
coverImage {
medium
}
type
status
}
}
}
`
if (localStorage.getItem("ALtoken")) {
options.headers['Authorization'] = localStorage.getItem("ALtoken")
}
if (method == "Trending") {
search.placeholder = "Search"
query = `
query ($page: Int, $perPage: Int, $sort: [MediaSort], $type: MediaType) {
Page (page: $page, perPage: $perPage) {
media(type: $type, sort: $sort) {
${queryObjects}
}
}
}`
} else if (method == "SearchReleasesSingle") {
variables.search = searchName
variables.perPage = 1
variables.status = "RELEASING"
query = `
query ($page: Int, $sort: [MediaSort], $search: String, $type: MediaType, $status: MediaStatus) {
Page (page: $page) {
media (type: $type, search: $search, sort: $sort, status: $status) {
${queryObjects}
}
}
}`
} else if (method == "SearchName") {
variables.search = searchName
variables.sort = "TRENDING_DESC"
query = `
query ($page: Int, $perPage: Int, $sort: [MediaSort], $type: MediaType, $search: String) {
Page (page: $page, perPage: $perPage) {
media(type: $type, search: $search, sort: $sort) {
${queryObjects}
}
}
}`
} else if (method == "SearchAnySingle") {
variables.search = searchName
variables.perPage = 1
variables.sort = "TRENDING_DESC"
query = `
query ($page: Int, $sort: [MediaSort], $search: String, $type: MediaType) {
Page (page: $page) {
media (type: $type, search: $search, sort: $sort) {
${queryObjects}
}
}
}`
} else if (method == "SearchIDSingle") {
variables.id = searchName
query = `
query ($id: Int, $type: MediaType) {
Media (id: $id, type: $type){
${queryObjects}
}
}`
} else if (method == "Viewer") {
query = `
query {
Viewer {
avatar {
medium
},
name,
id
}
}
`
} else if (method == "UserLists") {
variables.id = searchName
query = `
query ($id: Int, $type: MediaType){
MediaListCollection (userId: $id, type: $type, forceSingleCompletedList: true, status_in: [CURRENT,PLANNING]) {
lists {
entries {
media {
${queryObjects}
}
}
}
}
}
`
}
options.body = JSON.stringify({
query: query,
variables: variables
})
let res = await fetch('https://graphql.anilist.co', options).catch((error) => console.error(error)),
json = await res.json();
return json
}
function alEntry() {
if (playerData.nowPlaying && playerData.nowPlaying[0] && localStorage.getItem("ALtoken")) {
let query = `
mutation ($id: Int, $status: MediaListStatus, $episode: Int) {
SaveMediaListEntry (mediaId: $id, status: $status, progress: $episode) {
id
status
progress
}
}`,
variables = {
id: playerData.nowPlaying[0].id,
status: "CURRENT",
episode: parseInt(playerData.nowPlaying[1])
},
options = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + localStorage.getItem("ALtoken"),
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
query: query,
variables: variables
})
}
fetch("https://graphql.anilist.co", options)
}
}
let alResponse
async function searchAnime(a) { //search bar functionality
let frag = document.createDocumentFragment(),
browse = document.querySelector(".browse")
browse.textContent = '';
browse.appendChild(skeletonCard)
a ? alResponse = await alRequest(a, "SearchName") : alResponse = await alRequest(a, "Trending")
try {
alResponse.data.Page.media.forEach(media => {
let template = cardCreator(media)
template.onclick = () => {
viewAnime(media)
}
frag.appendChild(template)
})
} catch (e) {
console.error(e)
}
browse.textContent = '';
browse.appendChild(frag)
}
//these really shouldnt be global
let detailsfrag = document.createDocumentFragment()
let details = {
averageScore: "Average Score",
// duration: "Episode Duration",
// episodes: "Episodes",
// format: "Format",
genres: "Genres",
// season: "Season",
// seasonYear: "Year",
status: "Status",
english: "English",
romaji: "Romaji",
native: "Native",
synonyms: "Synonyms"
}
const episodeRx = /Episode (\d+) - (.*)/;
// this is fucked beyond belief, this is why you use frameworks
function viewAnime(media) {
halfmoon.showModal("view")
view.setAttribute("style", `background-image: url(${media.bannerImage}) !important`)
viewImg.src = media.coverImage.extraLarge
viewTitle.textContent = media.title.userPreferred
viewDesc.innerHTML = media.description || ""
viewDetails.innerHTML = ""
detailsCreator(media)
viewDetails.appendChild(detailsfrag)
if (media.nextAiringEpisode) {
let temp = document.createElement("p")
temp.innerHTML = `Airing
Episode ${media.nextAiringEpisode.episode}: ${toTS(media.nextAiringEpisode.timeUntilAiring)}`
viewDetails.prepend(temp)
}
viewSeason.innerHTML = `${(media.season ? media.season.toLowerCase() + " " : "") + (media.seasonYear ? media.seasonYear : "")}`
viewMediaInfo.innerHTML = `${media.format ? "" + media.format + "" : ""}${media.episodes ? "" + media.episodes + " Episodes" : ""}${media.duration ? "" + media.duration + " Minutes" : ""}`
viewPlay.onclick = () => { nyaaSearch(media, 1); halfmoon.toggleModal("view") }
if (media.trailer) {
viewTrailer.removeAttribute("disabled", "")
viewTrailer.onclick = () =>
trailerPopup(media.trailer)
} else {
viewTrailer.setAttribute("disabled", "")
}
if (media.status == "NOT_YET_RELEASED") {
viewPlay.setAttribute("disabled", "")
} else {
viewPlay.removeAttribute("disabled", "")
}
if (media.relations.edges.length) {
viewRelationsGallery.classList.remove("d-none")
viewRelationsGallery.innerHTML = ""
let frag = document.createDocumentFragment()
media.relations.edges.forEach(edge => {
let template = document.createElement("div")
template.classList.add("card", "m-0", "p-0")
template.innerHTML = `
${edge.node.title.userPreferred}
${edge.relationType.toLowerCase()}
${(media.format ? (media.format == "TV" ? "" + media.format + " Show" : "" + media.format.toLowerCase().replace(/_/g, " ")) : "") + ""} ${media.episodes ? "" + media.episodes + " Episodes" : media.duration ? "" + media.duration + " Minutes" : ""} ${media.status ? "" + media.status.toLowerCase().replace(/_/g, " ") + "" : ""} ${media.season || media.seasonYear ? "" + (!!media.season ? media.season.toLowerCase() + " " : "") + (media.seasonYear || "") + "" : ""}