-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
136 lines (121 loc) · 3.67 KB
/
chat.js
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
var socket=io();
var messages = document.getElementById("messages");
var m = document.getElementById("messageSender");
var users = document.getElementById("onlineUsers");
var room = '';
var menuOpen = false;
var online = []
function changeIco(ref) {
var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
link.href = ref;
document.getElementsByTagName('head')[0].appendChild(link);
}
var vis = (function(){
var stateKey, eventKey, keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function(c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})();
function getUrlVars() {
var vars = {};
var parts = window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
var rmCookie = function(name) {
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
function logout() {
rmCookie('user');
rmCookie('key');
window.location.replace("/");
}
function sendMsg() {
if (room !== undefined && m.value.trim() !== '') {
socket.emit('message', m.value);
m.value = '';
}
}
function menu() {
menuOpen = !menuOpen;
if (menuOpen) {
socket.emit('query');
document.getElementById("menuBtn").innerHTML = 'X';
document.getElementById("menu").hidden = false;
} else {
document.getElementById("menuBtn").innerHTML = 'Ξ';
document.getElementById("menu").hidden = true;
}
}
function changeRoom() {
var toGo = document.getElementById('roomSelect').value;
if (toGo == '' || toGo == undefined) {toGo = 'lobby';}
window.location.href = '/static/coms.html?room='+toGo;
}
if (getCookie('user') !== "" && getCookie('user') !== undefined) {
socket.emit('auth', [getCookie("user"), parseInt(getCookie("key"))])
var username = getCookie("user");
}
vis(function(){
if (vis()) {changeIco('/static/favicon.png');}
//changeIco(vis() ? '/static/favicon.png' : '/static/alert.png');
});
socket.on('err', function(data){
console.log(data);
});
socket.on('a-ok', function(){
console.log("A-OK recieved!");
document.getElementById("logout").hidden = false;
room = getUrlVars()['room'];
if (room == undefined) {room = 'lobby';}
//console.log(room);
socket.emit('join', [getCookie("user"), room]);
document.getElementById("roomName").innerHTML = "Room : "+room;
});
socket.on('users online', function(data){
users.innerHTML = '';
for (i in data) {
users.innerHTML += '<br><button style="background:lightcyan;border:none;font-size:15px;" onclick="m.value+=\'@'+data[i]+'\'">'+data[i]+'</button>';
}
});
socket.on("message", function(data){
// Add message
// console.log(data);
var start='<div>'
if (data.includes('@'+username)) {
if (!vis()) {changeIco('/static/alert.png');}
start = '<div class="alert">';
}
messages.innerHTML += start+data+"</div>";
window.scrollTo(0,document.body.scrollHeight);
});