1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-10-16 21:33:54 -05:00
chhoto-url/actix/resources/script.js

181 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-04-03 13:50:23 -05:00
const getSiteUrl = async () => await fetch("/api/siteurl")
2022-11-10 23:19:42 -06:00
.then(res => res.text())
.then(text => {
if (text == "unset") {
return window.location.host;
}
else {
return text;
}
});
2022-11-10 20:17:39 -06:00
2023-04-08 02:52:16 -05:00
const auth_fetch = async (link) => {
let reply = await fetch(link).then(res => res.text());
if (reply == "logged_out") {
pass = prompt("Please enter passkey to access this website");
await fetch("/api/login", {
method: "POST",
body: pass
});
return auth_fetch(link);
} else {
return reply;
}
}
2020-02-14 12:52:14 -06:00
const refreshData = async () => {
2023-04-08 02:52:16 -05:00
let data = await auth_fetch("/api/all");
2020-02-14 12:52:14 -06:00
data = data
.split("\n")
.filter(line => line !== "")
.map(line => line.split(","))
.map(arr => ({
2022-11-03 16:53:04 -05:00
short: arr[0],
2020-02-14 12:52:14 -06:00
long: arr[1],
2022-11-03 16:53:04 -05:00
hits: arr[2]
2020-02-14 12:52:14 -06:00
}));
displayData(data);
};
2022-11-10 20:17:39 -06:00
const displayData = async (data) => {
2022-11-10 23:19:42 -06:00
let site = await getSiteUrl();
2022-11-09 18:55:50 -06:00
table_box = document.querySelector(".pure-table");
if (data.length == 0) {
table_box.style.visibility = "hidden";
}
else {
const table = document.querySelector("#url-table");
if (!window.isSecureContext) {
const shortUrlHeader = document.getElementById("short-url-header");
2022-11-12 23:01:21 -06:00
shortUrlHeader.innerHTML = "Short URL (right click and copy)";
}
2022-11-09 18:55:50 -06:00
table_box.style.visibility = "visible";
table.innerHTML = ''; // Clear
2022-11-10 20:17:39 -06:00
data.forEach(tr => table.appendChild(TR(tr, site)));
2022-11-09 18:55:50 -06:00
}
2020-02-14 12:52:14 -06:00
};
const showAlert = async (text, col) => {
document.getElementById("alertBox")?.remove();
2022-11-08 19:18:14 -06:00
const controls = document.querySelector(".pure-controls");
2022-11-10 18:11:57 -06:00
const alertBox = document.createElement("p");
alertBox.setAttribute("id", "alertBox");
2022-11-10 23:19:42 -06:00
alertBox.setAttribute("style", `color:${col}`);
alertBox.innerHTML = text;
2022-11-10 18:11:57 -06:00
controls.appendChild(alertBox);
};
2022-11-08 19:18:14 -06:00
2022-11-10 20:17:39 -06:00
const TR = (row, site) => {
2020-02-14 12:52:14 -06:00
const tr = document.createElement("tr");
const longTD = TD(A_LONG(row.long));
var shortTD = null;
if (window.isSecureContext) {
shortTD = TD(A_SHORT(row.short, site));
}
else {
shortTD = TD(A_SHORT_INSECURE(row.short, site));
}
2022-11-03 16:53:04 -05:00
const hitsTD = TD(row.hits);
2020-02-16 09:52:54 -06:00
const btn = deleteButton(row.short);
2020-02-14 12:52:14 -06:00
tr.appendChild(shortTD);
tr.appendChild(longTD);
2022-11-03 16:53:04 -05:00
tr.appendChild(hitsTD);
2020-02-16 09:52:54 -06:00
tr.appendChild(btn);
2020-02-14 12:52:14 -06:00
return tr;
};
2022-11-10 23:19:42 -06:00
const copyShortUrl = async (link) => {
const site = await getSiteUrl();
try {
navigator.clipboard.writeText(`${site}/${link}`);
showAlert(`Short URL ${link} was copied to clipboard!`, "green");
} catch (e) {
console.log(e);
showAlert("Could not copy short URL to clipboard, please do it manually.", "red");
}
};
const addProtocol = (input) => {
var url = input.value.trim();
2022-11-13 00:06:29 -06:00
if (url != "" && !~url.indexOf("://") && !~url.indexOf("magnet:")) {
url = "https://" + url;
}
input.value = url;
return input
}
const A_LONG = (s) => `<a href='${s}'>${s}</a>`;
2022-11-12 23:01:21 -06:00
const A_SHORT = (s, t) => `<a href="javascript:copyShortUrl('${s}');">${s}</a>`;
const A_SHORT_INSECURE = (s, t) => `<a href="${t}/${s}">${s}</a>`;
2020-02-14 12:52:14 -06:00
2020-02-16 09:52:54 -06:00
const deleteButton = (shortUrl) => {
2022-11-09 18:55:50 -06:00
const td = document.createElement("td");
2020-02-16 09:52:54 -06:00
const btn = document.createElement("button");
btn.innerHTML = "&times;";
btn.onclick = e => {
e.preventDefault();
2022-11-09 17:58:15 -06:00
if (confirm("Do you want to delete the entry " + shortUrl + "?")) {
document.getElementById("alertBox")?.remove();
2023-04-03 17:58:19 -05:00
fetch(`/api/del/${shortUrl}`, {
2022-11-09 17:58:15 -06:00
method: "DELETE"
}).then(_ => refreshData());
}
2020-02-16 09:52:54 -06:00
};
2022-11-09 18:55:50 -06:00
td.setAttribute("name", "deleteBtn");
td.appendChild(btn);
return td;
2020-02-16 09:52:54 -06:00
};
2020-02-14 12:52:14 -06:00
const TD = (s) => {
const td = document.createElement("td");
2022-11-03 15:19:22 -05:00
const div = document.createElement("div");
div.innerHTML = s;
td.appendChild(div);
2020-02-14 12:52:14 -06:00
return td;
};
const submitForm = () => {
const form = document.forms.namedItem("new-url-form");
const longUrl = form.elements["longUrl"];
const shortUrl = form.elements["shortUrl"];
const url = `/api/new`;
2020-02-14 12:52:14 -06:00
fetch(url, {
method: "POST",
body: `${longUrl.value};${shortUrl.value}`
2020-02-14 12:52:14 -06:00
})
2022-11-10 20:17:39 -06:00
.then(res => {
2022-11-08 18:23:41 -06:00
if (!res.ok) {
showAlert("Short URL is not valid or it's already in use!", "red");
return "error";
2022-11-08 18:23:41 -06:00
}
else {
2022-11-10 18:11:57 -06:00
return res.text();
2022-11-08 18:23:41 -06:00
}
2022-11-10 20:17:39 -06:00
}).then(text => {
if (text != "error") {
copyShortUrl(text);
longUrl.value = "";
shortUrl.value = "";
refreshData();
}
});
2020-02-14 12:52:14 -06:00
};
(async () => {
await refreshData();
const form = document.forms.namedItem("new-url-form");
form.onsubmit = e => {
e.preventDefault();
submitForm();
}
})();