App Js
App Js
1/firebase-
app.js";
import {
getFirestore, collection, addDoc, serverTimestamp, onSnapshot,
getStorage, ref, uploadBytes, getDownloadURL
} from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";
const firebaseConfig = {
apiKey: "AIzaSyAFr0X5iAx4ESYKpfZmkcYIR6W4J7Gjdlw",
authDomain: "bogdanio-chat.firebaseapp.com",
projectId: "bogdanio-chat",
storageBucket: "bogdanio-chat.appspot.com", // Проверьте название в Firebase
Console!
messagingSenderId: "672006362714",
appId: "1:672006362714:web:60bee11c290f7029ba2a63"
};
// Элементы DOM
const msgForm = document.getElementById("msgForm");
const msgInput = document.getElementById("msgInput");
const fileInput = document.getElementById("fileInput");
const messages = document.getElementById("messages");
const notifySound = document.getElementById("notifySound");
// Отправка сообщений
msgForm.addEventListener("submit", async (e) => {
e.preventDefault();
const text = msgInput.value.trim();
const file = fileInput.files[0];
try {
let mediaUrl = "";
if (file) {
const fileRef = ref(storage, `uploads/${Date.now()}_${file.name}`);
await uploadBytes(fileRef, file);
mediaUrl = await getDownloadURL(fileRef);
}
msgInput.value = "";
fileInput.value = "";
msgInput.style.height = "auto";
} catch (error) {
console.error("Ошибка отправки:", error);
alert("Ошибка: " + error.message);
}
});
if (msg.mediaUrl) {
const isImage = msg.mediaUrl.match(/\.(jpe?g|png|gif|webp)$/i);
const isVideo = msg.mediaUrl.match(/\.(mp4|webm|ogg)$/i);
if (isImage) {
msgEl.innerHTML += `<img src="${msg.mediaUrl}" alt="Image">`;
} else if (isVideo) {
msgEl.innerHTML += `<video controls src="${msg.mediaUrl}"></video>`;
}
}
messages.appendChild(msgEl);
});
messages.scrollTop = messages.scrollHeight;
notifySound.play();
});