-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.html
162 lines (136 loc) · 5.9 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
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Online Compiler</title>
</head>
<body style="background-color: #eee; font-family: monospace; margin: 8px;" >
<h2>🔥 Online Compiler<sub><small> Powered by <a href="https://bit.ly/2UCsWRM">Judge0</a></small></sub></h2>
<h4>Source Code</h4>
<textarea id="source" style="width: calc(50% - 8px); height: 40%; resize: vertical;">
#include <iostream>
#include <string>
#pragma message "Hello from compilation!"
int main()
{
std::string name;
std::cin >> name;
std::cout << "Hi " << name << "!\n";
std::cerr << "This is a dummy error message " << name << ".\n";
std::cout << "But don't worry " << name << ", everything is fine! :)\n";
return 0;
}
</textarea>
<h4>Language</h4>
<select id="lang">
<option>Bash</option>
<option>C#</option>
<option selected>C++</option>
<option>C</option>
<option>Java</option>
<option>Python</option>
<option>Ruby</option>
</select>
<h4>Input</h4>
<textarea id="input" style="width: calc(50% - 8px); height: 10%; resize: vertical;">John</textarea>
<h4>Expected Output<sub><small>(optional)</small></sub></h4>
<textarea id="expected-output" style="width: calc(50% - 8px); height: 10%; resize: vertical;"></textarea>
</br></br>
<button id="run" onclick="run()">▶️ Run (Ctrl + Enter)</button>
<small style="width: 50%; text-align: center; color: #ccc;">Source code available on <a style="color: #bbb; font-weight: bold;" href="https://github.com/hermanzdosilovic/online-compiler">GitHub</a>.</p>
<textarea readonly id="output" style="width: 50%; height: 100%; position: fixed; top: 0; right: 0; resize: none;"></textarea>
<script type="text/javascript">
API_KEY = ""; // Get yours for free at https://judge0.com/ce or https://judge0.com/extra-ce
var language_to_id = {
"Bash": 46,
"C": 50,
"C#": 51,
"C++": 54,
"Java": 62,
"Python": 71,
"Ruby": 72
};
function encode(str) {
return btoa(unescape(encodeURIComponent(str || "")));
}
function decode(bytes) {
var escaped = escape(atob(bytes || ""));
try {
return decodeURIComponent(escaped);
} catch {
return unescape(escaped);
}
}
function errorHandler(jqXHR, textStatus, errorThrown) {
$("#output").val(`${JSON.stringify(jqXHR, null, 4)}`);
$("#run").prop("disabled", false);
}
function check(token) {
$("#output").val($("#output").val() + "\n⏬ Checking submission status...");
$.ajax({
url: `https://judge0-ce.p.rapidapi.com/submissions/${token}?base64_encoded=true`,
type: "GET",
headers: {
"x-rapidapi-host": "judge0-ce.p.rapidapi.com",
"x-rapidapi-key": API_KEY
},
success: function (data, textStatus, jqXHR) {
if ([1, 2].includes(data["status"]["id"])) {
$("#output").val($("#output").val() + "\nℹ️ Status: " + data["status"]["description"]);
setTimeout(function() { check(token) }, 1000);
}
else {
var output = [decode(data["compile_output"]), decode(data["stdout"])].join("\n").trim();
$("#output").val(`${data["status"]["id"] != "3" ? "🔴" : "🟢"} ${data["status"]["description"]}\n${output}`);
$("#run").prop("disabled", false);
}
},
error: errorHandler
});
}
function run() {
$("#run").prop("disabled", true);
$("#output").val("⚙️ Creating submission...");
let encodedExpectedOutput = encode($("#expected-output").val());
if (encodedExpectedOutput === "") {
encodedExpectedOutput = null; // Assume that user does not want to use expected output if it is empty.
}
$.ajax({
url: "https://judge0-ce.p.rapidapi.com/submissions?base64_encoded=true&wait=false",
type: "POST",
contentType: "application/json",
headers: {
"x-rapidapi-host": "judge0-ce.p.rapidapi.com",
"x-rapidapi-key": API_KEY
},
data: JSON.stringify({
"language_id": language_to_id[$("#lang").val()],
"source_code": encode($("#source").val()),
"stdin": encode($("#input").val()),
"expected_output": encodedExpectedOutput,
"redirect_stderr_to_stdout": true
}),
success: function(data, textStatus, jqXHR) {
$("#output").val($("#output").val() + "\n🎉 Submission created.");
setTimeout(function() { check(data["token"]) }, 2000);
},
error: errorHandler
});
}
$("body").keydown(function (e) {
if (e.ctrlKey && e.keyCode == 13) {
run();
}
});
$("textarea").keydown(function (e) {
if (e.keyCode == 9) {
e.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;
var append = " ";
$(this).val($(this).val().substring(0, start) + append + $(this).val().substring(end));
this.selectionStart = this.selectionEnd = start + append.length;
}
});
$("#source").focus();
</script>
</body>