<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Eclipse Recorder</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: #333;
            color: #fff;
        }
        #eclipseText {
            font-size: 48px;
            text-align: center;
            animation: slideDown 2s forwards;
        }
        @keyframes slideDown {
            0% { transform: translateY(-100px); opacity: 0; }
            100% { transform: translateY(0); opacity: 1; }
        }
        .button-container {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }
        button {
            width: 250px;
            height: 65px;
            font-size: 22px;
            cursor: pointer;
            border: none;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #009999;
        }
        #startBtn { background-color: #20797e; color: white; }
        #stopBtn { background-color: #c33333; color: white; pointer-events: none; opacity: 0.5; }
        #downloadBtn { background-color: #20797e; color: white; display: none; }
        #preview { margin-top: 20px; }
    </style>
</head>
<body>
    <div id="eclipseText">Eclipse Recorder</div>
    <div class="button-container">
        <button id="startBtn">Start Recording</button>
        <button id="stopBtn" disabled>Stop Recording</button>
        <button id="downloadBtn">Download</button>
    </div>
    <video id="preview" width="640" height="360" controls autoplay></video>
    <script>
        let mediaRecorder;
        let recordedChunks = [];

        const startBtn = document.getElementById('startBtn');
        const stopBtn = document.getElementById('stopBtn');
        const downloadBtn = document.getElementById('downloadBtn');
        const preview = document.getElementById('preview');

        startBtn.addEventListener('click', async () => {
            try {
                const stream = await navigator.mediaDevices.getDisplayMedia({ video: true });
                preview.srcObject = stream;
                preview.play();
                mediaRecorder = new MediaRecorder(stream);

                mediaRecorder.ondataavailable = function(event) {
                    if (event.data.size > 0) {
                        recordedChunks.push(event.data);
                    }
                };

                mediaRecorder.start();
                console.log("Recording started");
                startBtn.disabled = true;
                stopBtn.disabled = false;
                stopBtn.style.pointerEvents = 'auto';
                stopBtn.style.opacity = '1';
            } catch (err) {
                console.error("Error starting screen capture: ", err);
            }
        });

        stopBtn.addEventListener('click', () => {
            mediaRecorder.stop();
            preview.srcObject.getTracks().forEach(track => track.stop());
            startBtn.disabled = false;
            stopBtn.disabled = true;
            stopBtn.style.pointerEvents = 'none';
            stopBtn.style.opacity = '0.5';
            downloadBtn.style.display = 'block';
            console.log("Recording stopped");
        });

        downloadBtn.addEventListener('click', () => {
            const blob = new Blob(recordedChunks, { type: 'video/mp4' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            const filename = prompt('File name') || 'eclipse.webm';
            a.href = url;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            recordedChunks = [];
            downloadBtn.style.display = 'none';
        });
    </script>
</body>
</html>
