How to use the DOMAPI extension

When to use DOMAPI

The DOMAPI extension is designed to create a smooth API to interact with the JavaScript Document Object Model from Lua. You should use it only when you don't want to write any JavaScript, instead scripting the entire page from Lua.

Beware, with great power comes great responsibility; exposing the entire page to Lua can be dangerous.


Simply add the following code to your page, making sure you edit it to point at your distilled Lua script.

<script src="/path/to/moonshine/vm/moonshine.min.js"></script>
<script src="/path/to/moonshine/extensions/DOMAPI/DOMAPI.moonshine.js"></script>
<script>new shine.VM(shine.DOMAPI).load('./your-script.lua.json');</script>


DOMAPI places a single variable in the Lua global namespace: window. This represents the global namespace in JavaScript. All methods in this tree should be called using a colon (with a couple of exceptions, see below).

window:alert 'hello'
window.document:createElement 'div'
window.navigator.geolocation:getCurrentPosition(successCallback, failCallback)

Extracting the properties of window

The above examples are all well and good, but you wouldn't usually fully-qualify some of these variables. You can quickly extract all the properties of the window table into the Lua global namespace, by calling its extract() method.


alert 'hello'
document:createElement 'div'
navigator.geolocation:getCurrentPosition(successCallback, failCallback)

Instantiating objects

There is no keyword in Lua to intantiate a new object, therefore a new() method is added to classes.

local ws = 'ws://'


See the DOMAPI map example.