$(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(null, 500); $("#dlCodeButton").on("click", function() { var a = $("") .attr("href", "data:text/x-java;charset=UTF-8," + encodeURIComponent(editor.getValue())) .attr("download", "Program.java") .css("display", "none") .appendTo("body"); a.get(0).click(); a.remove(); }); $("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);
}
})
});
});