mirror of
https://github.com/amark/gun.git
synced 2025-07-09 14:22:33 +00:00
65 lines
2.4 KiB
HTML
65 lines
2.4 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<body>
|
|
<style>
|
|
.hide { display: none; }
|
|
form .who { width: 10%; }
|
|
form .what { width: 80%; }
|
|
ul { list-style: none; padding: 0; }
|
|
</style>
|
|
<ul><li class="hide">
|
|
<b class="who"></b>:
|
|
<span class="what"></span>
|
|
<i class="hide when">0</i>
|
|
<u class="hide sort">0</u>
|
|
</li></ul>
|
|
<form>
|
|
<input class="who" placeholder="alias">
|
|
<input class="what" placeholder="message">
|
|
<button>send</button>
|
|
</form>
|
|
<script src="../../gun.js"></script>
|
|
<script>
|
|
var chat = Gun(location.origin + '/gun').get('example/chat/data').not(function(){
|
|
return this.put({1: {who: 'Welcome', what: "to the chat app!", when: 1}}).key('example/chat/data');
|
|
});
|
|
chat.map().val(function(msg, field){
|
|
//console.log("the message:", field, msg);
|
|
if(!spam.lock && !spam.start){ spam(); }
|
|
var $ul = $('ul'), $last = $.sort(field, $ul.lastChild), $msg;
|
|
($msg = $("#msg-" + field) || $ul.insertBefore($.model.cloneNode(true), $last)).id = 'msg-' + field;
|
|
$('.who', $msg)[$.text] = msg.who;
|
|
$('.what', $msg)[$.text] = msg.what;
|
|
$msg.setAttribute('title', $('.when', $msg)[$.text] = new Date(msg.when).toLocaleTimeString().toLowerCase());
|
|
$('.sort', $msg)[$.text] = field;
|
|
window.scrollTo(0, document.body.scrollHeight);
|
|
});
|
|
|
|
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
|
|
$.text = document.body.textContent? 'textContent' : 'innerText'; // because browsers are stupid.
|
|
$.sort = function(when, e){ return (when > ($('.sort', e)[$.text] || 0))? e : $.sort(when, e.previousSibling) }
|
|
($.model = $('ul li').cloneNode(true)).removeAttribute('class');
|
|
|
|
$('.who', $('form')).value = document.cookie;
|
|
$('.what', $('form')).focus();
|
|
$('form').onsubmit = function(e){
|
|
var msg = {};
|
|
msg.when = Gun.time.is();
|
|
msg.who = document.cookie = $('.who', this).value || 'user' + Gun.text.random(6);
|
|
msg.what = $('.what', this).value || '';
|
|
chat.path(msg.when + '_' + Gun.text.random(4)).put(msg);
|
|
$('.what', this).value = '';
|
|
return (e && e.preventDefault()), false;
|
|
};
|
|
|
|
function spam(){
|
|
spam.start = true; spam.lock = false;
|
|
if(spam.count >= 100){ return }
|
|
var $f = $('form');
|
|
$('.what', $f).value = ++spam.count;
|
|
$f.onsubmit();
|
|
setTimeout(spam, 0);
|
|
}; spam.count = 0; spam.lock = true;
|
|
</script>
|
|
</body>
|
|
</html> |