|
| 1 | +<p> |
| 2 | + Discover what fart you are! Simply enter the first letter of your name and the day of the month you were born! Ex: A 18 |
| 3 | +</p> |
| 4 | + |
| 5 | +<input type="text" id="stdin"/> |
| 6 | +<p id="credits"></p> |
| 7 | +<form id="form"> |
| 8 | + <button id="run">Run</button> |
| 9 | +</form> |
| 10 | +<code id="output"></code> |
| 11 | + |
| 12 | +<script> |
| 13 | +var run = document.getElementById("run"); |
| 14 | + |
| 15 | +run.addEventListener("click", function (e) { |
| 16 | + |
| 17 | + e.preventDefault(); |
| 18 | + |
| 19 | + var stdin = document.getElementById("stdin"); |
| 20 | + var form = document.getElementById("form"); |
| 21 | + var credits = document.getElementById("credits"); |
| 22 | + var output = document.getElementById("output"); |
| 23 | + |
| 24 | + output.innerHTML = "Loading..."; |
| 25 | + |
| 26 | + console.log(stdin.value); |
| 27 | + |
| 28 | + var obj = {}; |
| 29 | + obj["clientId"] = "yourClientId"; |
| 30 | + obj["clientSecret"] = "yourClientSecret"; |
| 31 | + obj["script"] = "yourFormattedJavaCode"; |
| 32 | + obj["language"] = "java"; |
| 33 | + obj["versionIndex"] = "0"; |
| 34 | + obj["stdin"] = stdin.value; |
| 35 | + var jsonStr = JSON.stringify(obj); |
| 36 | + |
| 37 | + console.log(jsonStr); |
| 38 | + |
| 39 | + const proxyurl = "https://cors-anywhere.herokuapp.com/"; |
| 40 | + const url = "https://api.jdoodle.com/v1/execute"; |
| 41 | + fetch(proxyurl + url, { |
| 42 | + method: 'post', |
| 43 | + headers: { |
| 44 | + 'Accept': 'application/json', |
| 45 | + 'Content-Type': 'application/json' |
| 46 | + }, |
| 47 | + body: jsonStr |
| 48 | + }) |
| 49 | + .then(response => { |
| 50 | + return response.json() |
| 51 | + }) |
| 52 | + .then(data => { |
| 53 | + console.log(data); |
| 54 | + var outputStr = data.output; |
| 55 | + console.log(outputStr); |
| 56 | + var formattedOutput = outputStr.replace(/(?:\r\n|\r|\n)/g, '<br>'); |
| 57 | + output.innerHTML = formattedOutput; |
| 58 | + }) |
| 59 | + .catch(() => console.log("Can’t access " + url + " response. Blocked by browser?")) |
| 60 | + |
| 61 | + const creditsUrl = "https://api.jdoodle.com/v1/credit-spent"; |
| 62 | + fetch(proxyurl + creditsUrl, { |
| 63 | + method: 'post', |
| 64 | + headers: { |
| 65 | + 'Accept': 'application/json', |
| 66 | + 'Content-Type': 'application/json' |
| 67 | + }, |
| 68 | + body: jsonStr |
| 69 | + }) |
| 70 | + .then(response => { |
| 71 | + return response.json() |
| 72 | + }) |
| 73 | + .then(data => { |
| 74 | + console.log(data); |
| 75 | + var creditsLeft = 200 - data.used; |
| 76 | + console.log(credits); |
| 77 | + credits.innerHTML = "Runs left: " + creditsLeft; |
| 78 | + }) |
| 79 | + .catch(() => console.log("Can’t access " + url + " response. Blocked by browser?")) |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +}); |
| 85 | +</script> |
0 commit comments