$(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);
}
})
});
});