Pass values to and get result from JavaScript

Just starting out? Need help? Post your questions and find answers here.
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Pass values to and get result from JavaScript

Post by QuimV »

Hi,

I'm looking for the best way, fast and reliable, to pass values (numbers and strings) to a JavaScript piece of code and get the result of this code back in PB.

Simple example code:

Code: Select all

var x = 16;                // In PB we assign var x to value 16
.....                      // Pass this value to Javascript
var y = Math.sqrt(x);      // In JS calculate the square root of 16
.....                      // Pass this result to PB
y = 4                      // 4 is returned to PB
What is the best way? WebGadget, Comateplus, other solutions?

Thanks in advanced.
QuimV
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Pass values to and get result from JavaScript

Post by IdeasVacuum »

Have you tried SpiderBasic, PureBasic's sister?
http://www.spiderbasic.com/
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: Pass values to and get result from JavaScript

Post by Justin »

If you search Windows Script Host there is some old code in the forums. I was working with this the past days and did an updated version even passing PB objects to js but the code still need some cleaning i will post it when done altough i dont have much time.
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Re: Pass values to and get result from JavaScript

Post by QuimV »

:( I need to deal with javascript, not with Windows Script Host.
Thanks for the idea

In fact, pass values (number, strings, etc.) from PB to javascript using a webgadget is quite easy.
Return results from JS to PB is quite more difficult for me. Any idea?

Thanks in advanced
QuimV
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Pass values to and get result from JavaScript

Post by helpy »

QuimV wrote:I'm looking for the best way, fast and reliable, to pass values (numbers and strings) to a JavaScript piece of code and get the result of this code back in PB.
QuimV wrote::( I need to deal with javascript, ...
It is not possible to pass arguments from PB to JavaScript directly!
You need a "JavaScript engine" to execute the JavaScript.

Actually you need to pass an argument and the JavaScript to the "JavaScript engine".
Therefor it seems the best way to use WebGadget because the embedded browser uses its own "JavaScript engine"!


Why do you want to interact with a JavaScript? Is this necessary for your project? Why?

Do you want to give the user of your application the possibility to write additional code with JavaScript?
In this case I would search for a JavaScript engine, which could be embedded directly in to your application.
... or search for other scripting enginge like "LUA" ...

See also:
==> http://www.purebasic.fr/english/viewtop ... lua+script
==> http://www.purebasic.fr/english/viewtop ... lua+script
==> Embed JS Framework into PureBasic ==> http://www.purebasic.fr/english/viewtop ... 12&t=63652
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Pass values to and get result from JavaScript

Post by falsam »

Hello QuimV.

The JavaScript code is integrated into the PureBasic code or on a server?

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Pass values to and get result from JavaScript

Post by falsam »

Small test

Code: Select all

Enumeration Window
  #mainForm
EndEnumeration

Enumeration Gadget
  #HideWeb
EndEnumeration

Global HTML.s

OpenWindow(#mainForm, 88, 244, 500, 200, "PB <==> JavasScript", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget (#HideWeb, 0, 0, 0, 0, "")

Procedure.s sqrt(n)
  HTML = "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
  HTML + "<script>"
  HTML + "window.clipboardData.setData( 'Text', Math.sqrt(v_n).toString() );"
  HTML + "</script>"
  
  HTML = ReplaceString(HTML, "v_n", Str(n))
  SetGadgetItemText(#HideWeb, #PB_Web_HtmlCode , HTML)

  Delay(100)

  ProcedureReturn GetClipboardText()
EndProcedure

Debug sqrt(16)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by falsam on Tue Sep 13, 2016 10:01 pm, edited 1 time in total.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Re: Pass values to and get result from JavaScript

Post by QuimV »

:D Thanks a lot balsam

This is a very good idea!

Merci beaucoup encore une fois!
QuimV
User avatar
JHPJHP
Addict
Addict
Posts: 2258
Joined: Sat Oct 09, 2010 3:47 am

Re: Pass values to and get result from JavaScript

Post by JHPJHP »

Hi QuimV,

The solution used in Embed JS Framework into PureBasic was to pass data to document.title and retrieve it using GetGadgetItemText(0, #PB_Web_PageTitle).

The solution provided by falsam is very good and will work in most situations, but can be affected by the following:
- an additional "set clipboard" takes place between your scripted "get clipboard" and "set clipboard"
- Internet Options / Security / Local intranet / ... has been set to Disable or Prompt:

Image

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Re: Pass values to and get result from JavaScript

Post by QuimV »

:D Hi JHPJHP,

Thank you very much for your detailed explanation.
QuimV
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Pass values to and get result from JavaScript

Post by falsam »

JHPJHP wrote:The solution used in Embed JS Framework into PureBasic was to pass data to document.title and retrieve it using GetGadgetItemText(0, #PB_Web_PageTitle).
Yes, this is a good solution.

New code.

Code: Select all

Enumeration Window
  #mainForm
EndEnumeration

Enumeration Gadget
  #HideWeb
EndEnumeration

Global HTML.s

OpenWindow(#mainForm, 88, 244, 500, 200, "PB <==> JavasScript", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget (#HideWeb, 0, 0, 0, 0, "")

Procedure.s sqrt(n)
  HTML = "<html>"
  HTML + "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
  HTML + "<body>"
  
  HTML + "<script>"
  HTML + "result = Math.sqrt(v_n).toString();"
  HTML + "document.title = result;"
  HTML + "</script>"
  
  HTML + "</body>"
  HTML + "</html>"
  
  HTML = ReplaceString(HTML, "v_n", Str(n))
  SetGadgetItemText(#HideWeb, #PB_Web_HtmlCode , HTML)
  
  Delay(200) 
EndProcedure

sqrt(16)
Debug GetGadgetItemText(#HideWeb, #PB_Web_PageTitle)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
JHPJHP
Addict
Addict
Posts: 2258
Joined: Sat Oct 09, 2010 3:47 am

Re: Pass values to and get result from JavaScript

Post by JHPJHP »

Hi falsam,

Replace Delay(200) with the following:
- Delay(...) won't always catch the return value and is slower

Code: Select all

Repeat
  PageTitle.s = GetGadgetItemText(#HideWeb, #PB_Web_PageTitle)

  If FindString(GetGadgetItemText(#HideWeb, #PB_Web_HtmlCode), HTML) And PageTitle <> "" : Break : EndIf : WindowEvent() : Delay(1)

ForEver
Last edited by JHPJHP on Tue Sep 13, 2016 10:40 pm, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Pass values to and get result from JavaScript

Post by falsam »

Like this ?

Code: Select all

Enumeration Window
  #mainForm
EndEnumeration

Enumeration Gadget
  #HideWeb
EndEnumeration

Global HTML.s

OpenWindow(#mainForm, 88, 244, 500, 200, "PB <==> JavasScript", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget (#HideWeb, 0, 0, 0, 0, "")

Procedure.s sqrt(n)
  HTML = "<html>"
  HTML + "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
  HTML + "<body>"
  
  HTML + "<script>"
  HTML + "result = Math.sqrt(v_n).toString();"
  HTML + "document.title = result;"
  HTML + "</script>"
  
  HTML + "</body>"
  HTML + "</html>"
  
  HTML = ReplaceString(HTML, "v_n", Str(n))
  SetGadgetItemText(#HideWeb, #PB_Web_HtmlCode , HTML)
  
  Repeat
    PageTitle.s = GetGadgetItemText(#HideWeb, #PB_Web_PageTitle)
    If FindString(GetGadgetItemText(#HideWeb, #PB_Web_HtmlCode), HTML) And PageTitle <> "" : Break : EndIf : WindowEvent() : Delay(1)
  ForEver 
  
  ProcedureReturn GetGadgetItemText(#HideWeb, #PB_Web_PageTitle)
EndProcedure

Debug sqrt(16)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
JHPJHP
Addict
Addict
Posts: 2258
Joined: Sat Oct 09, 2010 3:47 am

Re: Pass values to and get result from JavaScript

Post by JHPJHP »

Hi falsam,

Your example is working great; with all the available JavaScript the possibilities are endless.

It's surprising how much data can be stored in the title; I've returned a Base64 encoded image using this method.
Last edited by JHPJHP on Tue Sep 13, 2016 11:00 pm, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Pass values to and get result from JavaScript

Post by falsam »

Thank for your help JHPJHP ^^

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
Post Reply