-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtalktalktalk.html
333 lines (306 loc) · 14.2 KB
/
talktalktalk.html
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!--
# TalkTalkTalk
#
# is an easy-installable small chat room, with chat history.
#
# author: Joseph Ernest (twitter: @JosephErnest)
# url: http://github.com/josephernest/talktalktalk
# license: MIT license
-->
<!DOCTYPE html>
<html>
<head>
<title>TalkTalkTalk</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
* { margin: 0; padding: 0; font-family: monospace; }
html { overflow-y: scroll; }
#writing { width: 100%; height: 10%; position: fixed; height: 100px; bottom: 0; resize: none; border-top: 1px solid #aaa; }
#wrapper { width: 75%; margin: 10px 10px 110px 10px; word-wrap: break-word; }
#main a { text-decoration: none; color: inherit; }
#main b { display: inline-block; } /* to prevent right to left utf8 characters */
#right { width: 20%; top: 0; left: 80%; position: fixed; height: calc(100% - 120px); padding: 10px 0 10px 0; overflow-y: auto; overflow-x: hidden; }
#username { padding: 2px 0 0px 1px; }
.dashed { display: inline-block; border-bottom: 1px dashed #CCC; cursor: pointer; margin-bottom: 1px; }
#usernameinput { display: none; width: 80%; }
#fork { z-index: 10; }
#previous { margin-bottom: 20px; display: block; }
#popup { z-index: 10; position: fixed; left: 20%; width:60%; top:40%; border: solid black 1px; background-color: white; text-align: center; padding: 30px 10px 30px 10px; color: black !important; }
.hidden { display: none !important; }
.opaque { color: #AAA; }
@media (max-width: 500px) { #right { width: 120px; left: calc(100% - 120px); } #wrapper { width: calc(100% - 130px); } #banner { display: none; } }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<a id="banner" href="https://github.com/josephernest/talktalktalk" target="_blank"><img style="position: fixed; top: 0; right: 0; border: 0; width: 115px;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" id="fork"></a>
<div id="right">
TALKTALKTALK<br>############<br><br><br>
<div id="notifications">NOTIFICATIONS<br><span id="notificationswitch" class="dashed">on</span><br><br></div>
USERNAME<br><span id="username" class="dashed">user</span><input value="user1" id="usernameinput" maxlength="16" /><br><br>
ONLINE USERS<div id="connected"></div>
</div>
<div id="wrapper">
<a id="previous" href="" class="hidden">Load previous messages...</a>
<div id="main">
</div>
</div>
<textarea id="writing"></textarea>
<div id="popup" class="hidden popup"></div>
<script type="text/javascript">
var popsound = new Audio('popsound.mp3');
var randomname = 'user' + Math.floor((Math.random() * 1000) + 1);
var displayeduser;
var ws;
var lastPong;
var users;
var firstId;
var lastId;
var loop;
function is_scrolled_end() { // See http://stackoverflow.com/a/40370876/1422096
return ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight);
}
function scroll_end () {
document.body.scrollTop = document.body.scrollHeight; // both useful because this one not working in FF47
document.documentElement.scrollTop = document.documentElement.scrollHeight; // both useful because this one not working in Chrome 43 Android
}
function scroll_top () {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
function add_urls(s) {
return s.replace(/(https?:\/\/[^ ]+)/g, '<a href="$1" target="_blank">$1</a>');
}
function timeConverter(t) { // http://stackoverflow.com/a/40449006/1422096
if (t == null)
return '';
var a = new Date(t * 1000);
var today = new Date();
var yesterday = new Date(Date.now() - 86400000);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var time = ("0" + hour).slice(-2) + ':' + ("0" + min).slice(-2);
if (a.setHours(0,0,0,0) == today.setHours(0,0,0,0))
return 'today, ' + time;
else if (a.setHours(0,0,0,0) == yesterday.setHours(0,0,0,0))
return 'yesterday, ' + time;
else if (year == today.getFullYear())
return date + ' ' + month + ', ' + time;
else
return date + ' ' + month + ' ' + year + ', ' + time;
}
function mediaPlaybackRequiresUserGesture() { // http://stackoverflow.com/a/40474415/1422096
var audio = document.createElement('audio');
audio.play();
return audio.paused;
}
function ready() {
lastPong = undefined;
users = [];
firstId = Infinity;
lastId = -1;
if (mediaPlaybackRequiresUserGesture()) { // mobile devices require user gesture to trigger a play. "Notifications" off for them.
$('#notificationswitch').text('off');
$('#notifications').hide();
}
function popup_message (message, clicktoclosepopup, clicktoreload) {
$('#popup').text(message).removeClass('hidden');
$(':not(.popup)').addClass('opaque');
if (clicktoclosepopup)
$(document).on('click keydown', function() { $(document).off('click keydown'); $('#popup').addClass('hidden'); $(':not(#popup)').removeClass('opaque'); });
else if (clicktoreload)
$(document).on('click keydown', function() { $(document).off('click keydown'); $(document).off('keydown'); ws.close(); ready(); });
}
if (!window.WebSocket) {
$('#popup').text('Your browser is not supported, please use another browser.').removeClass('hidden');
$('#writing').prop('disabled', true);
$(':not(#popup)').addClass('opaque');
}
else {
$('#main').text('');
$('#writing').val('');
}
if (localStorage.getItem("username") === null || localStorage.getItem("username") === '') {
$('#username').text(randomname);
localStorage.setItem("username", $('#username').text());
}
else
$('#username').text(localStorage.getItem("username"));
ws = new WebSocket(location.href.replace('http', 'ws') + 'ws'); // replace replaces only the first occurence
$(window).on('beforeunload', function() {
ws.close();
});
ws.onopen = function() {
ws.send(JSON.stringify({ "type" : "username", "username" : $('#username').text() }));
lastPong = Date.now();
$('#popup').addClass('hidden'); $(':not(#popup)').removeClass('opaque');
$('#writing').focus();
scroll_end();
setTimeout(function() { scroll_end(); }, 200); // useful on touch devices, when keyboard opens
};
ws.onclose = function() {
setTimeout(function() {
if (ws.readyState != 0 && ws.readyState != 1)
popup_message('Click anywhere or press any key to reload the page.', false, true);
}, 2000);
};
loop = setInterval(function() {
ws.send('ping');
if (Date.now() - lastPong > 10000) { // disconnected from internet or phone idle mode; in both cases, ws.readyState can be 1 (OPEN) so we can't use that
ws.send('ping'); // this is your last chance ! we send a packet now, and if in 1 second, nothings happen, this means we're dead
setTimeout( function() {
if (Date.now() - lastPong > 10000) // you missed your last chance !
popup_message('Click anywhere or press any key to reload the page.', false, true); // disconnected from internet
else // ok the phone was just idle, let's resume
ws.send(JSON.stringify({ "type" : "username", "username" : $('#username').text() })); // let's send username again, so that other users see me
}, 1000);
}
}, 5000);
$('#previous').click(function(e) {
ws.send(JSON.stringify({'type': 'messagesbefore', 'id': firstId}));
e.preventDefault();
return false;
});
ws.onmessage = function(e) {
lastPong = Date.now();
if (e['data'].slice(0,2) === 'id') {
var serverlastid = e['data'].slice(2);
if (serverlastid != lastId) { // some messages were lost
ws.send(JSON.stringify({'type': 'messagesafter', 'id': lastId}));
}
return;
}
var data = JSON.parse(e['data']);
if (data['type'] === 'message') {
if (data['id'] > lastId + 1) { // some messages were lost
ws.send(JSON.stringify({'type': 'messagesafter', 'id': lastId}));
return;
}
lastId = data['id'];
var isscrolledend = is_scrolled_end();
$('#main').append('<p title="' + timeConverter(data['datetime']) + '"><b>' + data['username'] + '</b>: ' + add_urls(data['message']) + '</p>');
if (isscrolledend)
scroll_end();
if (data['username'] !== $('#username').text() && data['username'] !== displayeduser && $('#notificationswitch').text() === 'on') {
popsound.load(); popsound.play();
}
}
else if (data['type'] === 'messages') {
var newmessages = document.createDocumentFragment();
var messagelist = data['messages'];
for (var i = 0; i < messagelist.length; i++) {
var msg = JSON.parse(messagelist[i]);
if (data['before'] != 1 && msg['id'] <= lastId) // message already added
continue;
$(newmessages).append('<p title="' + timeConverter(msg['datetime']) + '"><b>' + msg['username'] + '</b>: ' + add_urls(msg['message']) + '</p>');
firstId = Math.min(firstId, msg['id'])
lastId = Math.max(lastId, msg['id']);
}
if (data['before'] == 1)
$('#main').prepend(newmessages);
else
$('#main').append(newmessages);
if (firstId > 0 && firstId != Infinity)
$('#previous').removeClass('hidden');
else
$('#previous').addClass('hidden');
if (data['before'] == 1)
scroll_top();
else
scroll_end();
}
else if (data['type'] === 'userlist') {
users = data['connected'];
$('#connected').html(users.join('<br>')); // or .map(x => `${x}<br>`).join('')
}
else if (data['type'] === 'username') { // server asks to (re)send username
ws.send(JSON.stringify({ "type" : "username", "username" : $('#username').text() }));
}
else if (data['type'] === 'usernameunavailable') {
$('#username').text(data['username']);
localStorage.setItem("username", $('#username').text());
popup_message('Your username has been changed because the one you entered is reserved.', true, false);
}
else if (data['type'] === 'flood') {
popup_message('Please do not flood this chat.', false, false);
ws.onclose = undefined;
clearInterval(loop);
ws.close();
}
else if (data['type'] === 'displayeduser') {
displayeduser = data['username'];
}
};
$('#username').on('click', function(e) {
$('#username').hide();
$('#usernameinput').val($('#username').text()).show().focus();
e.preventDefault();
return false;
});
$('#writing').on('click', function(e) { // trick for touch devices: when phone keyboard opens, we won't be scrolled-end anymore, so force it
if (is_scrolled_end())
setTimeout(function() { scroll_end(); }, 200);
});
$('#writing').keydown(function(e) {
if (e.keyCode == 13 && !e.shiftKey) {
if ($('#writing').val().trim() !== '')
ws.send(JSON.stringify({ type: 'message', username: $('#username').text(), message: $('#writing').val().trim() }));
$('#writing').val('');
e.preventDefault();
}
else if (e.keyCode == 9) { // see http://stackoverflow.com/a/40319943/1422096
var input = document.getElementById('writing');
var patt = /\b@?(\S+)$/;
e.preventDefault();
var start = input.selectionStart;
var seg = input.value.slice(0, start);
var match = (seg.match(patt) || [])[0];
if (!match) { return; }
var idx = users.findIndex(function (x) { return x.startsWith(match); });
if (idx < 0) { return; }
var replace = users[users[idx] === match ? (idx + 1) % users.length : idx];
var newSeg = seg.replace(patt, replace);
input.value = newSeg + input.value.slice(start);
input.setSelectionRange(newSeg.length, newSeg.length);
}
else if (e.keyCode == 33 || e.keyCode == 34) { // alows to scroll with PAGE UP / DOWN even if textarea has focus
$('#writing').blur();
setTimeout(function() { $('#writing').focus(); }, 10);
}
});
$('#usernameinput').on('blur', function(e) {
var usr = $('#usernameinput').val();
usr = usr.replace(/[:\s]/g, '');
if (usr === '')
usr = randomname;
$('#username').text(usr);
localStorage.setItem("username", usr);
$('#usernameinput').hide();
$('#username').show();
ws.send(JSON.stringify({ "type" : "username", "username" : usr }));
e.preventDefault();
});
$('#usernameinput').keydown(function(e) {
if (e.keyCode == 13) {
$('#usernameinput').blur();
}
if (e.keyCode == 27) {
$('#usernameinput').val($('#username').text()).hide();
$('#username').show();
e.preventDefault();
return false;
}
});
$('#notificationswitch').click(function() {
$('#notificationswitch').text($('#notificationswitch').text() === 'on' ? 'off' : 'on');
});
}
$(document).ready(ready);
</script>
</body>
</html>