-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
218 lines (174 loc) · 5.95 KB
/
script.js
File metadata and controls
218 lines (174 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Automatically scroll to the bottom & toggles
let autoscroll = true;
function toBottom() {
window.scrollTo({
top:document.body.scrollHeight
});
}
function scrollToggle() {
autoscroll = !autoscroll;
console.log("Auto scroll: ", autoscroll);
let toggleButton = document.getElementById("scrollToggle");
if(autoscroll == true) {
toggleButton.innerText = "Autoscroll ON";
} else {
toggleButton.innerText = "Autoscroll OFF";
}
};
// Connect
async function connect() {
return new Promise((resolve, reject) => {
const socket = new WebSocket("wss://chat.stormyyy.xyz");
socket.onopen = function(event) {
console.log("Connection with server established!");
resolve(socket);
};
socket.onclose = function(event) {
console.log(`WebSocket closed. Reconnecting... (Code: ${event.code})`);
setTimeout(connect, 1000); // Reconnect after 1 sec
};
socket.onerror = function (error) {
console.log(error);
reject(error);
};
})
}
// Notification spammer //
let notify = true;
function notifyToggle() {
notify = !notify;
console.log("Notifications: ", autoscroll);
let notifyToggle = document.getElementById("notifyToggle");
if(notify == true) {
notifyToggle.innerText = "Notifications ON";
} else {
notifyToggle.innerText = "Notifications MUTED";
}
}
function sendNotif(notifMessage) {
if(Notification.permission == "granted") {
if(document.hidden || !document.hasFocus()) {
new Notification("StormChat Message!", {
body: notifMessage,
icon: "https://cdn.discordapp.com/avatars/1283147889702604841/29b0ac10fee801532b577c57f3bffb50.webp?size=1024&width=384&height=256"
})
let notificationSound = new Audio("borkNotif.mp3");
notificationSound.play().catch(error => {
console.warn("Notification sound error: ", error);
});
}
} else {
Notification.requestPermission();
}
}
// Client events
(async () => {
try {
// Login form
const socket = await connect();
console.log("WebSocket connected:", socket);
let username = "bob";
let password = "bob";
document.forms['login'].onsubmit = function(event) {
event.preventDefault();
username = this['username-login'].value;
password = this['password-login'].value;
socket.send(`LOGIN,${username},${password},"NULL"`);
this.reset();
return false;
};
document.forms.message_send.onsubmit = function() {
let out_msg = this.message.value;
socket.send(`USER_MESSAGE,${username},${password},${out_msg}`);
this.reset();
this.message.focus();
return false;
}
socket.onmessage = function(event) {
let split_msg = event.data.split(",");
let msg_type = split_msg[0];
let msg = `[${split_msg[1]}]: ${split_msg[2]}`;
if (msg_type === "RESPONSE_LOGIN_SUCCESS") {
document.getElementById("error-message").textContent = "";
document.getElementById("chat").classList.remove("removed");
// document.getElementById("register-form").classList.add("removed");
document.getElementById("login-form").classList.add("removed");
document.createElement
return; // don't show this message
}
if (msg_type === "RESPONSE_LOGIN_FAIL") {
document.getElementById("error-message").textContent = "Error logging in."
return;
}
// Message handler
console.log(event.data);
if (msg == `[${username}]: !givemedoom`) {
let doomElement = document.createElement("iframe");
doomElement.src = "https://ustymukhman.github.io/webDOOM/public/";
doomElement.classList.add("doom");
document.getElementById("messages").append(doomElement);
return;
}
if (msg == `[${username}]: !minecraftforfree`) {
let mcElement = document.createElement("iframe");
mcElement.src = "https://games.stormyyy.dev/minecraft";
mcElement.classList.add("minecraft");
document.getElementById("messages").append(mcElement);
return;
}
if (msg == `[${username}]: !givemeslope`) {
let slopeElement = document.createElement("iframe");
slopeElement.src = "https://y8.com/embed/slope";
slopeElement.scrolling= "no";
slopeElement.classList.add("slope");
document.getElementById("messages").append(slopeElement);
}
if (msg ==`[${username}]: !givemerick`) {
let video = document.createElement("video");
video.controls = true;
video.autoplay = true;
video.classList.add("rick");
let source = document.createElement("source");
source.src = "https://dn720407.ca.archive.org/0/items/rick-roll/Rick%20Roll.mp4";
source.type = "video/mp4";
video.appendChild(source);
document.getElementById("messages").append(video);
return;
}
if (msg ==`[${username}]: !drippybozo`) {
let video = document.createElement("video");
video.controls = true;
video.autoplay = true;
video.classList.add("drippy");
let source = document.createElement("source");
source.src = "https://stormyyy.dev/media/drippy.mp4";
source.type = "video/mp4";
video.appendChild(source);
document.getElementById("messages").append(video);
return;
}
// remove all games/videos added with commands
if (msg ==`[${username}]: !ihatefun`) {
return;
}
// Inserts messages
let msgElement = document.createElement("div");
msgElement.textContent = msg;
document.getElementById("messages").append(msgElement);
// autoscroll
if(autoscroll == true) {
toBottom();
}
// Annoy users with notifications!
if(notify == true) {
sendNotif(msg);
}
}
// no get booted
setInterval(() => {
socket.send("ping");
}, 10000);
} catch (error) {
console.error("Failed to connect:", error);
}
})();