-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
48 lines (42 loc) · 1.72 KB
/
script.js
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
window.onload = function () {
// Initialize CodeMirror
const editor = CodeMirror(document.getElementById("codeMirrorEditor"), {
mode: "text/x-c++src",
lineNumbers: true,
theme: "midnight",
value: "// Start coding here...\n"
});
editor.setSize("100%", "100%");
// Run Code button functionality
document.getElementById("runCode").addEventListener("click", async function () {
const input = document.getElementById("inputArea").value;
const code = editor.getValue();
// Judge0 language IDs: 54 corresponds to C++
const requestBody = {
source_code: code,
language_id: 54, // Change this if you're using another language
stdin: input
};
try {
// Send request to the local proxy server
const response = await fetch("http://localhost:3000/api/submissions", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error("Failed to execute code. Check your server setup.");
}
const result = await response.json();
// Display output
const outputBox = document.getElementById("outputBox");
outputBox.textContent = result.stdout || result.stderr || "No output received.";
} catch (error) {
console.error("Error:", error);
const outputBox = document.getElementById("outputBox");
outputBox.textContent = `Error: ${error.message}`;
}
});
};