Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
110 lines (91 sloc) 2.19 KB
<!doctype html>
<html>
<head>
<title>JSONAPI websocket test</title>
<script type="text/javascript" src="../jsonapi-v2.js"></script>
<style>
body {
background:black;
font:13px monospace;
color:white;
padding:5px 5px 45px 5px;
margin:0;
}
ul, ul li, li {
margin:0;
padding:0;
}
#input {
position:fixed;
bottom:0;
left:0;
right:0;
background:white;
padding:5px;
}
input {
margin:0;
padding:5px;
width:83%;
}
button {
width:9%;
margin-left:5%;
}
.color-33 {color:gold;}
.color-35 {color:purple;}
.color-37 {color:gray;}
.color-31 {color:red;}
</style>
</head>
<body>
<ul name="console" id="console"></ul>
<div id="input"><input type="text" id="inp" /><button id="run">Send</button></div>
<script type="text/javascript">
var j = new JSONAPI({
hostname: "localhost",
port: 20059,
username: "usernameGoesHere",
password: "passwordGoesHere",
salt: "salt goes here"
})
, $ = function(x) { return document.getElementById(x); }
, c = $('console')
;
j.stream('console', function(json) {
appendLine(json.success.line);
});
function send () {
var txt = $('inp').value;
if(!txt.length) return;
j.call('runConsoleCommand', txt);
appendLine("> " + txt + "\r\n");
}
function appendLine (txt) {
var el = document.createElement('li');
el.innerHTML = colorize(escape_html(txt));
c.appendChild(el);
document.body.scrollTop = document.body.scrollHeight;
}
$('run').addEventListener('click', send);
$('inp').addEventListener('keydown', function(e) {
if(e.which == 13) {
send();
e.target.value = "";
}
});
$('inp').focus();
function escape_html(string) {
return (''+string).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g,'&#x2F;');
}
function colorize(str) {
var i = 0;
return str.replace(/(\[([0-9]{1,2})m)/g, function() {
var txt = (i ? '</span>' : '') + '<span class="color-' + arguments[2] + '">';
i++;
return txt;
}) + (i?'</span>':'');
}
</script>
</body>
</html>