Page 1 of 2
Pass values to and get result from JavaScript
Posted: Mon Sep 12, 2016 9:40 am
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.
Re: Pass values to and get result from JavaScript
Posted: Mon Sep 12, 2016 2:56 pm
by IdeasVacuum
Have you tried SpiderBasic, PureBasic's sister?
http://www.spiderbasic.com/
Re: Pass values to and get result from JavaScript
Posted: Mon Sep 12, 2016 4:28 pm
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.
Re: Pass values to and get result from JavaScript
Posted: Mon Sep 12, 2016 7:07 pm
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
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 6:19 am
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
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 9:24 am
by falsam
Hello QuimV.
The JavaScript code is integrated into the PureBasic code or on a server?
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 11:13 am
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
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 7:55 pm
by QuimV

Thanks a lot balsam
This is a very good idea!
Merci beaucoup encore une fois!
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 9:22 pm
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:

Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 9:31 pm
by QuimV

Hi JHPJHP,
Thank you very much for your detailed explanation.
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 10:15 pm
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
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 10:26 pm
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
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 10:40 pm
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
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 10:47 pm
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.
Re: Pass values to and get result from JavaScript
Posted: Tue Sep 13, 2016 10:54 pm
by falsam
Thank for your help JHPJHP ^^