import proxy from "$lib/Utility/proxy"; interface Chapter { href: string; chapterNum: string; chapterDate: string; } export const getChapterCount = async ( nativeTitle: string, ): Promise => { const html = new DOMParser().parseFromString( await ( await fetch( proxy(`https://rawkuma.com/?s=${encodeURIComponent(nativeTitle)}`), ) ).text(), "text/html", ); const listContent = html.querySelector(".listupd"); if ( listContent && listContent.textContent && listContent.textContent.includes("Not Found") ) { return undefined; } const chapterCount = html.querySelector(".epxs"); if ( chapterCount && chapterCount.textContent && chapterCount.textContent.includes("Chapter") ) { return Number.parseInt( chapterCount.textContent.replace("Chapter", "").trim(), 10, ); } return undefined; }; export const getChaptersFromText = (text: string) => { const dom = new DOMParser() .parseFromString(text, "text/html") .querySelectorAll(".eph-num"); const chapters: Chapter[] = []; dom.forEach((chapter) => { const href = chapter.querySelector("a")?.getAttribute("href"); const chapterNum = chapter.querySelector(".chapternum")?.textContent; const chapterDate = chapter.querySelector(".chapterdate")?.textContent; if (href && chapterNum && chapterDate) chapters.push({ href, chapterNum, chapterDate, }); }); return chapters; };