improvement
This commit is contained in:
+52
-55
@@ -1,23 +1,22 @@
|
||||
const tbody = document.getElementById("content-display");
|
||||
|
||||
async function init() {
|
||||
try {
|
||||
const response = await fetch("/api/url");
|
||||
try {
|
||||
const response = await fetch("/api/url");
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const urls = await response.json();
|
||||
const urls = await response.json();
|
||||
|
||||
urls.forEach((url) => {
|
||||
urls.forEach((url) => {
|
||||
const id = url.replace(/[^a-zA-Z0-9]/g, "_");
|
||||
|
||||
const id = url.replace(/[^a-zA-Z0-9]/g, "_");
|
||||
const row = document.createElement("tr");
|
||||
row.id = id;
|
||||
|
||||
const row = document.createElement("tr");
|
||||
row.id = id;
|
||||
|
||||
row.innerHTML = `
|
||||
row.innerHTML = `
|
||||
<td>${url}</td>
|
||||
<td class="status">Waiting...</td>
|
||||
<td class="statusCode">-</td>
|
||||
@@ -25,12 +24,11 @@ async function init() {
|
||||
<td class="error">-</td>
|
||||
`;
|
||||
|
||||
tbody.appendChild(row);
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
tbody.appendChild(row);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", init);
|
||||
@@ -43,60 +41,59 @@ console.log(socket);
|
||||
const websocket_status = document.getElementById("websocket_status");
|
||||
|
||||
socket.onopen = () => {
|
||||
websocket_status.textContent = "Connected";
|
||||
websocket_status.textContent = "Connected";
|
||||
};
|
||||
let counter = 0
|
||||
let counter = 0;
|
||||
socket.onmessage = (event) => {
|
||||
// console.log(counter)
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === "checking") {
|
||||
counter = counter + 1
|
||||
console.log(counter)
|
||||
document.getElementById("status").textContent = "Refreshed " + counter + " Times";
|
||||
return;
|
||||
}
|
||||
// console.log(counter)
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === "checking") {
|
||||
counter = counter + 1;
|
||||
document.getElementById("status").textContent =
|
||||
"Refreshed " + counter + " Times";
|
||||
return;
|
||||
}
|
||||
|
||||
const id = data.url.replace(/[^a-zA-Z0-9]/g, "_");
|
||||
const row = document.getElementById(id);
|
||||
if (!row) return;
|
||||
|
||||
const id = data.url.replace(/[^a-zA-Z0-9]/g, "_");
|
||||
const row = document.getElementById(id);
|
||||
if (!row) return;
|
||||
|
||||
row.querySelector(".status").textContent = data.status;
|
||||
row.querySelector(".statusCode").textContent = data.statusCode;
|
||||
row.querySelector(".responseTime").textContent = data.responseTime != null ? `${data.responseTime} ms` : "-";
|
||||
row.querySelector(".error").textContent = data.error || "-";
|
||||
row.querySelector(".status").textContent = data.status;
|
||||
row.querySelector(".statusCode").textContent = data.statusCode;
|
||||
row.querySelector(".responseTime").textContent =
|
||||
data.responseTime != null ? `${data.responseTime} ms` : "-";
|
||||
row.querySelector(".error").textContent = data.error || "-";
|
||||
};
|
||||
|
||||
|
||||
socket.onclose = () => {
|
||||
websocket_status.textContent = "Disconnected";
|
||||
websocket_status.textContent = "Disconnected";
|
||||
};
|
||||
|
||||
document.getElementById("addBtn").addEventListener("click", async () => {
|
||||
const url = document.getElementById("urlInput").value.trim();
|
||||
if (!url) return;
|
||||
const url = document.getElementById("urlInput").value.trim();
|
||||
if (!url) return;
|
||||
|
||||
const response = await fetch("/api/url/register", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ url })
|
||||
});
|
||||
const response = await fetch("/api/url/register", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ url }),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
document.getElementById("urlInput").value = "";
|
||||
if (response.ok) {
|
||||
document.getElementById("urlInput").value = "";
|
||||
|
||||
const id = url.replace(/[^a-zA-Z0-9]/g, "_");
|
||||
if (!document.getElementById(id)) {
|
||||
const row = document.createElement("tr");
|
||||
row.id = id;
|
||||
row.innerHTML = `
|
||||
const id = url.replace(/[^a-zA-Z0-9]/g, "_");
|
||||
if (!document.getElementById(id)) {
|
||||
const row = document.createElement("tr");
|
||||
row.id = id;
|
||||
row.innerHTML = `
|
||||
<td>${url}</td>
|
||||
<td class="status">Waiting...</td>
|
||||
<td class="statusCode">-</td>
|
||||
<td class="responseTime">-</td>
|
||||
<td class="error">-</td>
|
||||
`;
|
||||
tbody.appendChild(row);
|
||||
}
|
||||
tbody.appendChild(row);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user