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