Created
January 2, 2010 05:57
-
-
Save h0rs3r4dish/267398 to your computer and use it in GitHub Desktop.
A Javascript REPL/console thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Javascript REPL</title> | |
<script type="text/javascript"> | |
function $(i){return document.getElementById(i);} | |
</script> | |
</head> | |
<body> | |
<pre><span id="repl"></span><span style="font-size:12px;">█</span></pre> | |
<script type="text/javascript"> | |
// Some constants | |
var KEY_BACKSPACE = 8; | |
var KEY_RETURN = 13; | |
var KEY_UP = 38; | |
var KEY_DOWN = 40; | |
var repl = { | |
version: "1.0" | |
} | |
repl.screen = { | |
div: $("repl"), | |
print: function(i) { | |
repl.screen.div.innerHTML += i; | |
}, | |
println: function(i) { | |
repl.screen.div.innerHTML += i + "\n"; | |
}, | |
backspace: function() { | |
if (repl.command.text.length == 0) | |
return; | |
v = repl.screen.div.innerHTML; | |
repl.screen.div.innerHTML = v.substring(0,v.length-1); | |
repl.command.text = repl.command.text.substring(0,repl.command.text.length-1); | |
}, | |
clearline: function() { | |
while (repl.command.text.length > 0) | |
repl.screen.backspace(); | |
} | |
} | |
repl.command = { | |
text: '', | |
history: [], | |
historypos: 0, | |
list: { | |
"eval": function(str) { | |
repl.screen.println(eval(str)); | |
}, | |
"set": function(str) { | |
var parted = str.split(' '); | |
repl.env[parted.shift()] = parted.join(' '); | |
}, | |
"env": function(str) { | |
repl.screen.println(repl.env[str]); | |
}, | |
"command": function(str) { | |
var parted = str.split(' '); | |
var name = parted.shift(); | |
eval('repl.command.list["' + name + '"] = function(args){' + | |
parted.join(' ') + '}'); | |
return name; | |
}, | |
"get": function(url) { | |
if (url.indexOf(':') == -1) | |
url = "http://"+url; | |
repl.screen.println("Getting '"+url+"'"); | |
try { | |
var req = new XMLHttpRequest(); | |
req.open('GET',url,false); req.send(null); | |
if (req.status == 200) { | |
repl.screen.println(req.responseText); | |
} else { repl.screen.println("Error encountered: "+req.status); } | |
} catch(e) { | |
repl.screen.println("Error encountered: "+e); return; | |
} | |
}, | |
"help": function(args) { | |
repl.screen.println("jsrepl commands:\n"); | |
place = 0; | |
for (var i in repl.command.list) { | |
if (place == 3) { | |
place = 0; repl.screen.print("\n"); | |
} | |
repl.screen.print(i); | |
var diff = 15 - i.length; | |
for (var j = 0; j < diff; j++) { | |
repl.screen.print(' '); | |
} | |
place += 1; | |
} | |
repl.screen.print("\n"); | |
} | |
}, | |
launch: function() { | |
repl.command.history.push(repl.command.text); | |
input = repl.command.text; | |
repl.command.text = ''; | |
if (input[0] == '`') { | |
repl.screen.println(eval(input.replace('`',''))); return; | |
} | |
cmd = input.split(' '); | |
repl.command.run(cmd.shift(),cmd.join(' ')); | |
}, | |
run: function(cmd,args) { | |
if (args == null) args = []; | |
if (repl.command.list[cmd] == null) { | |
repl.screen.println("No such command '"+cmd+"'"); | |
return; | |
} | |
repl.command.list[cmd](args); | |
}, | |
pre_prompt: function() { | |
document.body.style.background = repl.env.background; | |
document.body.style.color = repl.env.foreground; | |
} | |
} | |
repl.env = { | |
user: "user", | |
background: "#fff", | |
foreground: "#000" | |
} | |
repl.input = { | |
keyboard_event: function(evt) { | |
if (evt.keyCode) { | |
var keyCode = evt.keyCode; | |
if (keyCode == KEY_BACKSPACE) { | |
repl.screen.backspace(); | |
} else if (keyCode == KEY_RETURN) { | |
repl.screen.print("\n"); | |
repl.command.launch(); | |
repl.command.pre_prompt(); | |
repl.screen.print(repl.env.user+"@jsrepl$ "); | |
} else if (keyCode == KEY_UP) { | |
return; // XXX: It doesn't work. Fuck JS. | |
repl.screen.clearline(); | |
if (repl.command.history.length < (-repl.command.historypos)+1) | |
return; | |
repl.command.historypos -= 1; | |
repl.command.text = repl.command.history.slice(repl.command.historypos,1)[0]; | |
repl.screen.print(repl.command.text); | |
} else if (keyCode == KEY_DOWN) { | |
return; // XXX: It doesn't work. Fuck JS. | |
repl.screen.clearline(); | |
if (repl.command.historypos == -1) | |
return; | |
repl.command.historypos += 1; | |
repl.command.text = repl.command.history.slice(repl.command.historypos,1)[0]; | |
repl.screen.print(repl.command.text); | |
} | |
} else { | |
kchar = String.fromCharCode(evt.charCode); | |
repl.screen.print(kchar); | |
repl.command.text += kchar; | |
} | |
} | |
} | |
document.onkeypress = repl.input.keyboard_event; | |
repl.screen.println("Javascript REPL version "+repl.version+"\n"); | |
repl.screen.print("user@jsrepl$ "); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment