-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
120 lines (98 loc) · 3.79 KB
/
script.js
File metadata and controls
120 lines (98 loc) · 3.79 KB
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
document.addEventListener("DOMContentLoaded", function () {
// Initialize CodeMirror with Syntax Highlighting
const editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: "python",
theme: "material",
lineNumbers: true,
indentUnit: 4,
smartIndent: true,
matchBrackets: true,
autoCloseBrackets: true,
lineWrapping: true,
});
// Default Python Code
editor.setValue("print('HAppy Coding')");
// Function to compile code with dynamic input handling
async function compileCode() {
const code = editor.getValue();
const outputElement = document.getElementById("output");
outputElement.textContent = "🔄 Running...";
// Check if input() is needed?
const inputPattern = /input\((?:'|"|`)(.*?)(?:'|"|`)\)/g;
const inputPrompts = [...code.matchAll(inputPattern)].map(
(match) => match[1]
);
if (inputPrompts.length > 0) {
// Clear previous content
outputElement.innerHTML = "<h3>Provide Input:</h3>";
// Create a container for inputs
const inputsContainer = document.createElement("div");
inputsContainer.classList.add("inputs-container");
inputPrompts.forEach((prompt, index) => {
const inputGroup = document.createElement("div");
inputGroup.classList.add("input-group");
const label = document.createElement("label");
label.setAttribute("for", `input-${index}`);
label.textContent = prompt;
const input = document.createElement("input");
input.type = "text";
input.id = `input-${index}`;
input.classList.add("user-input");
// Append label and input to the input group
inputGroup.appendChild(label);
inputGroup.appendChild(input);
// Append input group to the inputs container
inputsContainer.appendChild(inputGroup);
});
// Append inputs container to the output element
outputElement.appendChild(inputsContainer);
// Create and append the submit button
const submitButton = document.createElement("button");
submitButton.id = "submit-inputs-btn";
submitButton.textContent = "Submit Inputs";
outputElement.appendChild(submitButton);
// Attach event listener to the submit button
submitButton.addEventListener("click", submitInputs);
return;
}
await executeCode(code);
}
// Function to Execute Code
async function executeCode(code, inputs = []) {
const outputElement = document.getElementById("output");
outputElement.textContent = "🔄 Running...";
// Replace input() calls with provided inputs
inputs.forEach((input, index) => {
code = code.replace(/input\((?:'|"|`)(.*?)(?:'|"|`)\)/, `"${input}"`);
});
try {
const response = await fetch("https://emkc.org/api/v2/piston/execute", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
language: "python",
version: "3.10.0",
files: [{ content: code }],
}),
});
if (!response.ok) throw new Error("⚠ Error executing code!");
const result = await response.json();
const output = result.run.output || "⚠ No output!";
// Add the `>` before the output, mimicking terminal behavior
outputElement.innerHTML = `> ${output}`;
} catch (error) {
outputElement.textContent = "⚠ Error: " + error.message;
}
}
// Collect inputs and Rerun code
function submitInputs() {
const inputs = Array.from(document.querySelectorAll(".user-input")).map(
(input) => input.value
);
const code = editor.getValue();
executeCode(code, inputs);
}
// Add event listener to run code button
const runButton = document.querySelector(".run-btn");
runButton.addEventListener("click", compileCode);
});