-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
86 lines (69 loc) · 2.35 KB
/
Copy pathscript2.js
File metadata and controls
86 lines (69 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 🔥 Firebase Config
const firebaseConfig = {
apiKey: "AIzaSyDZMYWXxl-W6tKvGF7aRrq8hcpg4TdE9I",
authDomain: "chatwleo.firebaseapp.com",
databaseURL: "https://chatwleo-default-rtdb.firebaseio.com",
projectId: "chatwleo",
storageBucket: "chatwleo.firebasestorage.app",
messagingSenderId: "616988584387",
appId: "1:616988584387:web:81b829019252d78cdd7576"
};
firebase.initializeApp(firebaseConfig);
const db = firebase.database();
// 👤 Username
let username = localStorage.getItem("chatww_username");
if (!username) {
username = "Guest_" + Math.floor(Math.random() * 10000);
localStorage.setItem("chatww_username", username);
}
// 📍 Detect page
const isRoomPage = window.location.pathname.includes("roomchat.html");
// =======================
// 🏠 LOBBY PAGE
// =======================
if (!isRoomPage) {
const roomList = document.getElementById("roomList");
// Create Room
window.createRoom = () => {
const name = roomName.value.trim();
const pass = roomPass.value.trim();
if (!name || !pass) return alert("Fill all");
const id = name.toLowerCase().replace(/\s/g, "_");
db.ref("rooms/" + id).once("value").then(snap => {
if (snap.exists()) return alert("Room exists");
db.ref("rooms/" + id).set({
name,
password: CryptoJS.SHA256(pass).toString()
});
alert("Created!");
});
};
// Join Room
window.joinRoom = () => {
const name = joinRoomName.value.trim();
const pass = joinRoomPass.value.trim();
const id = name.toLowerCase().replace(/\s/g, "_");
db.ref("rooms/" + id).once("value").then(snap => {
const room = snap.val();
if (!room) return alert("Not found");
if (CryptoJS.SHA256(pass).toString() !== room.password)
return alert("Wrong password");
localStorage.setItem("chatww_room", id);
window.location.href = "roomchat.html";
});
};
// Load Rooms
db.ref("rooms").on("child_added", snap => {
const div = document.createElement("div");
div.textContent = snap.val().name;
div.onclick = () => {
const pass = prompt("Password:");
if (!pass) return;
if (CryptoJS.SHA256(pass).toString() !== snap.val().password)
return alert("Wrong password");
localStorage.setItem("chatww_room", snap.key);
window.location.href = "roomchat.html";
};
roomList.appendChild(div);
});
}