chookchat/client-web/index.js

355 lines
12 KiB
JavaScript
Raw Normal View History

2024-11-23 18:10:56 +11:00
alert("Chookchat is currently in early development, expect bugs! Please don't try breaking the public server, do that with your own test server (read more in the Git repo). Thanks for trying Chookchat!")
const width = $(document).width();
if (width < 512) {
console.log("enlarging");
const loginDiv = document.getElementById('login');
if (loginDiv) {
console.log(loginDiv.style.width = `${width}px`);
console.log("enlarged?");
}
}
2024-11-05 20:35:10 +11:00
let ws;
let username;
let password;
2024-11-24 16:07:45 +11:00
let typingTimeout;
let typingPeople = new Array();
2024-11-05 20:35:10 +11:00
2024-11-23 18:10:56 +11:00
function resizeMessaging() {
const messagingDiv = document.getElementById('messaging');
if (messagingDiv) {
messagingDiv.style.width = `${window.innerWidth - 40}px`; // -40 for body margins
messagingDiv.style.height = `${window.innerHeight - 40}px`; // -40 for body margins
}
}
// Call it initially
resizeMessaging();
// Add resize listener to handle window resizing
window.addEventListener('resize', resizeMessaging);
2024-11-05 20:35:10 +11:00
function showConfig() {
const serverconfig = document.getElementById('serverconfig')
if (serverconfig) {
serverconfig.style.display = 'block';
}
}
function md5(string) {
return CryptoJS.MD5(string).toString();
}
function getUrl() {
const serverUrl = document.getElementById('serverUrl').value.trim();
const serverPort = document.getElementById('serverPort').value;
const useWss = document.getElementById('securityStatus').checked;
const protocol = useWss ? 'wss' : 'ws';
const cleanUrl = serverUrl.replace(/^(https?:\/\/|wss?:\/\/)/, '');
return `${protocol}://${cleanUrl}:${serverPort}/api/websocket`;
}
function getSignupUrl() {
const serverUrl = document.getElementById('serverUrl').value.trim();
const serverPort = document.getElementById('serverPort').value;
const useWss = document.getElementById('securityStatus').checked;
const protocol = useWss ? 'https' : 'http';
const cleanUrl = serverUrl.replace(/^(https?:\/\/|wss?:\/\/)/, '');
return `${protocol}://${cleanUrl}:${serverPort}/api/createaccount/`;
}
2024-11-25 14:25:42 +11:00
function getUploadUrl() {
const serverUrl = document.getElementById('serverUrl').value.trim();
const serverPort = document.getElementById('serverPort').value;
const useWss = document.getElementById('securityStatus').checked;
const protocol = useWss ? 'https' : 'http';
2024-11-05 20:35:10 +11:00
2024-11-25 14:25:42 +11:00
const cleanUrl = serverUrl.replace(/^(https?:\/\/|wss?:\/\/)/, '');
return `${protocol}://${cleanUrl}:${serverPort}/api/upload`;
}
2024-11-05 20:35:10 +11:00
function connect() {
username = document.getElementById('username').value;
password = document.getElementById('password').value;
if (!username || !password) {
alert('Please enter a username and password');
return;
}
const wsUrl = getUrl();
if (ws) {
ws.close();
}
ws = new WebSocket(wsUrl);
2024-11-23 18:10:56 +11:00
var incorrectDetail = 0;
2024-11-05 20:35:10 +11:00
ws.onopen = () => {
2024-11-24 14:13:05 +11:00
if (typeof Notification !== "undefined") {
Notification.requestPermission();
}
2024-11-05 20:35:10 +11:00
console.log('Connected!');
document.getElementById('login').style.display = 'none';
document.getElementById('messaging').style.display = 'block';
2024-11-23 18:10:56 +11:00
const connectMessage = {
"type": "connect",
"username": username,
"token": md5(password),
"content": `${username} joined the room!`
}
ws.send(JSON.stringify(connectMessage));
2024-11-24 16:07:45 +11:00
console.log(typingPeople);
2024-11-05 20:35:10 +11:00
ws.onmessage = (event) => {
if (event.data === "ping") {
ws.send("pong");
return;
}
2024-11-23 18:10:56 +11:00
const message = JSON.parse(event.data);
if (message.type == "error") {
if (message.username == "system") {
if (message.content == "invalid-token") {
alert("Your password is incorrect! Please try putting in your password right.");
incorrectDetail = 1;
location.reload();
}
if (message.content == "unknown-account") {
alert("That username isn't on the server. Maybe try registering?");
incorrectDetail = 1;
location.reload();
}
2024-11-24 16:07:45 +11:00
if (message.content == "banned") {
alert("kiddo you're banned lol what did you do to get banned lmaooo");
incorrectDetail = 1;
location.reload();
}
}
}
if (message.type == "typing" && message.content == "1") {
console.log(`${message.username} is typing`);
if (username !== message.username && !typingPeople.includes(message.username)) {
typingPeople.push(message.username);
console.log(typingPeople);
updatePeopleTyping();
}
return;
}
if (message.type == "typing" && message.content == "0") {
console.log(`${message.username} is no longer typing`)
if (username !== message.username && typingPeople.includes(message.username)) {
const index = typingPeople.indexOf(message.username);
typingPeople.splice(index, 1);
console.log(typingPeople);
updatePeopleTyping();
}
return;
}
if (message.type == "users" && message.username == "system") {
usersDiv = document.getElementById("users");
if (usersDiv) {
usersDiv.textContent = `Online users: ${message.content}`
2024-11-23 18:10:56 +11:00
}
2024-11-24 16:07:45 +11:00
return;
2024-11-23 18:10:56 +11:00
}
2024-11-25 14:25:42 +11:00
if (message.type == "file") {
const messagesDiv = document.getElementById('messagebox');
const fileElement = document.createElement('embed');
if (fileElement) {
if (messagesDiv) {
messagesDiv.appendChild(fileElement);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
fileElement.className = "file";
fileElement.src = message.content;
fileElement.height = 200;
fileElement.addEventListener("click", function() {
window.open(message.content, "_blank");
});
}
}
return;
}
2024-11-05 20:35:10 +11:00
const messagesDiv = document.getElementById('messagebox');
const messageElement = document.createElement('div');
if (messageElement) {
if (messagesDiv) {
messagesDiv.appendChild(messageElement);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
messageElement.className = 'message';
2024-11-23 18:10:56 +11:00
messageElement.textContent = `${message.username}: ${message.content}` ;
2024-11-05 20:35:10 +11:00
}
}
if (document.hidden) {
2024-11-23 18:10:56 +11:00
const notifiction = new Notification("Chookchat", {body: messageElement.textContent});
2024-11-05 20:35:10 +11:00
}
};
}
2024-11-23 18:10:56 +11:00
ws.onclose = () => {
alert("Chookchat has disconnected :/ Refresh the page to try again");
}
2024-11-05 20:35:10 +11:00
}
function sendMessage() {
const messageInput = document.getElementById('messageInput');
const message = messageInput.value.trim();
2024-11-23 18:10:56 +11:00
const processedMessage = {
"type": "message",
"username": username,
"token": md5(password),
"content": message
2024-11-25 14:25:42 +11:00
}
2024-11-23 18:10:56 +11:00
if (processedMessage && ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(processedMessage));
2024-11-05 20:35:10 +11:00
messageInput.value = '';
2024-11-24 16:07:45 +11:00
if (typingTimeout) {
clearTimeout(typingTimeout);
}
const stoppedTypingMessage = {
"type": "typing",
"username": username,
"token": md5(password),
"content": "0"
};
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(stoppedTypingMessage));
}
}
const processedMessage2 = {
"type": "typing",
"username": username,
"token": md5(password),
"content": "0"
}
if (processedMessage && ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(processedMessage2));
2024-11-05 20:35:10 +11:00
}
}
2024-11-25 14:25:42 +11:00
function showFileUpload() {
const fileUploadElement = document.getElementById("upload");
if (fileUploadElement) {
fileUploadElement.style.display = "block";
}
}
async function uploadFile() {
console.log("placeholder");
const fileInput = document.getElementById("fileupload");
if (!fileInput.files.length) {
alert("Please add a file!");
return;
}
const formData = new FormData();
formData.append("file", fileInput.files[0]);
try {
const response = await fetch(getUploadUrl(), {
method: 'POST',
mode: "no-cors",
body: formData
});
if (response.ok) {
const result = await response.text();
const processedMessage = {
"type": "message",
"username": username,
"token": md5(password),
"content": `Sent a file`
}
if (processedMessage && ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(processedMessage));
}
} else {
alert("Something went wrong lmao");
}
} catch (error) {
alert(error);
}
const fileUploadElement = document.getElementById("upload");
if (fileUploadElement) {
fileUploadElement.style.display = "none";
}
}
2024-11-05 20:35:10 +11:00
document.getElementById('messageInput').addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
sendMessage();
}
});
document.getElementById('password').addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
connect();
}
});
function register() {
username = document.getElementById('username').value;
password = document.getElementById('password').value;
if (!username || !password) {
alert('Please enter a username and password');
return;
}
window.open(`${getSignupUrl()}username:{${username}}token:{${md5(password)}}`);
}
2024-11-24 16:07:45 +11:00
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function startTypingIndicator() {
if (typingTimeout) {
clearTimeout(typingTimeout);
}
const typingMessage = {
"type": "typing",
"username": username,
"token": md5(password),
"content": "1"
};
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(typingMessage));
}
typingTimeout = setTimeout(() => {
const stoppedTypingMessage = {
"type": "typing",
"username": username,
"token": md5(password),
"content": "0"
};
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(stoppedTypingMessage));
}
}, 5000);
}
function updatePeopleTyping() {
const typingDiv = document.getElementById('typing');
if (typingDiv) {
if (typingPeople.length === 0) {
typingDiv.textContent = '';
} else if (typingPeople.length === 1) {
typingDiv.textContent = `${typingPeople[0]} is typing...`;
} else if (typingPeople.length === 2) {
typingDiv.textContent = `${typingPeople[0]} and ${typingPeople[1]} are typing...`;
} else {
typingDiv.textContent = `${typingPeople.length} people are typing...`;
}
}
}
document.getElementById('messageInput').addEventListener('input', startTypingIndicator);