Calling JS in a browser object from LC?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 346
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Calling JS in a browser object from LC?

Post by thatkeith » Wed Oct 14, 2020 1:57 pm

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
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 346
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Calling JS in a browser object from LC?

Post by thatkeith » Wed Oct 14, 2020 2:51 pm

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
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 346
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Calling JS in a browser object from LC?

Post by thatkeith » Wed Oct 14, 2020 3:26 pm

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
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Calling JS in a browser object from LC?

Post by Thierry » Wed Oct 14, 2020 3:49 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 346
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Calling JS in a browser object from LC?

Post by thatkeith » Wed Oct 14, 2020 4:00 pm

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
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Calling JS in a browser object from LC?

Post by Thierry » Wed Oct 14, 2020 5:44 pm

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
Attachments
thierry4keith.livecode.zip
basic exchange LC - JS
(2.3 KiB) Downloaded 345 times
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 346
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Calling JS in a browser object from LC?

Post by thatkeith » Fri Oct 16, 2020 8:47 pm

Thanks Thierry! Interesting and helpful stuff. :)
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

Post Reply

Return to “Internet”