<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Answer Fetcher</title>
    <link rel="icon" href="https://cdn.discordapp.com/attachments/1274715030390308876/1330038565211476060/QUIZIZZ.png?ex=67946f70&is=67931df0&hm=ba4da7ce1d517474728176752eb260fad5d65660c32912cf03d6fde8f1803586&" type="image/png">

    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-image: url('https://cdn.discordapp.com/attachments/1274715030390308876/1332319889112698941/gif.gif?ex=6794d316&is=67938196&hm=4914a6be073489570bd18b10fc66396d75b79f7aa293502985d3381799315e1e&');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: fixed;
            height: 100%;
            min-height: 100vh;
            color: white;
        }
        html, body {
            height: 100%;
        }
        h1 {
            text-align: center;
            color: #fff;
            margin-bottom: 20px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7);
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, 0.8);
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
            color: #333;
            margin-bottom: 30px; /* เพิ่มระยะห่างระหว่างฟอร์ม */
        }
        label {
            font-size: 16px;
            margin-bottom: 10px;
            display: block;
        }
        input[type="text"] {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
            margin-bottom: 20px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            font-size: 16px;
            padding: 12px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
            margin-bottom: 20px;
        }
        button:hover {
            background-color: #45a049;
        }
        .question {
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f9f9f9;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        .question h3 {
            margin: 0;
            color: #333;
        }
        .answers {
            margin-top: 10px;
            color: #333;
        }
        .answers p {
            font-size: 16px;
            margin: 0;
        }
        .error {
            color: red;
            font-weight: bold;
            text-align: center;
        }

        /* Message Notification Style */
        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            background-color: #ff0000;
            color: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            display: none;
            align-items: center;
            font-size: 16px;
            z-index: 999;
            min-width: 200px;
        }
        .notification .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #fff;
            border-radius: 50%;
            width: 20px;
            height: 20px;
            margin-right: 10px;
            animation: spin 2s linear infinite;
        }

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

    </style>
</head>
<body>

    <h1>Answer Fetcher</h1>

    <div class="container">
        <h2>Quizizz Answer</h2>
        <label for="quizLink">Enter Quizizz Link</label>
        <img src="https://schoolcheats.net/kahoot.png" style="width: 100%; max-width: 900px; display: block; margin: 0 auto; margin-bottom: 10px; border-radius: 12px;">
        <input type="text" id="quizLink" placeholder="Quizizz Link Here">
        <button onclick="fetchQuizData()">Show Answers</button>
        <div id="quizOutput"></div>
    </div>

    <div class="container">
        <h2>Kahoot Answer</h2>
        <img src="https://media.discordapp.net/attachments/1274715030390308876/1332358380173394022/image.png?ex=6794f6ef&is=6793a56f&hm=476bc02b3e5d030db713eadcf697e98a3e3a9bb226207c1d020fe6a688838291&=&format=webp&quality=lossless&width=1121&height=57" style="width: 100%; max-width: 900px; display: block; margin: 0 auto; margin-bottom: 10px; border-radius: 12px;">
        <label for="linkkahoot">Enter Kahoot Link</label>
        <input type="text" id="linkkahoot" placeholder="Enter Id Here">
        <button onclick="fetchKahootAnswers()">Show Answers</button>
        <div id="kahootOutput"></div>
    </div>

    <!-- Notification -->
    <div id="notification" class="notification">
        <div class="spinner"></div>
        <span id="notification-message">Link is incorrect</span>
    </div>

    <script>
        async function fetchQuizData() {
            const quizLink = document.getElementById("quizLink").value;
            const notification = document.getElementById("notification");
            const notificationMessage = document.getElementById("notification-message");

            if (!quizLink) {
                notificationMessage.textContent = "Please enter a Quizizz link!";
                notification.style.display = 'flex';
                setTimeout(() => notification.style.display = 'none', 3000);
                return;
            }

            try {
                const response = await fetch('https://v3.schoolcheats.net/quizizz/answers', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ input: quizLink })
                });

                const data = await response.json();
                const questions = data.questions;
                let output = '';

                if (questions && questions.length > 0) {
                    questions.forEach((question, idx) => {
                        const queryText = cleanText(question.structure.query.text);
                        const options = question.structure.options.map(opt => cleanText(opt.text));
                        const correctIndex = question.structure.answer;
                        const correctAnswer = correctIndex !== null && correctIndex < options.length ? options[correctIndex] : "No correct answer";

                        output += `
                            <div class="question">
                                <h3>${idx + 1}: ${queryText}</h3>
                                <div class="answers">
                                    <p><strong>Answer:</strong> ${correctAnswer}</p>
                                </div>
                            </div>
                        `;
                    });
                } else {
                    output = "<p class='error'>No questions found</p>";
                }

                document.getElementById("quizOutput").innerHTML = output;
            } catch (error) {
                document.getElementById("quizOutput").innerHTML = `<p class='error'>Error: ${error.message}</p>`;
            }
        }

        async function fetchKahootAnswers() {
            const linkkahoot = document.getElementById("linkkahoot").value;
            const resultsDiv = document.getElementById("kahootOutput");

            if (!linkkahoot) {
                resultsDiv.innerHTML = "<p class='error'>Please enter a Kahoot link!</p>";
                return;
            }

            try {
                const response = await fetch("https://v3.schoolcheats.net/kahoot/answers", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({ input: linkkahoot })
                });

                const data = await response.json();
                if (data.kahoot && data.kahoot.questions) {
                    const questions = data.kahoot.questions;
                    let output = '';
                    questions.forEach((question, index) => {
                        const questionText = question.question || "No question text found";
                        const correctAnswers = question.choices.filter(choice => choice.correct).map(choice => choice.answer);

                        output += `
                            <div class="question">
                                <h3>${index + 1}: ${questionText}</h3>
                                <div class="answers">
                                    <p><strong>Answers:</strong> ${correctAnswers.join(", ")}</p>
                                </div>
                            </div>
                        `;
                    });
                    resultsDiv.innerHTML = output;
                } else {
                    resultsDiv.innerHTML = "<p class='error'>No questions found</p>";
                }
            } catch (error) {
                resultsDiv.innerHTML = `<p class='error'>An error occurred: ${error.message}</p>`;
            }
        }

        function cleanText(text) {
            return text.replace(/<\/?[^>]+(>|$)/g, ""); // Remove HTML tags
        }
    </script>

</body>
</html>
