|
| 1 | +const environment = document |
| 2 | + .querySelector('meta[name="environment"]') |
| 3 | + .getAttribute("content"); |
| 4 | +const API_URL = |
| 5 | + environment === "production" |
| 6 | + ? "https://paste.socratic.dev/paste" |
| 7 | + : "http://127.0.0.1:3000/paste"; |
| 8 | + |
| 9 | +const TIMEOUT_MS = 10000; |
| 10 | + |
| 11 | +function getClientId() { |
| 12 | + const queryParams = new URLSearchParams(window.location.search); |
| 13 | + const clientId = queryParams.get("cid"); |
| 14 | + return clientId !== null ? clientId : null; |
| 15 | +} |
| 16 | +function utf8ToBase64(str) { |
| 17 | + return btoa(unescape(encodeURIComponent(str))); |
| 18 | +} |
| 19 | + |
| 20 | +async function submitText() { |
| 21 | + const textInput = document.getElementById("pasteContent"); |
| 22 | + const textData = textInput.value; |
| 23 | + |
| 24 | + let content; |
| 25 | + if (textData) { |
| 26 | + content = textData; |
| 27 | + console.log(`content: {content}`); |
| 28 | + } else { |
| 29 | + alert("Please enter text to be saved"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // base64 encode content to preserve formatting |
| 34 | + content = utf8ToBase64(content); |
| 35 | + |
| 36 | + // Create a JSON object with "content" as the key |
| 37 | + var data = { |
| 38 | + content: content, |
| 39 | + }; |
| 40 | + |
| 41 | + var clientId = getClientId(); |
| 42 | + if (clientId !== null) { |
| 43 | + data["client_id"] = clientId; |
| 44 | + } |
| 45 | + |
| 46 | + // Convert the JSON object to a string |
| 47 | + const jsonData = JSON.stringify(data); |
| 48 | + |
| 49 | + // Send the data to the API using the fetch API |
| 50 | + try { |
| 51 | + const controller = new AbortController(); |
| 52 | + const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS); |
| 53 | + |
| 54 | + const response = await fetch(API_URL, { |
| 55 | + method: "POST", |
| 56 | + headers: { |
| 57 | + accept: "application/json", |
| 58 | + "Content-Type": "application/json", |
| 59 | + }, |
| 60 | + body: jsonData, |
| 61 | + signal: controller.signal, |
| 62 | + }); |
| 63 | + |
| 64 | + clearTimeout(timeoutId); |
| 65 | + const result = await response.json(); |
| 66 | + |
| 67 | + // Display the API response on the web page |
| 68 | + const responseContainer = document.getElementById("responseContainer"); |
| 69 | + var id = result["id"]; |
| 70 | + id = id.replace('"', ""); |
| 71 | + const url = `${API_URL}?id=${id}`; |
| 72 | + responseContainer.innerHTML = `<p>Visit this URL to view most recent paste: <a href=${url}>${url}</a></p>`; |
| 73 | + textInput.value = ""; //clearing textbox from its value |
| 74 | + } catch (error) { |
| 75 | + console.error("Error sending data to API:", error); |
| 76 | + alert("Error sending data to API"); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +async function displayPasteUrls() { |
| 81 | + var pastebinAPIuri = `${API_URL}/api/pastes`; |
| 82 | + var clientId = getClientId(); |
| 83 | + console.log("muh client id"); |
| 84 | + console.log(clientId); |
| 85 | + if (clientId !== null) { |
| 86 | + pastebinAPIuri = pastebinAPIuri + "?client_id=" + clientId; |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + const controller = new AbortController(); |
| 91 | + const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS); |
| 92 | + |
| 93 | + const response = await fetch(pastebinAPIuri, { |
| 94 | + signal: controller.signal, |
| 95 | + }); |
| 96 | + clearTimeout(timeoutId); |
| 97 | + |
| 98 | + // Setup timeout for JSON parsing |
| 99 | + const jsonPromise = response.json(); |
| 100 | + const timeoutPromise = new Promise((_, reject) => |
| 101 | + setTimeout(() => reject(new Error("JSON parsing timed out")), TIMEOUT_MS) |
| 102 | + ); |
| 103 | + |
| 104 | + const data = await Promise.race([jsonPromise, timeoutPromise]); |
| 105 | + |
| 106 | + // Get the div where we'll display the latest Paste Urls |
| 107 | + const urlsDiv = document.getElementById("latestPasteUrls"); |
| 108 | + |
| 109 | + // Display each Url |
| 110 | + data.forEach((url) => { |
| 111 | + const p = document.createElement("p"); |
| 112 | + p.innerHTML = `<a href="${url}" target="_blank">${url}</a>`; |
| 113 | + urlsDiv.appendChild(p); |
| 114 | + }); |
| 115 | + } catch (error) { |
| 116 | + console.error("Error fetching strings from API:", error); |
| 117 | + alert("Error fetching strings from API."); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +window.onload = function () { |
| 122 | + displayPasteUrls(); |
| 123 | +}; |
0 commit comments