From 8ccf2f9bf55fdf9d707c76aa9074d60d3207fc13 Mon Sep 17 00:00:00 2001 From: Jonas Kohl Date: Fri, 8 Nov 2024 11:43:34 +0100 Subject: Add permalink option --- public/index.php | 30 ++++++++++++++++++++++++++---- public/site.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/public/index.php b/public/index.php index eab78af..ad32102 100644 --- a/public/index.php +++ b/public/index.php @@ -40,12 +40,23 @@ public class Program { } java; +const CODE_MAXLENGTH = 16383; + $code = null; -$result = null; -if (isset($_POST["code"]) && strlen($_POST["code"]) <= 16383) { - $id = getid(); +if (!empty($_GET["c"])) { + $compressedCode = @base64_decode($_GET["c"], true); + if ($compressedCode !== false) { + $uncompressedCode = @gzinflate($compressedCode, CODE_MAXLENGTH); + if ($compressedCode !== false) { + $code = &$uncompressedCode; + } + } +} + +$result = null; +if (isset($_POST["code"]) && strlen($_POST["code"]) <= CODE_MAXLENGTH) { header("Content-Type: application/json"); $code = $_POST["code"]; @@ -60,6 +71,16 @@ if (isset($_POST["code"]) && strlen($_POST["code"]) <= 16383) { exit; } + if (($_POST["permalink"] ?? "") === "1") { + echo json_encode([ + "ok" => true, + "data" => base64_encode(gzdeflate($code, 9, ZLIB_ENCODING_RAW)), + ]); + exit; + } + + $id = getid(); + $runnerDir = __DIR__ . "/../_runners/$id"; $srcDir = $runnerDir . "/src"; @@ -150,10 +171,11 @@ $csrf = csrf_token();

Powered by Eclipse Temurin

- +
+
diff --git a/public/site.js b/public/site.js index ed7173d..e19d6ef 100644 --- a/public/site.js +++ b/public/site.js @@ -21,6 +21,34 @@ $(function() { a.remove(); }); + $("#permalink").on("click", function() { + $("#loader").show(); + $.ajax({ + type: "POST", + url: location.href, + data: { + code: editor.getValue(), + csrf: csrf, + permalink: "1" + }, + dataType: "json", + success: function(data) { + $("#loader").hide(); + + if (!data.ok) { + alert("Error: " + data.message); + if (data.csrf) + csrf = data.csrf; + return; + } + + csrf = data.csrf; + + location.href = "?c=" + encodeURIComponent(data.data); + } + }); + }); + $("form").on("submit", function(e) { e.preventDefault(); -- cgit v1.2.3