<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>9Streams.xyz - Unlimited Free Manga Reader and more Online for Free!</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

        <script type='text/javascript' src='//spokentomatoestraumatic.com/29/44/32/294432dcc07bb2f46fa506e1e78d45e4.js'></script>
    <style>
body {
    position: relative;
    color: #e0e0e0;
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow-x: hidden;
    background-color: rgba(22, 22, 22);
}

.container {
    width: 100%;
    margin: 20px 0;
    text-align: center;
    padding: 0;
}

.top-buttons {
    width: 100%;
    display: flex;
    justify-content: flex-start;
    position: fixed;
    top: 20px;
    left: 20px;
    z-index: 1000;
    transition: top 0.3s ease;
    gap: 15px;
}

::-webkit-scrollbar {
    width: 10px; /* Adjust the width */
}

/* Customize the thumb */
::-webkit-scrollbar-thumb {
    background: #525252; /* Change the thumb color */
    border-radius: 10px; /* Add rounded corners */
}

/* Customize the track */
::-webkit-scrollbar-track {
    background: #0d0d0d; /* Change the track color */
}

.top-buttons button {
    background-color: #333333;
    color: #ffffff;
    border: none;
    border-radius: 5px;
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: background-color 0.3s, transform 0.3s;
}

.top-buttons button:hover {
    background-color: #555555;
    transform: scale(1.05);
}

.top-buttons button i {
    margin-right: 10px;
}

.chapter-title {
    font-size: 24px;
    margin: 50px 0 10px 0;
}

.pages-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    margin-top: 20px;
}

.pages-container img {
    width: 100%;
    height: auto;
    margin: 0;
}

@media (min-width: 768px) {
    .pages-container img {
        width: 90%;
        max-width: 700px; /* Reduced max-width for smaller screens */
    }
}

@media (min-width: 1024px) {
    .pages-container img {
        width: 80%;
        max-width: 800px; /* Adjusted max-width for larger screens */
    }
}

@media (min-width: 1200px) {
    .pages-container img {
        width: 70%;
        max-width: 900px; /* Further reduced max-width for desktop */
    }
}

.navigation-buttons {
    display: flex;
    justify-content: center;
    width: 100%;
    margin: 20px 0;
}

.navigation-buttons button {
    background-color: #6a0dad;
    color: #ffffff;
    border: none;
    border-radius: 5px;
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    margin: 0 10px;
    transition: background-color 0.3s, transform 0.3s;
}

.navigation-buttons button:hover {
    background-color: #5a0b9a;
    transform: scale(1.05);
}

.navigation-buttons #prev-button {
    background-color: #6a6a6a;
}

.navigation-buttons #prev-button:hover {
    background-color: #5a5a5a;
}

.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #000000;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 999;
}

.loading-spinner {
    width: 60px;
    height: 60px;
    border: 6px solid #e0e0e0;
    border-top: 6px solid #333333;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.not-found {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    color: #e0e0e0;
    font-size: 24px;
    z-index: 1000;
    text-align: center;
    padding: 20px;
    box-sizing: border-box;
}

.congrats-box {
    background-color: #333333;
    color: #ffffff;
    padding: 20px;
    border-radius: 5px;
    text-align: center;
    margin-top: 20px;
    display: none;
}

.congrats-box iframe {
    width: 100%;
    max-width: 600px;
    height: 200px;
    border: none;
    margin-top: 10px;
}

/* Background Image Styles */
body::before {
    content: "";
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('https://small.fileditchstuff.me/s18/TLOVjFhnxAUgtxHjMpli.jpg');
    background-size: cover;
    background-position: center;
    opacity: 0.1; /* Adjust transparency here */
    z-index: -1; /* Keep it behind everything */
}
    </style>
</head>
<body>

    <div class="loading-overlay" id="loading-overlay">
        <div class="loading-spinner"></div>
    </div>
<div class="top-buttons" id="top-buttons">
    <button id="back-button"><i class="fa-solid fa-arrow-left"></i> Back to Info Page</button>
    <button id="fullscreen-button"><i class="fa-solid fa-expand"></i> Full Screen</button>
</div>
    <div class="container">
        <h1 class="chapter-title" id="chapter-title">Chapter Title</h1>
        <div class="pages-container" id="pages-container"></div>
        <div class="navigation-buttons">
            <button id="prev-button"><i class="fa-solid fa-arrow-left"></i> Previous Ch. </button>
            <button id="next-button">Next Ch. <i class="fa-solid fa-arrow-right"></i></button>
        </div>
        <div class="congrats-box" id="congrats-box">
            <p>Congrats! You finished this Manga, wanna rate it?</p>
            <iframe id="rate-iframe" src="" title="Rate this Manga"></iframe>
        </div>
    </div>
    <div class="not-found" id="not-found" style="display: none;">
        <p>Manga or Chapter Not Found</p>
    </div>

<script>
document.addEventListener("DOMContentLoaded", () => {
    const overlay = document.getElementById('loading-overlay');
    const notFound = document.getElementById('not-found');
    const chapterTitle = document.getElementById('chapter-title');
    const pagesContainer = document.getElementById('pages-container');
    const prevButton = document.getElementById('prev-button');
    const nextButton = document.getElementById('next-button');
    const backButton = document.getElementById('back-button');
    const congratsBox = document.getElementById('congrats-box');
    const rateIframe = document.getElementById('rate-iframe');
    const topButtons = document.getElementById('top-buttons');
    let lastScrollTop = 0;

    // Function to determine if the device is less powerful
    function isLowPerformanceDevice() {
        const start = performance.now();
        for (let i = 0; i < 1000000; i++) {
            // Simple benchmark task
        }
        const end = performance.now();
        return (end - start) > 200; // Adjust threshold based on testing
    }

    const lazyLoad = isLowPerformanceDevice(); // Determine if lazy loading should be enabled

    function handleScroll() {
        let st = window.pageYOffset || document.documentElement.scrollTop;
        if (st > lastScrollTop) {
            topButtons.style.top = '-100px'; // hide top buttons
        } else {
            topButtons.style.top = '20px'; // show top buttons
        }
        lastScrollTop = st <= 0 ? 0 : st;
    }

    window.addEventListener('scroll', handleScroll, false);

    async function fetchMangaInfo(mangaId) {
        try {
            const response = await fetch(`https://goodproxy.goodproxy.workers.dev/fetch?url=https://manga-scraper-blue.vercel.app/manganato/info/${mangaId}`);
            const data = await response.json();
            if (data.status === 200) {
                return data.results;
            } else {
                showNotFoundPage();
            }
        } catch (error) {
            console.error('Error fetching manga info:', error);
            showNotFoundPage();
        }
    }

    async function fetchChapterPages(mangaId, chapterNumber) {
        try {
            const response = await fetch(`https://goodproxy.goodproxy.workers.dev/fetch?url=https://manga-scraper-blue.vercel.app/manganato/pages/${mangaId}/${chapterNumber}`);
            const data = await response.json();
            if (data.status === 200) {
                return data.results;
            } else {
                showNotFoundPage();
            }
        } catch (error) {
            console.error('Error fetching chapter pages:', error);
            showNotFoundPage();
        }
    }

    function showNotFoundPage() {
        overlay.style.display = 'none';
        notFound.style.display = 'flex';
    }

    function displayChapter(chapter, chapterPages) {
        chapterTitle.textContent = chapter.title;
        pagesContainer.innerHTML = '';

        chapterPages.forEach(pageUrl => {
            const img = document.createElement('img');
            img.src = lazyLoad ? '' : `https://goodproxy.goodproxy.workers.dev/fetch?url=${encodeURIComponent(pageUrl)}&ref=https://manganato.com`;
            img.dataset.src = `https://goodproxy.goodproxy.workers.dev/fetch?url=${encodeURIComponent(pageUrl)}&ref=https://manganato.com`;
            img.classList.add('lazy');
            pagesContainer.appendChild(img);
        });

        // Add a 25% probability to show the image page
        const showImagePage = Math.random() < 0.25;
        if (showImagePage) {
            const imgPage = document.createElement('img');
            imgPage.src = 'https://small.fileditchstuff.me/s19/KAfrooVzIOcEzdNVYLGI.png';
            imgPage.classList.add('image-page'); 
            pagesContainer.appendChild(imgPage);
        }

        if (lazyLoad) {
            // Add Intersection Observer to handle lazy loading
            const observer = new IntersectionObserver((entries) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        const img = entry.target;
                        img.src = img.dataset.src;
                        img.classList.remove('lazy');
                        observer.unobserve(img);
                    }
                });
            });

            document.querySelectorAll('.lazy').forEach(img => observer.observe(img));
        }
    }

    function getAdjacentChapters(chapters, currentChapterId) {
        let prevChapter = null;
        let nextChapter = null;
        for (let i = 0; i < chapters.length; i++) {
            if (chapters[i].id === currentChapterId) {
                if (i > 0) prevChapter = chapters[i - 1];
                if (i < chapters.length - 1) nextChapter = chapters[i + 1];
                break;
            }
        }
        return { prevChapter, nextChapter };
    }

    async function loadChapter(mangaId, chapterNumber) {
        overlay.style.display = 'flex';
        const mangaInfo = await fetchMangaInfo(mangaId);
        if (!mangaInfo) return;

        const chapters = mangaInfo.chapters;
        const chapterPages = await fetchChapterPages(mangaId, chapterNumber);
        if (!chapterPages) return;

        const currentChapter = chapters.find(chapter => chapter.id.split('/').pop() === chapterNumber);
        displayChapter(currentChapter, chapterPages);

        const { prevChapter, nextChapter } = getAdjacentChapters(chapters, currentChapter.id);

        prevButton.style.display = prevChapter ? 'block' : 'none';
        nextButton.style.display = nextChapter ? 'block' : 'none';

        prevButton.onclick = () => {
            if (prevChapter) {
                window.location.href = `https://9streams.xyz/manga/read?id=${mangaId}&chapter=${prevChapter.id.split('/').pop()}`;
            }
        };

        nextButton.onclick = () => {
            if (nextChapter) {
                window.location.href = `https://9streams.xyz/manga/read?id=${mangaId}&chapter=${nextChapter.id.split('/').pop()}`;
            } else {
                const showRatingBox = Math.random() < 0.3;
                if (showRatingBox) {
                    congratsBox.style.display = 'block';
                    congratsBox.querySelector('p').textContent = "Enjoying this Manga so far? Or not? Here you can rate it!";
                    rateIframe.src = `https://9streams.xyz/watch_page.php#${mangaId}`;
                }
            }
        };

        backButton.onclick = () => {
            window.location.href = `https://9streams.xyz/manga/info?manga=${mangaId}`;
        };

        overlay.style.display = 'none';
    }

    const urlParams = new URLSearchParams(window.location.search);
    const mangaId = urlParams.get('id');
    const chapterNumber = urlParams.get('chapter');

    if (mangaId && chapterNumber) {
        loadChapter(mangaId, chapterNumber);
    } else {
        document.body.innerHTML = '<h1 style="color: #e0e0e0; text-align: center; width: 100%; margin: auto; padding: 20px;">Invalid manga ID or chapter number in URL</h1>';
        overlay.style.display = 'none';
    }
});

document.addEventListener("DOMContentLoaded", () => {
    const fullscreenButton = document.getElementById('fullscreen-button');

    function updateFullscreenButton() {
        if (document.fullscreenElement) {
            fullscreenButton.innerHTML = '<i class="fa-solid fa-compress"></i> Exit';
        } else {
            fullscreenButton.innerHTML = '<i class="fa-solid fa-expand"></i> Full-Screen';
        }
    }

    function toggleFullscreen() {
        if (!document.fullscreenElement) {
            document.documentElement.requestFullscreen().catch((err) => {
                console.error(`Error attempting to enter fullscreen mode: ${err.message} (${err.name})`);
            });
        } else {
            document.exitFullscreen().catch((err) => {
                console.error(`Error attempting to exit fullscreen mode: ${err.message} (${err.name})`);
            });
        }
    }

    fullscreenButton.addEventListener('click', toggleFullscreen);
    document.addEventListener('fullscreenchange', updateFullscreenButton);

    // Initialize button state
    updateFullscreenButton();
});
</script>
</body>
</html>
