From c6ec81174ce04edc2a8ac396e1ef2489e809130a Mon Sep 17 00:00:00 2001 From: Jonas Kohl Date: Thu, 7 Nov 2024 16:19:33 +0100 Subject: CSRF tokens --- public/site.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 public/site.js (limited to 'public/site.js') diff --git a/public/site.js b/public/site.js new file mode 100644 index 0000000..21d51a7 --- /dev/null +++ b/public/site.js @@ -0,0 +1,80 @@ +$(function() { + var csrf = $("#csrf-token").val(); + var editor = CodeMirror.fromTextArea($("#editor")[0], { + lineNumbers: true, + mode: "text/x-java", + theme: "eclipse", + tabSize: 4, + indentWithTabs: false, + smartIndent: true, + indentUnit: 4 + }); + editor.setSize(898, 500); + + $("form").on("submit", function(e) { + e.preventDefault(); + + $("#loader").show(); + $("#output").empty().hide(); + + $.ajax({ + type: "POST", + url: location.href, + data: { + code: editor.getValue(), + csrf: csrf + }, + dataType: "json", + success: function(data) { + $("#loader").hide(); + + if (!data.ok) { + alert("Error: " + data.message); + if (data.csrf) + csrf = data.csrf; + return; + } + + csrf = data.csrf; + + if (data.compile.status == 0) { + if (data.run.status != null) { + // program was executed + $("#output").append( + $("

").text(data.run.status == 0 ? "Executed successfully" : "Execution failed").css("color", data.run.status == 0 ? "green" : "red"), + ); + + if (data.run.stdout) { + $("#output").append( + $("

").text("Program output:"), + $("
").append(
+                                    $("").text(data.run.stdout)
+                                )
+                            );
+                        }
+
+                        if (data.run.stderr) {
+                            $("#output").append(
+                                $("

").text("Error output:"), + $("
").append(
+                                    $("").text(data.run.stderr)
+                                )
+                            );
+                        }
+                    }
+                } else {
+                    // compilation failed, stderr will contain compiler errors
+                    $("#output").append(
+                        $("

").text("Compilation failed").css("color", "red"), + $("

").text("Compiler errors:"), + $("
").append(
+                            $("").text(data.compile.stderr)
+                        )
+                    );
+                }
+
+                $("#output").slideDown(400);
+            }
+        })
+    });
+});
-- 
cgit v1.2.3