One of the most requested features for LiveCode HTML5 applications has been to add the ability to interact with the browser’s JavaScript APIs.  In the latest Developer Preview release of LiveCode 9, we’ve added this capability!
You can “do” it
For a long time, the LiveCode do command has had a form which runs the script you give it as a different programming language.  The languages available are listed in the alternateLanguages, depending on which operating system you’re running.  For example, you might do some arithmetic using VBScript on Windows with:
do "result = 1 + 1" as "vbscript"
We have now added “JavaScript” as one of the possible alternate languages in LiveCode HTML5 applications.  You can evaluate a snippet of JavaScript code with the do command, and the result will be set to whatever the result of the JavaScript computation was.
The colours of the rainbow
It’s pretty easy to use this new capability to manipulate the webpage surrounding an HTML5 standalone stack. Â As a simple example, you can easily build an app that changes the background colour of the part of the page surrounding it, using the JavaScript DOM (Document Object Model) APIs and some CSS.
First, try out the JavaScript snippet that your app is going to run. Â Open an existing HTML5 standalone in a browser, and go to the JavaScript developer console. Â Run:
Module.canvas.parentNode.setAttribute("style", "background: hsl(80, 100%, 50%)")
Explanation:
- The Module global variable contains all the information about your app
- It has a canvas field, which is the HTML element where the app is displayed.
- The canvas has a parentNode field, which its parent HTML element.
- We set the style attribute on the element to a CSS snippet that changes its background colour, specifying the colour using a hue, saturation and luminance (HSL) triple.
Your app should now be surrounded by a beautiful green colour!
Jump into LiveCode and create a new stack, and drag a button onto it. Â When the button is clicked, the app will animate the web page by cycling through different colours, and when you click it again, it’ll stop.
Edit the script of the button.  The script needs some static variables (to hold the animation state), a mouseUp handler to deal with starting and stopping, and a doUpdate handler to do the colour animation by running JavaScript.
local sCycleEnabled
local sHue
-- When the button is clicked, it toggles the
-- animation on or off and updates its label to match
on mouseUp
put not sCycleEnabled into sCycleEnabled
if sCycleEnabled then
set the label of me to "Stop"
doUpdate
else
set the label of me to "Start"
end if
end mouseUp
-- Each animation tick, gradually cycle through the
-- colours by modifying the colour hue
on doUpdate
-- The hue is actually an angle in degrees
put (1 + sHue) mod 360 into sHue
-- Prepare and run the JavaScript snippet
local tScript
put "Module.canvas.parentNode.setAttribute" after tScript
put "(" & quote & "style" & quote & ", " after tScript
put quote &
merge("background: hsl([[sHue]],100%,50%)") &
quote & ")" after tScript
if the platform is "html5" then
do tScript as "JavaScript"
end if
-- Continue the animation
if sCycleEnabled then
send "doUpdate" to me in 100 millisecs
end if
end doUpdate
If you like, you can check that clicking the button starts and stops as you expect, and possibly add a field to show the JavaScript that the app is executing.
And that’s it. Deploy your app as an HTML5 standalone, open it in a browser, click “Start” and enjoy the pretty colours.
This is just one of the exciting new features coming in LiveCode 9. Try it out now! You can get LiveCode 9 DP 2 via automatic updates from within the IDE (‘Help → Check For Updates’), or download an installer.
3 comments
Join the conversationDavid Simpson - November 23, 2016
It would be neat if this new JavaScript functionality would allow cross application reading of the clipboard as described in linked blog posting from LucidChart:
https://www.lucidchart.com/techblog/2014/12/02/definitive-guide-copying-pasting-javascript/
Ideally, I would like to be able to use the built in LiveCode clipboard handling commands to read info copied from other apps. Plain text would be fine for my needs. So I am thinking that maybe the HTML5 builder could automatically add the appropriate bits of code on its own when the HTML5 app gets built. It is generally considered to be almost impossible to make cross application clipboard handling work – Google doesn’t even do it. But if LiveCode could make it work transparently – that would be amazingly cool.
Peter Brett - November 24, 2016
Getting information from the clipboard is actually pretty difficult, even in pure JS apps. Browser security policies mean that the clipboard information is only available at certain times, under specific constraints. I think that at the very least, there would need to be some way for JavaScript running in the web page to send a message to the HTML5 LiveCode app. I think that it is already possible to put content onto the clipboard using the feature described in this blog post, though.
MaxV - December 1, 2016
Here is a working example: http://www.maxvessi.net/livecode/public/HTML5withJavascriptExample1/