176 lines
5.6 KiB
JavaScript
176 lines
5.6 KiB
JavaScript
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?");
|
|
}
|
|
}
|
|
|
|
let ws;
|
|
let username;
|
|
let password;
|
|
|
|
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);
|
|
|
|
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/`;
|
|
}
|
|
|
|
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);
|
|
|
|
var incorrectDetail = 0;
|
|
|
|
ws.onopen = () => {
|
|
if (typeof Notification !== "undefined") {
|
|
Notification.requestPermission();
|
|
}
|
|
console.log('Connected!');
|
|
document.getElementById('login').style.display = 'none';
|
|
document.getElementById('messaging').style.display = 'block';
|
|
const connectMessage = {
|
|
"type": "connect",
|
|
"username": username,
|
|
"token": md5(password),
|
|
"content": `${username} joined the room!`
|
|
}
|
|
ws.send(JSON.stringify(connectMessage));
|
|
ws.onmessage = (event) => {
|
|
if (event.data === "ping") {
|
|
ws.send("pong");
|
|
return;
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
const messagesDiv = document.getElementById('messagebox');
|
|
const messageElement = document.createElement('div');
|
|
if (messageElement) {
|
|
if (messagesDiv) {
|
|
messagesDiv.appendChild(messageElement);
|
|
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
|
messageElement.className = 'message';
|
|
messageElement.textContent = `${message.username}: ${message.content}` ;
|
|
}
|
|
}
|
|
if (document.hidden) {
|
|
const notifiction = new Notification("Chookchat", {body: messageElement.textContent});
|
|
}
|
|
};
|
|
}
|
|
ws.onclose = () => {
|
|
alert("Chookchat has disconnected :/ Refresh the page to try again");
|
|
}
|
|
}
|
|
|
|
function sendMessage() {
|
|
const messageInput = document.getElementById('messageInput');
|
|
const message = messageInput.value.trim();
|
|
|
|
const processedMessage = {
|
|
"type": "message",
|
|
"username": username,
|
|
"token": md5(password),
|
|
"content": message
|
|
}
|
|
|
|
if (processedMessage && ws && ws.readyState === WebSocket.OPEN) {
|
|
ws.send(JSON.stringify(processedMessage));
|
|
messageInput.value = '';
|
|
}
|
|
}
|
|
|
|
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)}}`);
|
|
}
|