about summary refs log tree commit diff stats
path: root/html/playground/index.html
blob: d987c229b633312ced2a7506cee4dba6512fcb72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Playground</title>
    <style>
        /*
        @font-face {
            font-family: "APL386";
            src: url("./APL386.ttf") format("truetype");
        }
        */

        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #ddd;
            padding: 10px;
            height: 100vh;
            margin: 0;
        }

        textarea {
            width: 100%;
            height: 64%;
            font-family: "APL386", "APL385", "Go Mono", monospace;
            background-color: #FFFEEC;
            border: 2px solid #000;
            scrollbar-width: none;            
            font-size: 1rem;
            margin-bottom: 10px;
            padding: 10px;
            box-sizing: border-box;
            resize: none;
            border-bottom: 12px solid teal;
            -webkit-user-modify: read-write-plaintext-only;
        }

        textarea::-webkit-scrollbar {
            display: none;
        }

        textarea::selection {
            background-color: #EFECA7;
        }

        textarea:focus {
            outline: none;
        }

        #console {
            width: 100%;
            height: 22%;
            background-color: #000;
            color: #0fc;
            font-family: monospace;
            font-size: 1rem;
            overflow-y: auto;
            padding: 10px;
            box-sizing: border-box;
            white-space: pre-wrap;
        }

        .button-container {
            width: 100%;
            display: flex;
            justify-content: flex-end;
            margin-bottom: 10px;
        }

        button {
            padding: 10px 20px;
            font-size: 1rem;
            margin-left: 10px;
            cursor: pointer;
            border: none;
            transition: background-color 0.3s ease;
        }

        button:hover, button:focus {
            outline: none; 
        }

        button.run {
            background-color: teal;
            color: #FFFFFF;
        }

    </style>
</head>
<body>

    <div class="button-container">
        <button onclick="document.getElementById('codeInput').value='';window.location.hash='';">Clear</button>
        <button onclick="downloadCodeAndEditor()">Download</button>
        <button onclick="shareCode()">Share</button>
        <button onclick="evaluateCode()" class="run">Run Code</button>
    </div>
    <textarea id="codeInput" placeholder="Enter JavaScript..."></textarea>
    <div id="console"></div>

    <script>
        function evaluateCode() {
            const code = document.getElementById('codeInput').value;
            const consoleDiv = document.getElementById('console');
            consoleDiv.innerHTML = '';

            // Custom console.log function to output to the console div
            const originalConsoleLog = console.log;
            console.log = function(...args) {
                args.forEach(arg => {
                    const output = document.createElement('div');
                    output.textContent = typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg;
                    consoleDiv.appendChild(output);
                });
                originalConsoleLog.apply(console, args);
            };

            try {
                eval(code);
            } catch (error) {
                const errorOutput = document.createElement('div');
                errorOutput.textContent = error;
                errorOutput.style.color = 'red';
                consoleDiv.appendChild(errorOutput);
            }

            // Restore browser's console.log
            console.log = originalConsoleLog;
        }

        function downloadCodeAndEditor() {
            const codeInput = document.getElementById('codeInput').value;
            const htmlContent = document.documentElement.outerHTML.replace(
                /<textarea id="codeInput"[^>]*>.*<\/textarea>/,
                `<textarea id="codeInput">${codeInput}</textarea>`
            );

            const blob = new Blob([htmlContent], { type: 'text/html' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'code_editor.html';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }

        function shareCode() {
            const code = document.getElementById('codeInput').value;
            const encodedCode = btoa(encodeURIComponent(code));
            window.location.hash = encodedCode;
            window.prompt("Copy the URL to share.\nBe warned! Very long URLs don't share wicked well, sometimes.", window.location.href);
        }

        function loadCodeFromHash() {
            const hash = window.location.hash.substring(1);
            if (hash) {
                try {
                    const decodedCode = decodeURIComponent(atob(hash));
                    document.getElementById('codeInput').value = decodedCode;
                } catch (error) {
                    console.error('Failed to decode the URL hash:', error);
                }
            }
        }

        document.getElementById('codeInput').addEventListener('keydown', function(event) {
            if (event.metaKey && event.key === 'Enter') {
                event.preventDefault();
                evaluateCode();
            }
        });

        window.onload = loadCodeFromHash;
    </script>
    </script>

</body>
</html>