-
-
Save nguyenit67/e28c1599d55f126cdaa208c825f4125d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function searchManga(title) { | |
const result = await (await fetch(`https://api.mangadex.org/manga?title=${encodeURI(title)}`)).json(); | |
return result.results.map(v => v.data) | |
} | |
async function getChapters(mangaID) { | |
const result = await (await fetch(`https://api.mangadex.org/chapter/?manga=${mangaID}&limit=50`)).json(); | |
return result.results; | |
} | |
async function getChapterInfo(chapterID) { | |
const result = await (await fetch(`https://api.mangadex.org/chapter/${chapterID}`)).json(); | |
return result; | |
} | |
async function atHomeLink(chapterID) { | |
const result = await (await fetch(`https://api.mangadex.org/at-home/server/${chapterID}`)).json(); | |
return result.baseUrl; | |
} | |
function craftImageLink(homeLink, chapter, isDataSaver=true) { | |
// {server-specific base url}/{temporary access token}/{quality mode}/{chapter hash}/{filename} | |
const mode = isDataSaver? "data-saver" : "data"; | |
const chapterHash = chapter.data.attributes.hash; | |
const images = chapter.data.attributes[isDataSaver? "dataSaver" : "data"] | |
.map(img => `${homeLink}/${isDataSaver}/${chapterHash}/${img}`); | |
return images; | |
} | |
function listChapters(chapters) { | |
return chapters.map(chapter => chapter.data.attributes).map(v => `v${v.volume}c${v.chapter}: ${v.title}`) | |
} | |
let MANGA_NAME = "kubera"; | |
let CHAPTER_INDEX = 0; | |
// get first search result | |
let manga = (await searchManga(MANGA_NAME))[0]; | |
// get chapter IDs | |
let mangaChapters = await getChapters(manga.id); | |
// get home link with first chapter ID | |
let homeLink = await atHomeLink(mangaChapters[CHAPTER_INDEX].data.id); | |
// get image links | |
console.log(mangaChapters[CHAPTER_INDEX]) | |
let firstChapter = (await getChapterInfo(mangaChapters[CHAPTER_INDEX].data.id)); | |
let imageLinks = craftImageLink(homeLink, firstChapter, true); | |
//console.log(listChapters(mangaChapters)); | |
console.log(listChapters(mangaChapters)[CHAPTER_INDEX]); | |
console.log(imageLinks.join("\n")); | |
imageLinks.forEach(s => { | |
let m = document.createElement("img"); | |
m.src = s; | |
document.body.append(m); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment