wxWebView call C++ function from javascript

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
gridow
In need of some credit
In need of some credit
Posts: 1
Joined: Tue Dec 22, 2015 6:15 am

wxWebView call C++ function from javascript

Post by gridow » Tue Dec 22, 2015 6:23 am

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

DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 223
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxWebView call C++ function from javascript

Post by DenDev » Tue Dec 22, 2015 2:13 pm

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 :-)
I have a bad habbit of not testing the code I post :D

Anil8753
Experienced Solver
Experienced Solver
Posts: 80
Joined: Sat Jan 16, 2016 5:57 am

Re: wxWebView call C++ function from javascript

Post by Anil8753 » Thu Nov 15, 2018 1:40 pm

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++?

Natulux
I live to help wx-kind
I live to help wx-kind
Posts: 154
Joined: Thu Aug 03, 2017 12:20 pm

Re: wxWebView call C++ function from javascript

Post by Natulux » Tue Nov 27, 2018 2:43 pm

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

senj
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sat Mar 21, 2009 10:46 pm

Re: wxWebView call C++ function from javascript

Post by senj » Mon Jan 28, 2019 11:36 am

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).

User avatar
doublemax
Moderator
Moderator
Posts: 12996
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWebView call C++ function from javascript

Post by doublemax » Mon Jan 28, 2019 5:02 pm

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.
Use the source, Luke!

Post Reply