-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
211 lines (185 loc) · 6.24 KB
/
index.html
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>JavaScript Playground</title>
<style>
:root {
--primary-color: #007bff;
--hover-color: #0056b3;
--success-color: #28a745;
--background-color: #f7f7f7;
--container-bg: #fff;
--border-color: #ddd;
--text-color: #333;
--input-bg: #f4f4f4;
--font-family: "Arial", sans-serif;
}
body {
font-family: var(--font-family);
background-color: var(--background-color);
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
height: 100vh;
box-sizing: border-box;
}
header {
text-align: center;
padding: 10px;
width: 100%;
}
h1 {
color: var(--text-color);
font-size: clamp(1.5rem, 5vw, 2.5rem);
margin: 0;
}
.container {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: stretch;
max-width: 900px;
margin: 0 auto;
padding: 15px;
background-color: var(--container-bg);
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 100%;
height: 100%; /* Ensures the container takes the full height */
box-sizing: border-box;
}
textarea {
width: 100%;
height: 100px; /* Make textarea smaller initially */
font-family: monospace;
font-size: 16px;
padding: 10px;
border: 1px solid var(--border-color);
border-radius: 4px;
background-color: var(--input-bg);
margin-bottom: 10px;
resize: vertical; /* Allow vertical resizing */
box-sizing: border-box;
flex-grow: 1; /* Make it take available space */
}
button {
width: 100%;
padding: 10px;
background-color: var(--primary-color);
color: white;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
box-sizing: border-box;
transition: background-color 0.3s ease;
}
button:hover {
background-color: var(--hover-color);
}
.output {
margin-top: 10px;
padding: 15px;
background-color: var(--container-bg);
border: 1px solid var(--border-color);
border-radius: 4px;
min-height: 80px; /* Increased minimum height for better visibility */
font-family: monospace;
font-size: 14px;
white-space: pre-wrap;
color: var(--text-color);
overflow-y: auto;
max-height: 200px; /* Increased max height for larger output */
box-sizing: border-box;
}
.copy-btn {
margin-top: 10px;
background-color: var(--success-color);
width: auto;
padding: 8px 16px; /* Reduced padding for better sizing */
font-size: 14px; /* Smaller font size */
cursor: pointer;
}
@media (max-width: 768px) {
.container {
padding: 10px;
}
textarea {
height: 120px; /* Slightly taller textarea for smaller devices */
}
h1 {
font-size: clamp(
1.5rem,
8vw,
2.5rem
); /* Larger header for small devices */
}
button {
padding: 12px; /* Slightly larger buttons on mobile */
}
.output {
max-height: 150px; /* Smaller output box for mobile */
}
.copy-btn {
font-size: 12px; /* Even smaller button on mobile */
padding: 6px 12px; /* Smaller button size */
}
}
</style>
</head>
<body>
<header>
<h1>JavaScript Playground</h1>
</header>
<main class="container">
<textarea id="jsCode" placeholder="Write your JavaScript code here..." aria-label="JavaScript code editor"></textarea>
<button id="runBtn" aria-label="Run JavaScript code">Run Code</button>
<button id="copyBtn" class="copy-btn" aria-label="Copy output">Copy Output</button>
<div id="output" class="output" role="region" aria-live="polite" aria-relevant="additions text"></div>
</main>
<script>
// Save the original console.log function
const originalConsoleLog = console.log;
// Override console.log to display logs in the output div
console.log = function (message) {
const outputDiv = document.getElementById('output');
outputDiv.textContent += message + '\n'; // Append the message to the output
originalConsoleLog.apply(console, arguments); // Optionally keep logging to the browser console
};
// Function to run the user's code
async function runCode() {
const code = document.getElementById('jsCode').value;
const outputDiv = document.getElementById('output');
outputDiv.textContent = ''; // Clear previous output
try {
// Using the Function constructor to safely execute the code
const func = new Function(code);
await func(); // Execute the code asynchronously
} catch (error) {
// Display error message if the code has issues
outputDiv.textContent = `Error: ${error.message}`;
}
}
// Function to copy output text to the clipboard
async function copyOutput() {
const outputDiv = document.getElementById('output');
const textToCopy = outputDiv.textContent;
try {
await navigator.clipboard.writeText(textToCopy);
alert('Output copied to clipboard!');
} catch (err) {
alert(`Failed to copy text: ${err.message}`);
}
}
// Event listeners for button interactions
document.getElementById('runBtn').addEventListener('click', runCode);
document.getElementById('copyBtn').addEventListener('click', copyOutput);
</script>
</body>
</html>