/
pastebin.html
177 lines (154 loc) · 5.57 KB
/
pastebin.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<sergey-import src="header">
<title>dailybuild pastebin;#dailybuild</title>
</sergey-import>
<style>
pastebin-body {
display: flex;
justify-content: center;
align-items: center;
height: 50vh; /* Set the body to full height of the viewport */
margin: 0; /* Remove default margin */
}
.centered-div {
text-align: center; /* Optional: Align content within the centered div */
padding: 5em; /* Optional: Add padding for better visual appearance */
}
#textInput {
width: 70%;
height: 10em;
padding: 10px;
margin-top: 2em;
box-sizing: border-box;
margin-bottom: 10px;
}
.green-button {
background-color: green;
color: white;
padding: 1em;
border: none;
cursor: pointer;
}
.subtitle {
color: white;
}
</style>
<div class="project pastebin-body row slider-text align-items-center justify-content-center" style="padding-top: 80px;"></div>
<div class="centered-div">
<h1 style="color: orange">poor man cloud-native pastebin</h1>
<div style="margin-left: 10em">
<p>You can either upload some text pasted to the textbox area or a file</p>
<textarea id="textInput" placeholder="Paste text here (keep empty if you woona upload a file)"></textarea>
<button class="green-button" onclick="uploadData()">Upload Data to
Pastebin</button>
<p style="margin-top: 5em">
... or upload a text file (kode?)
<input type="file" id="fileInput" />
</p>
<p>
<div id="responseContainer"></div>
</p>
<h3 class="subtitle">your most recent pastes:</h3>
<div id="latestPasteUrls"></div>
</div>
</div>
<div class="col-xs-3"></div>
<script>
function getClientId() {
const queryParams = new URLSearchParams(window.location.search);
const clientId = queryParams.get("cid");
return clientId !== null ? clientId: null;
}
function utf8ToBase64(str) {
return btoa(unescape(encodeURIComponent(str)));
}
async function uploadData() {
const textInput = document.getElementById("textInput");
const textData = textInput.value;
const fileInput = document.getElementById("fileInput");
const file = fileInput.files[0];
const BaseURL = "https://paste.socratic.dev/paste";
let content;
if (textData) {
content = textData;}
else if (file) {
const reader = new FileReader();
content = await new Promise((resolve) => {
reader.onload = function (e) {
resolve(e.target.result);
};
reader.readAsText(file);
});
} else {
alert("Please enter text or select a file.");
return;
}
// base64 encode content to preserve formatting
content = utf8ToBase64(content);
// Create a JSON object with "content" as the key
var data = {
content: content,
};
var clientId = getClientId();
if (clientId !== null) {
data["client_id"] = clientId;
}
// Convert the JSON object to a string
const jsonData = JSON.stringify(data);
// Send the data to the API using the fetch API
try {
const response = await fetch(BaseURL, {
method: "POST",
headers: {
accept: "application/json",
"Content-Type": "application/json",
},
body: jsonData,
});
const result = await response.json();
// Display the API response on the web page
const responseContainer =
document.getElementById("responseContainer");
var id = result["id"];
id = id.replace('"', "");
const url = `${BaseURL}?id=${id}`;
responseContainer.innerHTML = `<p>Visit this URL to view most recent paste: <a href=${url}>${url}</a></p>`;
textInput.value = ""; //clearing textbox from its value
} catch (error) {
console.error("Error sending data to API:", error);
alert("Error sending data to API. Check payload size: payload must be smaller than 5MB");
}
}
async function displayPasteUrls() {
var pastebinAPIuri = "https://paste.socratic.dev/paste/api/pastes";
var clientId = getClientId();
if (clientId !== null) {
pastebinAPIuri = pastebinAPIuri + "?client_id=" + clientId;
}
try {
const response = await fetch(pastebinAPIuri);
const data = await response.json();
// Get the div where we'll display the latest Paste Urls
const urlsDiv = document.getElementById("latestPasteUrls");
// Display each Url
data.forEach(url => {
const p = document.createElement('p');
p.innerHTML = `<a href="${url}" target="_blank">${url}</a>`;
urlsDiv.appendChild(p);
});
} catch (error) {
console.error("Error fetching strings from API:", error);
alert("Error fetching strings from API.");
}
}
window.onload = function() {
displayPasteUrls();
};
</script>
<div style="margin-top: 260px;">
<sergey-import src="footer">
<a href="dpro-comic.html#top" title="Back to top">
</sergey-import>
<sergey-import src="scripts" />
</div>
</body>
</html>