1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-28 08:28:36 -06:00
chhoto-url/resources/static/script.js

239 lines
6.9 KiB
JavaScript
Raw Normal View History

2024-04-03 20:40:26 -05:00
// SPDX-FileCopyrightText: 2023 Sayantan Santra <sayantan.santra689@gmail.com>
// SPDX-License-Identifier: MIT
const prepSubdir = (link) => {
let thisPage = new URL(window.location.href);
let subdir = thisPage.pathname;
2024-04-01 02:07:43 -05:00
let out = (subdir + link).replace('//', '/');
console.log(out);
2024-04-01 02:07:43 -05:00
return (subdir + link).replace('//', '/');
}
2024-04-01 02:07:43 -05:00
const getSiteUrl = async () => {
let url = await fetch(prepSubdir("/api/siteurl"))
.then(res => res.text());
if (url == "unset") {
return window.location.host.replace(/\/$/, '');
}
else {
2024-04-02 21:02:11 -05:00
return url.replace(/\/$/, '').replace(/^"/, '').replace(/"$/, '');
2024-04-01 02:07:43 -05:00
}
}
2022-11-10 20:17:39 -06:00
2024-04-01 02:07:43 -05:00
const getVersion = async () => {
let ver = await fetch(prepSubdir("/api/version"))
.then(res => res.text());
return ver;
}
2024-02-10 19:41:50 -06:00
2023-04-11 19:17:04 -05:00
const refreshData = async () => {
2024-04-01 02:07:43 -05:00
let res = await fetch(prepSubdir("/api/all"));
if (!res.ok) {
let errorMsg = await res.text();
console.log(errorMsg);
document.getElementById("container").style.filter = "blur(2px)";
2023-04-11 19:17:04 -05:00
document.getElementById("login-dialog").showModal();
document.getElementById("password").focus();
2023-04-08 02:52:16 -05:00
} else {
2024-04-01 02:07:43 -05:00
let data = await res.json();
2023-04-11 19:17:04 -05:00
displayData(data);
2023-04-08 02:52:16 -05:00
}
2024-04-01 02:07:43 -05:00
}
2020-02-14 12:52:14 -06:00
2022-11-10 20:17:39 -06:00
const displayData = async (data) => {
2024-02-10 19:41:50 -06:00
let version = await getVersion();
link = document.getElementById("version-number")
link.innerText = "v" + version;
link.href = "https://github.com/SinTan1729/chhoto-url/releases/tag/" + version;
link.hidden = false;
2022-11-10 23:19:42 -06:00
let site = await getSiteUrl();
2024-02-10 19:41:50 -06:00
2022-11-09 18:55:50 -06:00
table_box = document.querySelector(".pure-table");
2023-06-03 18:49:59 -05:00
loading_text = document.getElementsByName("loading-text")[0];
2022-11-09 18:55:50 -06:00
if (data.length == 0) {
table_box.style.visibility = "hidden";
2023-06-03 18:49:59 -05:00
loading_text.style.display = "block";
loading_text.innerHTML = "No active links.";
2022-11-09 18:55:50 -06:00
}
else {
2023-06-03 18:49:59 -05:00
loading_text.style.display = "none";
2022-11-09 18:55:50 -06:00
const table = document.querySelector("#url-table");
if (!window.isSecureContext) {
const shortUrlHeader = document.getElementById("short-url-header");
shortUrlHeader.innerHTML = "Short URL<br>(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
}
2024-04-01 02:07:43 -05:00
}
2020-02-14 12:52:14 -06:00
const showAlert = async (text, col) => {
document.getElementById("alert-box")?.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.id = "alert-box";
alertBox.style.color = col;
2022-11-10 23:19:42 -06:00
alertBox.innerHTML = text;
2022-11-10 18:11:57 -06:00
controls.appendChild(alertBox);
2024-04-01 02:07:43 -05:00
}
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");
2024-03-31 15:38:59 -05:00
const longTD = TD(A_LONG(row["longlink"]), "Long URL");
var shortTD = null;
if (window.isSecureContext) {
2024-03-31 15:38:59 -05:00
shortTD = TD(A_SHORT(row["shortlink"], site), "Short URL");
}
else {
2024-03-31 15:38:59 -05:00
shortTD = TD(A_SHORT_INSECURE(row["shortlink"], site), "Short URL");
}
2024-04-01 02:07:43 -05:00
let hitsTD = TD(row["hits"]);
2023-04-14 17:18:18 -05:00
hitsTD.setAttribute("label", "Hits");
hitsTD.setAttribute("name", "hitsColumn");
2024-03-31 15:38:59 -05:00
const btn = deleteButton(row["shortlink"]);
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;
2024-04-01 02:07:43 -05:00
}
2020-02-14 12:52:14 -06:00
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");
}
2024-04-01 02:07:43 -05:00
}
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;
2024-04-01 02:07:43 -05:00
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");
2024-03-18 19:27:58 -05:00
const div = document.createElement("div");
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("alert-box")?.remove();
showAlert("&nbsp;", "black");
fetch(prepSubdir(`/api/del/${shortUrl}`), {
2022-11-09 17:58:15 -06:00
method: "DELETE"
2024-04-01 02:07:43 -05:00
}).then(res => {
if (res.ok) {
console.log("Deleted " + shortUrl);
} else {
console.log("Unable to delete " + shortUrl);
}
refreshData();
});
2022-11-09 17:58:15 -06:00
}
2020-02-16 09:52:54 -06:00
};
2022-11-09 18:55:50 -06:00
td.setAttribute("name", "deleteBtn");
2023-04-14 17:18:18 -05:00
td.setAttribute("label", "Delete");
2024-03-18 19:27:58 -05:00
div.appendChild(btn);
td.appendChild(div);
2022-11-09 18:55:50 -06:00
return td;
2024-04-01 02:07:43 -05:00
}
2020-02-16 09:52:54 -06:00
2023-04-14 17:18:18 -05:00
const TD = (s, u) => {
2020-02-14 12:52:14 -06:00
const td = document.createElement("td");
2022-11-03 15:19:22 -05:00
const div = document.createElement("div");
div.innerHTML = s;
td.appendChild(div);
2023-04-14 17:18:18 -05:00
td.setAttribute("label", u);
2020-02-14 12:52:14 -06:00
return td;
2024-04-01 02:07:43 -05:00
}
2020-02-14 12:52:14 -06:00
const submitForm = () => {
const form = document.forms.namedItem("new-url-form");
2024-04-01 02:07:43 -05:00
const data = {
"longlink": form.elements["longUrl"].value,
"shortlink": form.elements["shortUrl"].value,
};
2020-02-14 12:52:14 -06:00
const url = prepSubdir("/api/new");
2024-04-02 21:26:18 -05:00
let ok = false;
2020-02-14 12:52:14 -06:00
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
2020-02-14 12:52:14 -06:00
})
2022-11-10 20:17:39 -06:00
.then(res => {
2024-04-02 21:26:18 -05:00
ok = res.ok;
return res.text();
})
.then(text => {
if (!ok) {
showAlert(text, "red");
2022-11-08 18:23:41 -06:00
}
else {
copyShortUrl(text);
longUrl.value = "";
shortUrl.value = "";
refreshData();
}
2024-04-01 02:07:43 -05:00
})
}
2020-02-14 12:52:14 -06:00
2023-04-11 19:17:04 -05:00
const submitLogin = () => {
const password = document.getElementById("password");
fetch(prepSubdir("/api/login"), {
2023-04-11 19:17:04 -05:00
method: "POST",
body: password.value
}).then(res => {
if (res.ok) {
document.getElementById("container").style.filter = "blur(0px)"
document.getElementById("login-dialog").remove();
refreshData();
} else {
const wrongPassBox = document.getElementById("wrong-pass");
wrongPassBox.innerHTML = "Wrong password!";
wrongPassBox.style.color = "red";
2023-04-11 19:17:04 -05:00
password.focus();
}
})
}
2020-02-14 12:52:14 -06:00
(async () => {
await refreshData();
2023-04-11 19:17:04 -05:00
2020-02-14 12:52:14 -06:00
const form = document.forms.namedItem("new-url-form");
form.onsubmit = e => {
e.preventDefault();
submitForm();
}
2023-04-11 19:17:04 -05:00
const login_form = document.forms.namedItem("login-form");
login_form.onsubmit = e => {
e.preventDefault();
submitLogin();
}
2024-04-01 02:07:43 -05:00
})()