mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Split code into classes to make the codebase look cleaner
Added a new favicon/icon Add more checks before uploading a paste, changed alert to use the showNotification function Added password modal for private pastes instead of using alert Fix a paste page navigation in dashboard Improve auth css
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
const siteInfo = [];
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async function () {
|
||||
var visibility = document.getElementById("visibility");
|
||||
visibility.value = "0";
|
||||
getSiteInfo();
|
||||
});
|
||||
|
||||
|
||||
@@ -13,11 +16,20 @@ document.getElementById("create-form").addEventListener("submit", async function
|
||||
var password = document.getElementById("password").value;
|
||||
var syntax = document.getElementById("syntax").value;
|
||||
|
||||
if (content.length > siteInfo.maxFileSize) {
|
||||
showNotification("Content exceeds maximum size.", "error");
|
||||
return;
|
||||
}
|
||||
if (content.length === 0) {
|
||||
showNotification("Content cannot be empty.", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (visibility === "2" && password !== "" && password.length >= 6) {
|
||||
content = await encryptAES(content, password);
|
||||
}
|
||||
else if (visibility === "2") {
|
||||
alert("Password must be at least 6 characters long");
|
||||
showNotification("Password must be at least 6 characters long.", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -42,6 +54,16 @@ function changeVisibility() {
|
||||
}
|
||||
}
|
||||
|
||||
function getSiteInfo() {
|
||||
fetch("/api/site/info")
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
siteInfo = data.info;
|
||||
var content = document.getElementById("content")
|
||||
content.setAttribute("maxlength", siteInfo.maxFileSize);
|
||||
});
|
||||
}
|
||||
|
||||
async function encryptAES(content, password) {
|
||||
const enc = new TextEncoder();
|
||||
const keyMaterial = await window.crypto.subtle.importKey(
|
||||
|
||||
@@ -66,6 +66,7 @@ const navbarHTML = `
|
||||
}
|
||||
|
||||
.nav-link {
|
||||
background-color: #171717;
|
||||
color: rgba(255, 255, 255, 0.85);
|
||||
text-decoration: none;
|
||||
font-size: 1.1rem;
|
||||
@@ -217,7 +218,7 @@ function showNotification(message, type = 'info', duration = 3000) {
|
||||
position: fixed;
|
||||
bottom: 32px;
|
||||
right: 32px;
|
||||
z-index: 2000;
|
||||
z-index: 1000;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
|
||||
@@ -14,14 +14,99 @@ document.addEventListener("DOMContentLoaded", async function () {
|
||||
const [content, info] = await Promise.all([contentPromise, infoPromise]);
|
||||
const paste = info.paste;
|
||||
|
||||
function showPasswordModal(errorMsg = "") {
|
||||
return new Promise((resolve, reject) => {
|
||||
const oldModal = document.getElementById("password-modal-bg");
|
||||
if (oldModal) oldModal.remove();
|
||||
|
||||
const modalBg = document.createElement("div");
|
||||
modalBg.id = "password-modal-bg";
|
||||
|
||||
const modal = document.createElement("div");
|
||||
modal.id = "password-modal";
|
||||
|
||||
const title = document.createElement("div");
|
||||
title.innerText = "Private Paste";
|
||||
title.className = "modal-title";
|
||||
|
||||
const label = document.createElement("label");
|
||||
label.innerText = "Enter the password to view this paste:";
|
||||
label.htmlFor = "password-input";
|
||||
|
||||
const input = document.createElement("input");
|
||||
input.type = "password";
|
||||
input.id = "password-input";
|
||||
input.autocomplete = "current-password";
|
||||
|
||||
if (errorMsg) {
|
||||
showNotification(errorMsg, "error", 3000);
|
||||
}
|
||||
|
||||
const btnRow = document.createElement("div");
|
||||
btnRow.className = "modal-btn-row";
|
||||
|
||||
const okBtn = document.createElement("button");
|
||||
okBtn.innerText = "Unlock";
|
||||
|
||||
const cancelBtn = document.createElement("button");
|
||||
cancelBtn.innerText = "Cancel";
|
||||
|
||||
btnRow.appendChild(cancelBtn);
|
||||
btnRow.appendChild(okBtn);
|
||||
|
||||
modal.appendChild(title);
|
||||
modal.appendChild(label);
|
||||
modal.appendChild(input);
|
||||
modal.appendChild(btnRow);
|
||||
modalBg.appendChild(modal);
|
||||
document.body.appendChild(modalBg);
|
||||
|
||||
input.focus();
|
||||
|
||||
function cleanup() {
|
||||
if (modalBg.parentNode) modalBg.parentNode.removeChild(modalBg);
|
||||
}
|
||||
|
||||
okBtn.onclick = () => {
|
||||
resolve(input.value);
|
||||
};
|
||||
cancelBtn.onclick = () => {
|
||||
cleanup();
|
||||
reject(new Error("User cancelled"));
|
||||
};
|
||||
input.addEventListener("keydown", e => {
|
||||
if (e.key === "Enter") {
|
||||
okBtn.click();
|
||||
} else if (e.key === "Escape") {
|
||||
cancelBtn.click();
|
||||
}
|
||||
});
|
||||
modalBg.addEventListener("click", e => {
|
||||
if (e.target === modalBg) {
|
||||
cancelBtn.click();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (paste.visibility === 2) {
|
||||
const password = prompt("Enter the password");
|
||||
try {
|
||||
const decrypted = await decryptAES(content, password);
|
||||
blur2.dataset.decrypted = "true";
|
||||
addContent(decrypted, paste.syntax);
|
||||
} catch (error) {
|
||||
alert("Failed to decrypt, invalid password, or invalid data.");
|
||||
let success = false;
|
||||
let lastError = "";
|
||||
while (!success) {
|
||||
try {
|
||||
const password = await showPasswordModal(lastError);
|
||||
const decrypted = await decryptAES(content, password);
|
||||
blur2.dataset.decrypted = "true";
|
||||
addContent(decrypted, paste.syntax);
|
||||
document.getElementById("password-modal-bg")?.remove();
|
||||
success = true;
|
||||
} catch (error) {
|
||||
if (error.message === "User cancelled") {
|
||||
document.getElementById("password-modal-bg")?.remove();
|
||||
break;
|
||||
}
|
||||
lastError = "Failed to decrypt, invalid password, or invalid data.";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
blur2.dataset.decrypted = "true";
|
||||
@@ -36,6 +121,16 @@ function addInfo(paste) {
|
||||
document.getElementById("paste-date").innerText = `${convertUnixToLocal(paste.created)}`;
|
||||
document.getElementById("paste-syntax").innerText = paste.syntax;
|
||||
document.getElementById("paste-size").innerText = paste.size === 0 ? "" : formatSize(paste.size);
|
||||
if (paste.size !== paste.trueSize && paste.trueSize) {
|
||||
const sizeElem = document.getElementById("paste-size");
|
||||
sizeElem.classList.add("has-true-size-tooltip");
|
||||
let tooltip = document.createElement("div");
|
||||
tooltip.className = "true-size-tooltip";
|
||||
tooltip.innerText = `True size: ${formatSize(paste.trueSize)}`;
|
||||
sizeElem.appendChild(tooltip);
|
||||
sizeElem.onmouseenter = () => { tooltip.style.opacity = 1; };
|
||||
sizeElem.onmouseleave = () => { tooltip.style.opacity = 0; };
|
||||
}
|
||||
document.getElementById("paste-author").innerText = paste.username;
|
||||
document.getElementById("author-link").href = paste.username === "Anonymous" ? "" : `/u/${paste.username}`;
|
||||
if (paste.username === "Anonymous") {
|
||||
@@ -81,6 +176,7 @@ function addContent(content, syntax) {
|
||||
code.classList.add("language-" + syntax === "plaintext" ? "none" : syntax);
|
||||
if (syntax !== "plaintext" && syntax !== "none" && syntax !== "" && syntax !== null) {
|
||||
hljs.highlightElement(code);
|
||||
document.getElementById("paste-content").style.padding = "0px"
|
||||
}
|
||||
hljs.lineNumbersBlock(code);
|
||||
document.getElementById("content-throbber").style.display = "none";
|
||||
|
||||
@@ -55,13 +55,13 @@ document.addEventListener("DOMContentLoaded", async function () {
|
||||
|
||||
await loadUserPastes(userData, 0);
|
||||
currentPage = 0;
|
||||
document.getElementById("user-prev-page").onclick = async function () {
|
||||
document.getElementById("previous-button").onclick = async function () {
|
||||
if (currentPage > 0) {
|
||||
currentPage--;
|
||||
await loadUserPastes(userData, currentPage);
|
||||
}
|
||||
};
|
||||
document.getElementById("user-next-page").onclick = async function () {
|
||||
document.getElementById("next-button").onclick = async function () {
|
||||
if (!lastPage) {
|
||||
currentPage++;
|
||||
await loadUserPastes(userData, currentPage);
|
||||
|
||||
Reference in New Issue
Block a user