<!DOCTYPE html>
<html>
<head>
    <title>Simple Webshell</title>
    <style>
        body {
            font-family: 'Consolas', monospace;
        }
        #commandInput {
            display: block;
            width: 100%;
            box-sizing: border-box;
            padding: 5px;
            font-size: 16px;
        }
        ul {
            padding: 0;
        }
        li {
            list-style: none;
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
    <ul id="commandList"></ul>
    <input type="text" id="commandInput" placeholder="Enter Command" onkeydown="handleEnter(event)">

    <script>
        function executeCommand() {
            const input = document.getElementById('commandInput').value.trim();
            const commandList = document.getElementById('commandList');

            if (input === 'clear') {
                commandList.innerHTML = ''; // Clear the list
                document.getElementById('commandInput').value = ''; // Clear the input field
                return;
            }

            const encodedCommand = encodeURIComponent(input);
            const commandItem = document.createElement('li');

            fetch(`/cmd?cmd=${encodedCommand}`)
                .then(response => response.text())
                .then(result => {
                    const resultItem = document.createElement('li');
                    resultItem.appendChild(document.createTextNode(`> ${input}`));
                    commandList.appendChild(resultItem);

                    const outputLines = result.split('\n');
                    outputLines.forEach(line => {
                        const lineItem = document.createElement('li');
                        lineItem.appendChild(document.createTextNode(line));
                        commandList.appendChild(lineItem);
                    });

                    // Scroll to the bottom of the page
                    window.scrollTo(0, document.body.scrollHeight);
                })
                .catch(error => {
                    const errorItem = document.createElement('li');
                    errorItem.appendChild(document.createTextNode(`Error: ${error}`));
                    commandList.appendChild(errorItem);
                });

            document.getElementById('commandInput').value = ''; // Clear the input field
            document.getElementById('commandInput').focus(); // Set focus back to the input field
        }

        function handleEnter(event) {
            if (event.key === "Enter") {
                executeCommand();
            }
        }
    </script>
</body>
</html>