mirror of
https://github.com/SinTan1729/chhoto-url
synced 2024-12-26 23:58:35 -06:00
chg: Cleaned up the js code
This commit is contained in:
parent
604e95aa9c
commit
7ad874a1ff
1 changed files with 45 additions and 37 deletions
|
@ -6,35 +6,36 @@ const prepSubdir = (link) => {
|
||||||
return (subdir + link).replace('//', '/');
|
return (subdir + link).replace('//', '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
const getSiteUrl = async () => await fetch(prepSubdir("/api/siteurl"))
|
const getSiteUrl = async () => {
|
||||||
.then(res => res.text())
|
let url = await fetch(prepSubdir("/api/siteurl"))
|
||||||
.then(text => {
|
.then(res => res.text());
|
||||||
if (text == "unset") {
|
if (url == "unset") {
|
||||||
return window.location.host.replace(/\/$/, '');
|
return window.location.host.replace(/\/$/, '');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return text.replace(/\/$/, '').replace(/^"/, '').replace(/"$/, '');
|
return text.replace(/\/$/, '').replace(/^"/, '').replace(/"$/, '');
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
const getVersion = async () => await fetch(prepSubdir("/api/version"))
|
const getVersion = async () => {
|
||||||
.then(res => res.text())
|
let ver = await fetch(prepSubdir("/api/version"))
|
||||||
.then(text => {
|
.then(res => res.text());
|
||||||
return text;
|
return ver;
|
||||||
});
|
}
|
||||||
|
|
||||||
const refreshData = async () => {
|
const refreshData = async () => {
|
||||||
let reply = await fetch(prepSubdir("/api/all")).then(res => res.text());
|
let res = await fetch(prepSubdir("/api/all"));
|
||||||
if (reply == "Not logged in!") {
|
if (!res.ok) {
|
||||||
console.log("Not logged in!");
|
let errorMsg = await res.text();
|
||||||
document.getElementById("container").style.filter = "blur(2px)"
|
console.log(errorMsg);
|
||||||
|
document.getElementById("container").style.filter = "blur(2px)";
|
||||||
document.getElementById("login-dialog").showModal();
|
document.getElementById("login-dialog").showModal();
|
||||||
document.getElementById("password").focus();
|
document.getElementById("password").focus();
|
||||||
} else {
|
} else {
|
||||||
data = JSON.parse(reply)
|
let data = await res.json();
|
||||||
displayData(data);
|
displayData(data);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const displayData = async (data) => {
|
const displayData = async (data) => {
|
||||||
let version = await getVersion();
|
let version = await getVersion();
|
||||||
|
@ -64,7 +65,7 @@ const displayData = async (data) => {
|
||||||
table.innerHTML = ''; // Clear
|
table.innerHTML = ''; // Clear
|
||||||
data.forEach(tr => table.appendChild(TR(tr, site)));
|
data.forEach(tr => table.appendChild(TR(tr, site)));
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const showAlert = async (text, col) => {
|
const showAlert = async (text, col) => {
|
||||||
document.getElementById("alert-box")?.remove();
|
document.getElementById("alert-box")?.remove();
|
||||||
|
@ -74,7 +75,7 @@ const showAlert = async (text, col) => {
|
||||||
alertBox.style.color = col;
|
alertBox.style.color = col;
|
||||||
alertBox.innerHTML = text;
|
alertBox.innerHTML = text;
|
||||||
controls.appendChild(alertBox);
|
controls.appendChild(alertBox);
|
||||||
};
|
}
|
||||||
|
|
||||||
const TR = (row, site) => {
|
const TR = (row, site) => {
|
||||||
const tr = document.createElement("tr");
|
const tr = document.createElement("tr");
|
||||||
|
@ -86,7 +87,7 @@ const TR = (row, site) => {
|
||||||
else {
|
else {
|
||||||
shortTD = TD(A_SHORT_INSECURE(row["shortlink"], site), "Short URL");
|
shortTD = TD(A_SHORT_INSECURE(row["shortlink"], site), "Short URL");
|
||||||
}
|
}
|
||||||
hitsTD = TD(row["hits"]);
|
let hitsTD = TD(row["hits"]);
|
||||||
hitsTD.setAttribute("label", "Hits");
|
hitsTD.setAttribute("label", "Hits");
|
||||||
hitsTD.setAttribute("name", "hitsColumn");
|
hitsTD.setAttribute("name", "hitsColumn");
|
||||||
const btn = deleteButton(row["shortlink"]);
|
const btn = deleteButton(row["shortlink"]);
|
||||||
|
@ -97,7 +98,7 @@ const TR = (row, site) => {
|
||||||
tr.appendChild(btn);
|
tr.appendChild(btn);
|
||||||
|
|
||||||
return tr;
|
return tr;
|
||||||
};
|
}
|
||||||
|
|
||||||
const copyShortUrl = async (link) => {
|
const copyShortUrl = async (link) => {
|
||||||
const site = await getSiteUrl();
|
const site = await getSiteUrl();
|
||||||
|
@ -109,7 +110,7 @@ const copyShortUrl = async (link) => {
|
||||||
showAlert("Could not copy short URL to clipboard, please do it manually.", "red");
|
showAlert("Could not copy short URL to clipboard, please do it manually.", "red");
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
const addProtocol = (input) => {
|
const addProtocol = (input) => {
|
||||||
var url = input.value.trim();
|
var url = input.value.trim();
|
||||||
|
@ -117,7 +118,7 @@ const addProtocol = (input) => {
|
||||||
url = "https://" + url;
|
url = "https://" + url;
|
||||||
}
|
}
|
||||||
input.value = url;
|
input.value = url;
|
||||||
return input
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
const A_LONG = (s) => `<a href='${s}'>${s}</a>`;
|
const A_LONG = (s) => `<a href='${s}'>${s}</a>`;
|
||||||
|
@ -138,7 +139,14 @@ const deleteButton = (shortUrl) => {
|
||||||
showAlert(" ", "black");
|
showAlert(" ", "black");
|
||||||
fetch(prepSubdir(`/api/del/${shortUrl}`), {
|
fetch(prepSubdir(`/api/del/${shortUrl}`), {
|
||||||
method: "DELETE"
|
method: "DELETE"
|
||||||
}).then(_ => refreshData());
|
}).then(res => {
|
||||||
|
if (res.ok) {
|
||||||
|
console.log("Deleted " + shortUrl);
|
||||||
|
} else {
|
||||||
|
console.log("Unable to delete " + shortUrl);
|
||||||
|
}
|
||||||
|
refreshData();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
td.setAttribute("name", "deleteBtn");
|
td.setAttribute("name", "deleteBtn");
|
||||||
|
@ -146,7 +154,7 @@ const deleteButton = (shortUrl) => {
|
||||||
div.appendChild(btn);
|
div.appendChild(btn);
|
||||||
td.appendChild(div);
|
td.appendChild(div);
|
||||||
return td;
|
return td;
|
||||||
};
|
}
|
||||||
|
|
||||||
const TD = (s, u) => {
|
const TD = (s, u) => {
|
||||||
const td = document.createElement("td");
|
const td = document.createElement("td");
|
||||||
|
@ -155,7 +163,7 @@ const TD = (s, u) => {
|
||||||
td.appendChild(div);
|
td.appendChild(div);
|
||||||
td.setAttribute("label", u);
|
td.setAttribute("label", u);
|
||||||
return td;
|
return td;
|
||||||
};
|
}
|
||||||
|
|
||||||
const submitForm = () => {
|
const submitForm = () => {
|
||||||
const form = document.forms.namedItem("new-url-form");
|
const form = document.forms.namedItem("new-url-form");
|
||||||
|
@ -175,7 +183,7 @@ const submitForm = () => {
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
showAlert("Short URL is not valid or it's already in use!", "red");
|
showAlert(res.text(), "red");
|
||||||
return "error";
|
return "error";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -188,8 +196,8 @@ const submitForm = () => {
|
||||||
shortUrl.value = "";
|
shortUrl.value = "";
|
||||||
refreshData();
|
refreshData();
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
|
|
||||||
const submitLogin = () => {
|
const submitLogin = () => {
|
||||||
const password = document.getElementById("password");
|
const password = document.getElementById("password");
|
||||||
|
@ -224,4 +232,4 @@ const submitLogin = () => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
submitLogin();
|
submitLogin();
|
||||||
}
|
}
|
||||||
})();
|
})()
|
||||||
|
|
Loading…
Reference in a new issue