From 88ddb1ebba80e45fe9fd22423f7457ba10fa6c04 Mon Sep 17 00:00:00 2001 From: 20syldev Date: Sun, 22 Dec 2024 15:39:16 +0100 Subject: [PATCH] MAJOR: js: Add app js --- js/app.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 js/app.js diff --git a/js/app.js b/js/app.js new file mode 100644 index 0000000..f7ab23e --- /dev/null +++ b/js/app.js @@ -0,0 +1,50 @@ +let logs = []; +let API = prompt('Enter API url :'); + +/* Fetch logs from API & update logs */ +async function fetchLogs() { + try { + const data = await (await fetch(API)).json(); + const newLogs = data.filter(log => + !logs.some(old => + old.timestamp === log.timestamp && + old.method === log.method && + old.url === log.url + ) + ); + logs = data; + updateTimeline(newLogs); + } catch (error) { + console.error('Error fetching logs:', error); + } +} + +/* Update the timeline with new logs */ +function updateTimeline(logs) { + const timeline = document.getElementById('timeline'); + logs.forEach(log => { + const logElement = document.createElement('div'); + let method, status = ''; + + if (log.method === 'GET') method = 'method-get'; + else if (log.method === 'POST') method = 'method-post'; + + if (log.status === 200) status = 'status-200'; + else if (log.status === 404 || log.status === 500) status = 'status-error'; + + logElement.classList.add('timeline-item'); + logElement.innerHTML = ` +
+
${log.status}
+
${log.method}
+
${log.method} ${log.url}
+
${new Date(log.timestamp).toLocaleString()}  -  ${log.duration} ${log.platform ? ' -  ' + log.platform : ''}
+
+ `; + timeline.prepend(logElement); + }); +} + +/* Fetch logs on page load & then every 2 seconds */ +setInterval(fetchLogs, 2000); +fetchLogs(); \ No newline at end of file