You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

137 lines
4.3 KiB

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Dukweb.js test</title>
<script type="text/javascript" src="dukweb.js"></script>
<script type="text/javascript" src="jquery-1.11.2.js"></script>
<link href="dukweb.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>((o) Dukweb.js test</h1>
<div id="content-middle">
<p id="dukweb-intro">Duktape compiled using emcc into JS + wasm (can
also be compiled into pure JS), should work in Chrome/Chromium/Firefox.
See Web Console for details and errors. This page takes a a few seconds
to (re)load. You can embed code in the Dukweb link as a fragment identifier:
<a href="http://duktape.org/dukweb.html#print('Hello%20world!')%3B">http://duktape.org/dukweb.html#print('Hello%20world!')%3B</a>.</p>
<div id="dukweb-input-wrapper">
<textarea id="dukweb-input">
// This code runs inside the Duktape VM, print() and alert() are bound so
// that they integrate with the browser environment: output is written to
// the text area below.
print('Duktape version is ' + Duktape.version);
alert('alert from Duktape code');
print(Duktape.enc('jx', { foo: 1, bar: 'xxx', quux: Uint8Array.allocPlain('foo') }));
var obj = {};
Duktape.fin(obj, function () { print('obj finalized'); });
obj = null;
// The Dukweb object provides bindings to access the underlying web
// environment. Most importantly, you can use Dukweb.eval() to run
// code in the browser.
Dukweb.eval('alert("your browser userAgent is: " + navigator.userAgent.toString())');
</textarea>
</div>
<div id="dukweb-evaluate-wrapper">
<span id="dukweb-evaluate"><span>Evaluate</span></span>
</div>
<div id="dukweb-output-wrapper">
<pre id="dukweb-output">
</pre>
</div>
<script type="text/javascript">
//<![CDATA[
function dukwebSetup() {
if (typeof Duktape !== 'object') {
throw new Error('initialization failed (Duktape is undefined)');
}
if (!Duktape.initSuccess) {
throw new Error('initialization failed (initSuccess is false)');
}
// Override handlers for Duktape print() and alert() replacements, because
// we want to redirect output in our own way.
var dukwebOutput = [];
function dukwebAppendOutput(txt) {
var oldtxt = $('#dukweb-output').text();
var newtxt = oldtxt + txt; // find out a better way
$('#dukweb-output').text(newtxt);
}
Duktape.printHandler = function(msg) {
dukwebAppendOutput(msg + '\n');
}
Duktape.alertHandler = function(msg) {
alert(msg);
}
$('#dukweb-evaluate').click(function () {
$('#dukweb-output').text('');
dukwebOutput = [];
var code = $('#dukweb-input').val();
if (code.length < 65536 && location) {
// Although Firefox automatically decodes location.hash, this
// still seems to work.
location.hash = encodeURIComponent(code);
}
setTimeout(function () {
try {
var res = Duktape.eval(code);
dukwebAppendOutput('==> ' + res + '\n');
} catch (e) {
dukwebAppendOutput('==> ' + (e.stack || e) + '\n');
}
}, 1); // sleep to allow hash to update immediately
});
}
$(document).ready(function () {
function checkUriCode() {
// In some browsers location.hash is automatically decoded and in
// others it is not. This is more portable:
// http://stackoverflow.com/questions/4835784/firefox-automatically-decoding-encoded-parameter-in-url-does-not-happen-in-ie
if (location && location.href && location.href.indexOf('#') >= 0) {
var code = location.href.substring(location.href.indexOf('#') + 1);
$('#dukweb-input').val(decodeURIComponent(code));
}
/*
if (location && location.hash && location.hash[0] === '#') {
$('#dukweb-input').val(decodeURIComponent(location.hash.substring(1)));
}
*/
}
try {
checkUriCode();
} catch (e) {
alert(e);
}
Duktape.getInitializedPromise().then(function () {
dukwebSetup();
}).catch(function (e) {
$('#dukweb-output')
.addClass('error')
.text(
'Dukweb.js initialization failed (perhaps your browser is not compatible?):\n\n' +
String(e.stack || e) + '\n\n');
});
});
//]]>
</script>
</div> <!-- content-middle -->
</body>
</html>