JSFiddle - React, Tailwind, and code Playground
HTML
<p>
Input:<br/>
<form id="f1"><input id="input" /></form>
</p>
<p>
Output:<br/>
<textarea id="output" cols="50" rows="10"></textarea>
</p>
JavaScript
function Jail() {
// This is some code inside jail that is user-accessible
this.hello = function() {
return "hello world!";
}
this.eval = function(s) {
return eval(s);
}
var window = undefined;
var document = undefined;
var alert = undefined;
};
(function() {
var jail = new Jail();
var hidden = "Some object that I don't want a user to have access to";
document.getElementById('f1').onsubmit = function() {
var output = document.getElementById('output');
try {
var expr = document.getElementById('input').value;
var res = jail.eval(expr);
output.value = res;
} catch (e) {
output.value = e.toString();
console.error(e);
} finally {
return false;
}
}
})();