|
1 | 1 | const messagesList = document.getElementById("messages-list"); |
2 | 2 | const form = document.getElementById("message-form"); |
3 | | -const input = document.getElementById("message-input"); |
| 3 | +const messageInput = document.getElementById("message-input"); |
4 | 4 | const usernameInput = document.getElementById("username-input"); |
5 | 5 |
|
6 | 6 | const BACKEND_URL = |
7 | 7 | "https://xwc7hmpmk5kvt9ktptcvuuke.hosting.codeyourfuture.io"; |
8 | 8 |
|
| 9 | +//Utility: sanitize input to prevent HTML injection |
| 10 | +function sanitizeInput(input) { |
| 11 | + const div = document.createElement("div"); |
| 12 | + div.textContent = input; |
| 13 | + return div.innerHTML; |
| 14 | +} |
| 15 | + |
9 | 16 | // Fetch and display messages |
10 | 17 | async function loadMessages() { |
11 | | - const response = await fetch(`${BACKEND_URL}/messages`); |
12 | | - const messages = await response.json(); |
13 | | - messagesList.innerHTML = ""; |
14 | | - messages.forEach((msg) => { |
15 | | - const li = document.createElement("li"); |
16 | | - li.innerHTML = ` |
17 | | - <div class="message-header"> |
18 | | - <span class="username"> <strong>${msg.username}</strong></span> |
19 | | - <span class="timestamp">${msg.timeStamp}</span> |
20 | | - </div> |
21 | | - <div class="message-text">${msg.text}</div>`; |
22 | | - messagesList.appendChild(li); |
23 | | - }); |
| 18 | + try { |
| 19 | + const response = await fetch(`${BACKEND_URL}/messages`); |
| 20 | + if (!response.ok) { |
| 21 | + throw new Error("Failed to fetch messages"); |
| 22 | + } |
| 23 | + const messages = await response.json(); |
| 24 | + messagesList.innerHTML = ""; |
| 25 | + |
| 26 | + messages.forEach((msg) => { |
| 27 | + const li = document.createElement("li"); |
| 28 | + const header = document.createElement("div"); |
| 29 | + header.className = "message-header"; |
| 30 | + |
| 31 | + const usernameSpan = document.createElement("span"); |
| 32 | + usernameSpan.className = "username"; |
| 33 | + usernameSpan.textContent = msg.username; |
| 34 | + |
| 35 | + const timestamp = document.createElement("span"); |
| 36 | + timestamp.className = "timestamp"; |
| 37 | + timestamp.textContent = new Date(msg.timeStamp).toLocaleString(); |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + header.appendChild(usernameSpan); |
| 42 | + header.appendChild(timestamp); |
| 43 | + |
| 44 | + const textDiv = document.createElement("div"); |
| 45 | + textDiv.className = "message-text"; |
| 46 | + textDiv.textContent = msg.text; |
| 47 | + |
| 48 | + li.appendChild(header); |
| 49 | + li.appendChild(textDiv); |
| 50 | + messagesList.appendChild(li); |
| 51 | + }); |
| 52 | + // Scroll to the bottom to show the latest message |
| 53 | + messagesList.scrollTop = messagesList.scrollHeight; |
| 54 | + } catch (error) { |
| 55 | + console.error("Error loading messages:", error); |
| 56 | + messagesList.innerHTML = |
| 57 | + "<li>Error loading messages. Please try again later.</li>"; |
| 58 | + } |
24 | 59 | } |
25 | 60 |
|
26 | 61 | // Handle form submission |
27 | 62 | form.addEventListener("submit", async (e) => { |
28 | 63 | e.preventDefault(); |
29 | 64 |
|
30 | | - const newMessage = { |
31 | | - username: usernameInput.value, |
32 | | - text: input.value, |
33 | | - timeStamp: new Date().toLocaleTimeString("en-US", { |
34 | | - hour: "numeric", |
35 | | - minute: "2-digit", |
36 | | - hour12: true, |
37 | | - }), |
38 | | - }; |
39 | | - |
40 | | - await fetch(`${BACKEND_URL}/messages`, { |
41 | | - method: "POST", |
42 | | - headers: { |
43 | | - "Content-Type": "application/json", |
44 | | - }, |
45 | | - body: JSON.stringify(newMessage), |
46 | | - }); |
47 | | - |
48 | | - input.value = ""; |
49 | | - usernameInput.value = ""; |
50 | | - loadMessages(); |
| 65 | + const username = usernameInput.value.trim(); |
| 66 | + const text = messageInput.value.trim(); |
| 67 | + |
| 68 | + if (!username || !text) { |
| 69 | + alert("Please enter both a username and a message."); |
| 70 | + return; |
| 71 | + } |
| 72 | + const newMessage = { username, text }; |
| 73 | + |
| 74 | + try { |
| 75 | + const response = await fetch(`${BACKEND_URL}/messages`, { |
| 76 | + method: "POST", |
| 77 | + headers: { |
| 78 | + "Content-Type": "application/json", |
| 79 | + }, |
| 80 | + body: JSON.stringify(newMessage), |
| 81 | + }); |
| 82 | + if (!response.ok) { |
| 83 | + throw new Error("Failed to send message"); |
| 84 | + } |
| 85 | + messageInput.value = ""; |
| 86 | + usernameInput.value = ""; |
| 87 | + loadMessages(); |
| 88 | + } catch (error) { |
| 89 | + console.error("Error sending message:", error); |
| 90 | + alert("Failed to send message. Please try again."); |
| 91 | + } |
51 | 92 | }); |
| 93 | +loadMessages(); |
52 | 94 |
|
53 | 95 | // Refresh messages every 2 seconds |
54 | 96 | setInterval(loadMessages, 2000); |
0 commit comments