Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion chat.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ body {
}

input[type="password"], input[type="text"] {
width: calc(100% - 20px);
width: 100%;
box-sizing: border-box;
padding: 10px;
margin-bottom: 10px;
}
Expand Down
8 changes: 4 additions & 4 deletions chat.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="th">
<head>
<meta charset="UTF-8">
<title>Private Chat Room</title>
Expand All @@ -8,16 +8,16 @@
<body>
<div id="password-screen">
<h2>ใส่รหัสผ่านเพื่อเข้าสู่ห้องแชท</h2>
<input type="password" id="password-input" placeholder="รหัสผ่าน">
<button onclick="checkPassword()">เข้าสู่ระบบ</button>
<input type="password" id="password-input" placeholder="รหัสผ่าน" autocomplete="off">
<button id="login-button">เข้าสู่ระบบ</button>
<p id="error-msg"></p>
</div>

<div id="chat-screen" style="display:none;">
<h2>ห้องแชทส่วนตัว</h2>
<div id="chat-box"></div>
<input type="text" id="user-input" placeholder="พิมพ์ข้อความ...">
<button onclick="sendMessage()">ส่ง</button>
<button id="send-button">ส่ง</button>
</div>

<script src="chat.js"></script>
Expand Down
40 changes: 37 additions & 3 deletions chat.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
const PASSWORD = "QuietRoom#2025";
const PASSWORD_HASH = "cf77816f045ebf2ceb4cf57367a86c217ad3293fa17c13d3e6d52886b815cb12";

function checkPassword() {
async function sha256Hex(message) {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
return hashHex;
}

async function checkPassword() {
const input = document.getElementById("password-input").value;
const errorMsg = document.getElementById("error-msg");
errorMsg.textContent = "";

if (input === PASSWORD) {
const inputHash = await sha256Hex(input);
if (inputHash === PASSWORD_HASH) {
document.getElementById("password-screen").style.display = "none";
document.getElementById("chat-screen").style.display = "block";
} else {
Expand All @@ -25,3 +36,26 @@ function sendMessage() {
inputField.value = "";
chatBox.scrollTop = chatBox.scrollHeight;
}

// Wire up events after DOM is ready
window.addEventListener("DOMContentLoaded", () => {
const loginButton = document.getElementById("login-button");
const passwordInput = document.getElementById("password-input");
const sendButton = document.getElementById("send-button");
const userInput = document.getElementById("user-input");

if (loginButton) loginButton.addEventListener("click", () => { checkPassword(); });
if (passwordInput) passwordInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
checkPassword();
}
});

if (sendButton) sendButton.addEventListener("click", () => { sendMessage(); });
if (userInput) userInput.addEventListener("keydown", (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
});