Page 1 of 1

Calling JS in a browser object from LC?

Posted: Wed Oct 14, 2020 1:57 pm
by thatkeith
Is there an up-to-date example or instructions that show how to call a JavaScript handler in a browser widget and get the result? I've been trawling threads here without finding something definitive. I've also looked at the 'Livecode Javascript communication' video on YouTube but that's based on LC 6.7.

I have an HTML page with embedded JavaScript. This shows an interactive 360 panoramic image, and the JavaScript is used in that to return the current view parameters (horizontal and vertical direction and zoom level). This is the JavaScript:

Code: Select all

	function get_current_view()
	{
		if (krpano)
		{
			var hlookat = krpano.get("view.hlookat");
			var vlookat = krpano.get("view.vlookat");
			var fov = krpano.get("view.fov");
			var distortion = krpano.get("view.distortion");
			document.getElementById("currentview").innerHTML = 
				'hlookat="' + hlookat.toFixed(2) + '" '+
				'vlookat="' + vlookat.toFixed(2) + '" '+
				'fov="' + fov.toFixed(2) + '" '+
				'distortion="' + distortion.toFixed(2) + '"';
		}
	}
I'd like to be able to call this JS function from a LiveCode button and get the same results so I can make use of the data within the regular LC environment. Does anyone have pointers or a sample stack so I can set off in the right direction? :)

k

Re: Calling JS in a browser object from LC?

Posted: Wed Oct 14, 2020 2:51 pm
by thatkeith
One step closer...

Code: Select all

do "get_current_view();" in widget "vrbrowser"
Calling the JS function by name with 'do' from a LC button runs the JavaScript within the browser widget, which updates the HTML display to show the values, as if I'd clicked a regular HTML button in the web page itself. But how do I run the JS and get those values BACK?

k

Re: Calling JS in a browser object from LC?

Posted: Wed Oct 14, 2020 3:26 pm
by thatkeith
Crude solution:

Code: Select all

   do "get_current_view();" in widget "browser"
   put the htmlText of widget "browser" into tHTML
And then slice and dice the returned text using offset, etc. From my earlier reading I had thought that the htmlText of a browser widget would be the original rendered code, not including what's altered by the JavaScript. Fortunately that's not the case.

This works, although I'm certain it's far from the most elegant approach. Even after all these years – HyperCard since 1989, then SuperCard, then AppKit/Rev/LiveCode – I feel like a JS newbie. (What can I say? I prefer xTalk!) So if someone was able to tell me how to change the JS (quoted in the first post above) so it returns the values to LC rather than injecting them into the HTML... I would be delighted. :)

k

Re: Calling JS in a browser object from LC?

Posted: Wed Oct 14, 2020 3:49 pm
by Thierry
thatkeith wrote:
Wed Oct 14, 2020 3:26 pm
I would be delighted. :)

Hi Keith,

Are you aware of this command: javascriptHandlers

Please, check in the dictionary.

HTH,

Thierry

Re: Calling JS in a browser object from LC?

Posted: Wed Oct 14, 2020 4:00 pm
by thatkeith
Hi Thierry,

I did look at that, but it seemed to be the wrong way around for my requirements. Isn't this a way to have a JS *call* be passed to a LC script, making LC do the work instead of the JS? What I need to do is use that JS function to find the view parameters of the panorama shown in the browser widget. This works well as JS but I'm really not sure that's possible to do from a LC script!

Have I missed something basic here? It would be far from the first time. :D

k

Re: Calling JS in a browser object from LC?

Posted: Wed Oct 14, 2020 5:44 pm
by Thierry
thatkeith wrote:
Wed Oct 14, 2020 4:00 pm
Hi Thierry,
Have I missed something basic here? It would be far from the first time. :D
I know this feeling as well :)

Here is a small stack as a demonstration...
All the code is in the card script.

HTH,

Thierry

Re: Calling JS in a browser object from LC?

Posted: Fri Oct 16, 2020 8:47 pm
by thatkeith
Thanks Thierry! Interesting and helpful stuff. :)