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.

106 lines
2.9 KiB

<!DOCTYPE html>
<html>
<head>
<title>Dukweb.js test</title>
<script type="text/javascript" src="dukweb.js"></script>
<script type="text/javascript" src="jquery-1.11.0.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">Works in Chrome/Chromium/Firefox. There is much to
improve in the Duktape/browser bindings and errors are not handled nicely
yet, so keep your Javascript Console open. Also note that this page takes
a few seconds to (re)load.</p>
<div id="dukweb-input-wrapper">
<textarea id="dukweb-input">
11 years ago
// 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: Duktape.Buffer('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();
try {
var res = Duktape.eval(code);
dukwebAppendOutput('==> ' + res + '\n');
} catch (e) {
dukwebAppendOutput('==> ' + (e.stack || e) + '\n');
}
});
}
try {
dukwebSetup();
} catch (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>