Page 1 of 1

wxWebView call C++ function from javascript

Posted: Tue Dec 22, 2015 6:23 am
by gridow
I am trying to use HTML user interface by wxWebView,

and i know that we can use the RunScript to call javascript from C++

but how do I call C++ function from javascript and pass some parameter from javascript to C++

Thanks

Re: wxWebView call C++ function from javascript

Posted: Tue Dec 22, 2015 2:13 pm
by DenDev
I'm not sure if that is possible because it would yield a security risk. You can however do some other trickery with uri's. If you from JavaScript navigate to uri: "cfunc://func_name?arg1&arg2&arg3" then you can assign a "EVT_WEBVIEW_NAVIGATING" handler to the wxWebView where you can examine uri's prior to load. If the uri starts with "cfunc://" then you can veto the navigate event and call the C-function "func_name" with the arguments in the uri's query string. Easy to implements and secure. That's how I'd do it :-)

Re: wxWebView call C++ function from javascript

Posted: Thu Nov 15, 2018 1:40 pm
by Anil8753
This works well if data is less. If data is more, in KBs or MBs then max url length causes the limitations.
Is there any easy way to pass the large data from Javascript to C++?

Re: wxWebView call C++ function from javascript

Posted: Tue Nov 27, 2018 2:43 pm
by Natulux
As far as I know, this is not possible. The webview is merely a browser, which you can use to browse the web. You don't want the web to be able to execute your C++ code.
But in wxWidgets 3.1.x, RunScript can return a string value. So you could for example use a wxTimer to check for changes (or the URI, as DenDev said) and RunScript a JS-Function to return the text.

Or if you want to get fancy: Send a Http-REST-request from JS to localhost and catch it with your own app with a wxSocketServer.

Best
Natu

Re: wxWebView call C++ function from javascript

Posted: Mon Jan 28, 2019 11:36 am
by senj
Using wxWidgets 3.1 you could do window.open in javascript and then catch that event (EVT_WEBVIEW_NEWWINDOW), inspect URL and conditionally fetch content from javascript. I am using hidden textarea html element and then run wxWebView->RunScript to obtain it's content - there's an example in documentation (for textarea you need to do document.getElementById().value).

Re: wxWebView call C++ function from javascript

Posted: Mon Jan 28, 2019 5:02 pm
by doublemax
senj wrote:Using wxWidgets 3.1 you could do window.open in javascript and then catch that event (EVT_WEBVIEW_NEWWINDOW), inspect URL and conditionally fetch content from javascript. I am using hidden textarea html element and then run wxWebView->RunScript to obtain it's content - there's an example in documentation (for textarea you need to do document.getElementById().value).
That's a very nice idea =D>
I shall remember that.