import { initializeApp } from "[Link]
1/firebase-
[Link]";
import {
getFirestore, collection, addDoc, serverTimestamp, onSnapshot,
getStorage, ref, uploadBytes, getDownloadURL
} from "[Link]
const firebaseConfig = {
apiKey: "AIzaSyAFr0X5iAx4ESYKpfZmkcYIR6W4J7Gjdlw",
authDomain: "[Link]",
projectId: "bogdanio-chat",
storageBucket: "[Link]", // Проверьте название в Firebase
Console!
messagingSenderId: "672006362714",
appId: "1:672006362714:web:60bee11c290f7029ba2a63"
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const storage = getStorage(app);
// Элементы DOM
const msgForm = [Link]("msgForm");
const msgInput = [Link]("msgInput");
const fileInput = [Link]("fileInput");
const messages = [Link]("messages");
const notifySound = [Link]("notifySound");
// Автоматическое расширение textarea
[Link]("input", () => {
[Link] = "auto";
[Link] = [Link] + "px";
});
// Отправка сообщений
[Link]("submit", async (e) => {
[Link]();
const text = [Link]();
const file = [Link][0];
if (!text && !file) return;
try {
let mediaUrl = "";
if (file) {
const fileRef = ref(storage, `uploads/${[Link]()}_${[Link]}`);
await uploadBytes(fileRef, file);
mediaUrl = await getDownloadURL(fileRef);
}
await addDoc(collection(db, "messages"), {
text,
mediaUrl,
timestamp: serverTimestamp()
});
[Link] = "";
[Link] = "";
[Link] = "auto";
} catch (error) {
[Link]("Ошибка отправки:", error);
alert("Ошибка: " + [Link]);
}
});
// Получение сообщений в реальном времени
onSnapshot(collection(db, "messages"), (snapshot) => {
[Link] = "";
[Link](doc => {
const msg = [Link]();
const msgEl = [Link]("div");
[Link] = "message";
[Link] = `<div>${[Link] || ""}</div>`;
if ([Link]) {
const isImage = [Link](/\.(jpe?g|png|gif|webp)$/i);
const isVideo = [Link](/\.(mp4|webm|ogg)$/i);
if (isImage) {
[Link] += `<img src="${[Link]}" alt="Image">`;
} else if (isVideo) {
[Link] += `<video controls src="${[Link]}"></video>`;
}
}
[Link](msgEl);
});
[Link] = [Link];
[Link]();
});