async function searchContent(keyword,page=0){ 
    const results = [];
    try {
	  const headers = {
	    "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
        "Origin": "https://mangacloud.org",
        "Referer": "https://mangacloud.org/",
        "Accept": "application/json"
	  };
	  const postData = `{"terms":"${keyword}"}`;

      const response = await fetch(`https://api.mangacloud.org/search`, {
	    method: "POST",
	    headers: headers,
	    body: postData
	  });
	  const data = await response.json();

      for (const item of data.data) {
            results.push({
                id: item.id,
                imageURL: `https://pika.mangacloud.org/${item.id}/${item.cover.id}.${item.cover.f}`,
                title: item.title
            });
        }

        return results;
    } catch (err) {
        return [];
    }
}

async function getContentData(ID) {
    try {
        const response = await fetch(`https://api.mangacloud.org/comic/${ID}`);
        const data = await response.json();
        
        return {
            description: data.data.description,
            tags: data.data.tags.map(tag => tag.name)
        };
    } catch (err) {
        return {
            description: "Error",
            tags: []
        };
    }
}

async function getChapters(ID) {
    const results = [];
    try {
        const response = await fetch(`https://api.mangacloud.org/comic/${ID}`);
        const data = await response.json();
        
        const chapters = data.data.chapters;
        for (const chap of chapters) {
            results.push([
                String(chap.number),
                [{
                    id: ID + "/" + chap.id,
                    title: chap.name || `Chapter ${chap.number}`,
                    chapter: chap.number,
                    scanlation_group: chap.name || ""
                }]
            ]);
        }

        return { en: results.reverse() };
    } catch (err) {
        return { en: [] };
    }
}

async function getChapterImages(url) {
    const chapterID = url.split("/").pop();
    const ID = url.split("/").slice(-2, -1)[0];
    const results = [];
    try {
        const response = await fetch(`https://api.mangacloud.org/chapter/${chapterID}`);
        const data = await response.json();
        
        for (const img of data.data.images) {
            results.push(`https://pika.mangacloud.org/${ID}/${chapterID}/${img.id}.${img.f}`);
        }

        return results;
    } catch (err) {
        return [];
    }
}