async function searchResults(keyword) {
    const results = [];
    try {
        const response = await fetchv2(`https://donghuanosekai.com/wp-json/site/search/?keyword=${encodeURIComponent(keyword)}&type=undefined&nonce=4c4380bfaa`);
        const data = await response.json();

        for (const key in data) {
            if (data.hasOwnProperty(key)) {
                const item = data[key];
                results.push({
                    title: item.title,
                    image: item.img,
                    href: item.url
                });
            }
        }

        return JSON.stringify(results);
    } catch (err) {
        return JSON.stringify([{
            title: "Error",
            image: "Error",
            href: "Error"
        }]);
    }
}

async function extractDetails(url) {
    try {
        const response = await fetchv2(url);
        const html = await response.text();

        const regex = /<div class="context">\s*<p>([\s\S]*?)<\/p>/i;
        const match = html.match(regex);
        const description = match ? match[1].trim() : "N/A";

        return JSON.stringify([{
            description: description,
            aliases: "N/A",
            airdate: "N/A"
        }]);
    } catch (err) {
        return JSON.stringify([{
            description: "Error",
            aliases: "Error",
            airdate: "Error"
        }]);
    }
}

async function extractEpisodes(url) {
    const results = [];
    try {
        const response = await fetchv2(url);
        const html = await response.text();

        const regex = /<a href="([^"]+)"[^>]*>\s*.*?<span class="episode">Episódio\s+(\d+)<\/span>/g;
        let match;
        while ((match = regex.exec(html)) !== null) {
            results.push({
                href: match[1].trim(),
                number: parseInt(match[2], 10)
            });
        }

        return JSON.stringify(results.reverse());
    } catch (err) {
        return JSON.stringify([{
            href: "Error",
            number: "Error"
        }]);
    }
}
async function extractStreamUrl(url) {
    try {
        const response = await fetchv2(url);
        const html = await response.text();

        const divRegex = /<div class="slideItem"[^>]*data-video-url="([^"]+)"[^>]*>\s*Player 2\s*<\/div>/i;
        const divMatch = html.match(divRegex);
        if (!divMatch) return "https://error.org/";

        const playerUrl = divMatch[1].trim();

        const headers = {
            "Referer": url,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
        };
        const playerResponse = await fetchv2(playerUrl, headers);
        const playerHtml = await playerResponse.text();
        
        const iframeRegex = /<iframe[^>]+src="([^"]+)"/i;
        const iframeMatch = playerHtml.match(iframeRegex);
        
        if (!iframeMatch) return "https://error.org/";
        
        const iframeSrc = iframeMatch[1];
        
        const m3u8Regex = /v=(https:\/\/[^&"]+\.m3u8[^&"]*)/i;
        const m3u8Match = iframeSrc.match(m3u8Regex);
        
        return m3u8Match ? decodeURIComponent(m3u8Match[1]) : "https://error.org/";
    } catch {
        return "https://error.org/";
    }
}

