28 lines
883 B
JavaScript
Raw Normal View History

2025-01-03 01:47:07 -08:00
const runChat = async () => {
const frenId =
new URLSearchParams(document.location.search).get("fren_id") ??
document.getElementById("fren_id").value;
const html = await fetch(`/chat/messages?fren_id=${frenId}`).then((r) =>
r.text(),
);
const { scrollTop, scrollHeight } = document.getElementById(
"chat-container",
) ?? { scrollTop: 0 };
const isAtEdge = scrollTop === scrollHeight || scrollTop === 0;
document.getElementById("messages").innerHTML = html;
if (isAtEdge) {
document.getElementById("chat-container").scrollTop =
document.getElementById("chat-container").scrollHeight;
} else {
// save the position.
document.getElementById("chat-container").scrollTop = scrollTop;
}
};
setTimeout(() => {
2025-01-03 02:12:41 -08:00
if (document.location.pathname.startsWith("/chat")) {
runChat().then(() => setInterval(runChat, 2_500));
2025-01-03 01:47:07 -08:00
}
}, 200);