Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created April 3, 2013 21:47
Show Gist options
  • Select an option

  • Save beccasaurus/5305711 to your computer and use it in GitHub Desktop.

Select an option

Save beccasaurus/5305711 to your computer and use it in GitHub Desktop.
Run arbitrary code in Dartium
<!DOCTYPE html>
<html>
<head>
<style>
textarea { width: 100%; height: 150px; }
</style>
<script>
window.addEventListener('keydown', function(e) {
if ((e.keyCode == 10 || e.keyCode == 13) && event.ctrlKey)
run();
});
function run() {
addCodeToRunToQueue();
instantiateDartIframeToRunCode();
}
var input;
var codeToRun = [];
function addCodeToRunToQueue() {
if (input == null)
input = document.getElementById('input');
codeToRun.push(input.value);
}
function instantiateDartIframeToRunCode() {
var iframe = document.createElement('iframe');
iframe.setAttribute('src', 'runner.html');
iframe.setAttribute('sandbox', 'allow-scripts');
iframe.style.display = 'none';
document.body.appendChild(iframe);
}
window.addEventListener('message', function(e) {
if (e.data == 'can haz code?')
e.source.postMessage(codeToRun.pop(), '*');
});
</script>
</head>
<body>
<textarea id='input'>main() {
print('Hello World');
}</textarea>
<input type='button' value='Run (or Ctrl+Enter)' onclick='run()'></input>
View JavaScript console for output
</body>
</html>
<!DOCTYPE>
<html>
<head>
<script>
window.addEventListener('message', function(e) {
var script = document.createElement('script');
script.setAttribute('type', 'application/dart');
script.textContent = e.data;
document.body.appendChild(script);
navigator.webkitStartDart();
});
parent.postMessage('can haz code?', '*');
</script>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment