Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
- <title>Anonymous Chat</title>
- <style>
- body {
- background-color: white;
- font-family: Arial, sans-serif;
- margin: 0;
- padding: 0;
- }
- .chat-container {
- max-width: 600px;
- margin: 40px auto;
- padding: 20px;
- border: 2px solid #ddd;
- border-radius: 8px;
- height: 80vh;
- display: flex;
- flex-direction: column;
- background-color: #fff;
- }
- h2 {
- text-align: center;
- margin-bottom: 10px;
- color: #333;
- }
- .messages {
- flex: 1;
- overflow-y: auto;
- padding: 10px;
- border: 1px solid #ccc;
- margin-bottom: 10px;
- background: #fafafa;
- border-radius: 5px;
- }
- .message {
- margin-bottom: 10px;
- padding: 8px 12px;
- background-color: #e6e6e6;
- border-radius: 6px;
- }
- .input-container {
- display: flex;
- gap: 10px;
- }
- input[type="text"] {
- flex: 1;
- padding: 10px;
- font-size: 16px;
- border: 1px solid #aaa;
- border-radius: 4px;
- }
- button {
- padding: 10px 15px;
- font-size: 16px;
- background-color: #333;
- color: white;
- border: none;
- border-radius: 4px;
- cursor: pointer;
- }
- </style>
- </head>
- <body>
- <div class="chat-container">
- <h2>Anonymous Chat</h2>
- <div class="messages" id="messages"></div>
- <div class="input-container">
- <input type="text" id="chatInput" placeholder="Type your message..." />
- <button onclick="sendMessage()">Send</button>
- </div>
- </div>
- <!-- Firebase SDKs -->
- <script src="https://www.gstatic.com/firebasejs/9.0.0/firebase-app-compat.js"></script>
- <script src="https://www.gstatic.com/firebasejs/9.0.0/firebase-database-compat.js"></script>
- <script>
- // Replace with your Firebase project config
- const firebaseConfig = {
- apiKey: "YOUR_API_KEY",
- authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
- databaseURL: "https://YOUR_PROJECT_ID.firebaseio.com",
- projectId: "YOUR_PROJECT_ID",
- storageBucket: "YOUR_PROJECT_ID.appspot.com",
- messagingSenderId: "YOUR_SENDER_ID",
- appId: "YOUR_APP_ID"
- };
- firebase.initializeApp(firebaseConfig);
- const db = firebase.database();
- const messagesRef = db.ref("messages");
- function sendMessage() {
- const input = document.getElementById("chatInput");
- const text = input.value.trim();
- if (text) {
- messagesRef.push({ text: text, timestamp: Date.now() });
- input.value = "";
- }
- }
- messagesRef.on("child_added", (snapshot) => {
- const msg = snapshot.val();
- const msgDiv = document.createElement("div");
- msgDiv.classList.add("message");
- msgDiv.textContent = msg.text;
- document.getElementById("messages").appendChild(msgDiv);
- const container = document.getElementById("messages");
- container.scrollTop = container.scrollHeight;
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement