Optimize HTML

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
2025-10-08 15:14:18 +03:00
parent 113b6a6d66
commit e8a375796d

37
main.go
View File

@@ -37,7 +37,6 @@ type meta struct {
}
func init() {
// Ensure correct types for webmanifest on some systems
_ = mime.AddExtensionType(".webmanifest", "application/manifest+json")
}
@@ -89,17 +88,13 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>slenpaste</title>
<!-- Icons / PWA -->
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="icon" href="/favicon.ico">
<link rel="manifest" href="/site.webmanifest">
<meta name="theme-color" content="#ffffff">
<style>
body { font-family: system-ui, sans-serif; max-width: 800px; margin: 2rem auto; padding: 0 1rem; }
pre { background: #f6f6f6; padding: .75rem 1rem; border-radius: .5rem; overflow: auto; }
@@ -130,12 +125,9 @@ Upload from stdin (no file param, expire after 5m):
Upload from stdin and expire on first view:
cat yourfile.txt | curl --data-binary @- "%s/?expiry=view"
</pre>
<a href="https://git.slendi.dev/Slendi/slenpaste.koplugin">Download the new KOReader plugin!</a>
<form id="form" enctype="multipart/form-data" method="post" style="margin-top: 13px;">
<input type="file" name="file" id="fileInput">
<div class="controls" style="margin-top: .75rem">
<fieldset>
<legend>Expiry</legend>
@@ -145,16 +137,13 @@ Upload from stdin and expire on first view:
<label><input type="radio" name="expiry" value="24h"> 1 day</label>
<label><input type="radio" name="expiry" value="view"> Expire on first view</label>
</fieldset>
<div class="inline">
<label for="fname">Filename (optional):</label>
<input type="text" id="fname" placeholder="example.txt">
</div>
<input type="submit" value="Upload file">
</div>
</form>
<h2 style="margin-top:1.5rem">Quick text</h2>
<p class="small">Type or paste text and upload as a .txt (no file chooser needed).</p>
<textarea id="textArea" placeholder="Type or paste here..."></textarea>
@@ -162,19 +151,15 @@ Upload from stdin and expire on first view:
<button id="uploadTextBtn" type="button">Upload text</button>
<label class="inline small"><input type="checkbox" id="textAsMd"> Save as .md</label>
</div>
<h2 style="margin-top:1.5rem">Paste or drop images</h2>
<div id="dropzone" tabindex="0">
Paste an image (Ctrl/Cmd+V) or drag &amp; drop files here
<div class="small">Images are uploaded as files. Other files dropped here work too.</div>
</div>
<div id="result"></div>
<script>
(function() {
"use strict";
const form = document.getElementById("form");
const fileInput = document.getElementById("fileInput");
const fname = document.getElementById("fname");
@@ -183,18 +168,15 @@ Upload from stdin and expire on first view:
const textAsMd = document.getElementById("textAsMd");
const dropzone = document.getElementById("dropzone");
const result = document.getElementById("result");
function getExpiry() {
const checked = form.querySelector('input[name="expiry"]:checked');
return checked ? checked.value : "0";
}
function setBusy(b) {
if (b) {
result.textContent = "Uploading...";
}
}
function showLink(url) {
result.innerHTML = 'URL: <a href="' + url + '" target="_blank" rel="noopener">' + url + '</a>';
try {
@@ -202,7 +184,6 @@ Upload from stdin and expire on first view:
result.innerHTML += ' <span class="small">(copied)</span>';
} catch(e) {}
}
async function uploadFormData(fd, expiry) {
setBusy(true);
const res = await fetch("/?expiry=" + encodeURIComponent(expiry), {
@@ -217,38 +198,29 @@ Upload from stdin and expire on first view:
const url = (await res.text()).trim();
showLink(url);
}
async function uploadBlobAsFile(blob, filename, expiry) {
const fd = new FormData();
const file = new File([blob], filename, { type: blob.type || "application/octet-stream" });
fd.append("file", file);
return uploadFormData(fd, expiry);
}
// Enhance file input submit (without relying on default form submit)
form.addEventListener("submit", async (e) => {
// Use JS path when possible; the default submit still works if JS fails.
e.preventDefault();
if (!fileInput.files || fileInput.files.length === 0) {
result.textContent = "Choose a file first.";
return;
}
const expiry = getExpiry();
// If user provided a filename, recreate the File with that name.
let file = fileInput.files[0];
if (fname.value.trim()) {
file = new File([file], fname.value.trim(), { type: file.type });
}
const fd = new FormData();
fd.append("file", file);
await uploadFormData(fd, expiry);
fileInput.value = "";
fname.value = "";
});
// Upload text as a .txt (or .md)
uploadTextBtn.addEventListener("click", async () => {
const text = textArea.value;
if (!text) {
@@ -258,11 +230,8 @@ Upload from stdin and expire on first view:
const expiry = getExpiry();
const name = (fname.value.trim() || (textAsMd.checked ? "text.md" : "text.txt"));
await uploadBlobAsFile(new Blob([text], { type: "text/plain" }), name, expiry);
// clear only the filename; keep text in case they want to tweak
fname.value = "";
});
// Drag & drop support
;["dragenter","dragover"].forEach(ev => {
dropzone.addEventListener(ev, (e) => {
e.preventDefault(); e.stopPropagation();
@@ -280,14 +249,11 @@ Upload from stdin and expire on first view:
const files = Array.from(e.dataTransfer.files || []);
if (files.length === 0) return;
const fd = new FormData();
// support multi-file; server will handle each request, so send one by one for URLs
for (const f of files) {
fd.set("file", f);
await uploadFormData(fd, expiry);
}
});
// Paste image (or any file) support
window.addEventListener("paste", async (e) => {
const items = e.clipboardData && e.clipboardData.items ? Array.from(e.clipboardData.items) : [];
if (!items.length) return;
@@ -301,9 +267,6 @@ Upload from stdin and expire on first view:
const name = fname.value.trim() || ("pasted." + ext);
await uploadBlobAsFile(blob, name, expiry);
handled = true;
} else if (it.kind === "string") {
// If user copies text, allow quick text upload via paste when textarea is focused
// We'll let browser put text into the textarea naturally; no upload here.
}
}
if (handled) e.preventDefault();