Clicking on shortUrl copies it and bug fix

This commit is contained in:
SinTan1729 2022-11-10 19:01:21 -06:00
parent d196c0373f
commit e2656ff94e

View File

@ -28,13 +28,14 @@ const displayData = (data) => {
}; };
const addAlertBox = async (s, t) => { const addAlertBox = async (s, t) => {
document.getElementById("alertBox")?.remove();
const controls = document.querySelector(".pure-controls"); const controls = document.querySelector(".pure-controls");
const alertBox = document.createElement("p"); const alertBox = document.createElement("p");
alertBox.setAttribute("id", "alertBox"); alertBox.setAttribute("id", "alertBox");
alertBox.setAttribute("style", `color:${t}`); alertBox.setAttribute("style", `color:${t}`);
alertBox.innerHTML = s; alertBox.innerHTML = s;
controls.appendChild(alertBox); controls.appendChild(alertBox);
} };
const TR = (row) => { const TR = (row) => {
const tr = document.createElement("tr"); const tr = document.createElement("tr");
@ -51,8 +52,13 @@ const TR = (row) => {
return tr; return tr;
}; };
const copyShortUrl = (s) => {
navigator.clipboard.writeText(`${window.location.host}/${s}`);
addAlertBox(`Short URL ${s} copied to clipboard!`, "green");
};
const A = (s) => `<a href='${s}'>${s}</a>`; const A = (s) => `<a href='${s}'>${s}</a>`;
const A_INT = (s) => `<a href='/${s}'>${window.location.host}/${s}</a>`; const A_INT = (s) => `<a href="javascript:copyShortUrl('${s}');">${window.location.host}/${s}</a>`;
const deleteButton = (shortUrl) => { const deleteButton = (shortUrl) => {
const td = document.createElement("td"); const td = document.createElement("td");
@ -63,6 +69,7 @@ const deleteButton = (shortUrl) => {
btn.onclick = e => { btn.onclick = e => {
e.preventDefault(); e.preventDefault();
if (confirm("Do you want to delete the entry " + shortUrl + "?")) { if (confirm("Do you want to delete the entry " + shortUrl + "?")) {
document.getElementById("alertBox")?.remove();
fetch(`/api/${shortUrl}`, { fetch(`/api/${shortUrl}`, {
method: "DELETE" method: "DELETE"
}).then(_ => refreshData()); }).then(_ => refreshData());
@ -94,20 +101,19 @@ const submitForm = () => {
}) })
.then((res) => { .then((res) => {
if (!res.ok) { if (!res.ok) {
if (document.getElementById("alertBox") == null) {
addAlertBox("Short URL not valid or already in use!", "red"); addAlertBox("Short URL not valid or already in use!", "red");
} return "error";
} }
else { else {
return res.text(); return res.text();
} }
}).then((text) => { }).then((text) => {
navigator.clipboard.writeText(`${window.location.host}/${text}`); if (text != "error") {
addAlertBox("Short URL copied to clipboard!", "green"); copyShortUrl(text);
longUrl.value = ""; longUrl.value = "";
shortUrl.value = ""; shortUrl.value = "";
refreshData(); refreshData();
}
}); });
}; };