Hi All,
I’ve been working with the new ObjC API and have got to a releasable point on a JavaScript library.
I’m sure the code below will demonstrate what it’s capable of better than I can put into words!
JavaScript only:
~~~ -- JS only webview (no HTML provided) local webview = WebView()-- Imported functions made available to JS!
-- (Return values not yet supported)
webview:import("print", function(...)
print(...)
end)
-- Load JavaScript string
webview:loadJS([[
// Functions can accept parameters from Lua!
function main(num1, num2) {
// You can call imported Lua functions!
print("Hello JavaScript!");
// Values can be returned to Lua!
return num1 + num2;
}
]])
-- Can call a JS function with arguments and get the result!
local result = webview:call("main", 9, 7)
print("Result: ", result)
<h2>Loading a HTML page & calling a JavaScript function defined in the HTML:</h2>
-- Load and show a HTML page from disk
local webview = WebView(asset.core)
webview:show()
-- Imported functions made available to JS!
-- (Return values not yet supported)
webview:import("print", function(...)
print(...)
end)
-- Call a function defined in the HTML file
webview:call("htmlTest")
<h2>Just displaying a simple web page:</h2>
-- Load and display a simple HTML page
local webview = WebView([[
<center>
<br/>
<br/>
Hello Codea!
<br/>
I came from a string :)
</center>
]])
webview:show()
<h2>Bulk function imports:</h2>
-- JS only webview (no HTML provided)
local webview = WebView()
-- Imported functions made available to JS!
-- (Return values not yet supported)
local imports = {
["print"] = function(...)
print(...)
end,
["helloWorld"] = function()
print("Hello Codea!")
end
}
webview:import(imports)
-- Load JavaScript string
webview:loadJS([[
// Functions can accept parameters from Lua!
function main(num1, num2) {
// Can call imported Lua functions!
print("Demo! -----");
// Call another imported function!
helloWorld();
// Values can be returned to Lua!
return num1 * num2;
}
]])
-- Can call a JS function with arguments and get the result!
local result = webview:call("main", 9, 9)
print("Result: ", result)
Note that this entire library relies on some async -> sync magic provided by ‘STLib’ that requires a shift in the normal program flow in Codea.
Rather than use the `setup()` & `draw()` functions, please run your code within a `main()` function (shown in the included project demos). I hope to release this as a separate library soon as it allows for some very useful functionality.
Steppers
o7