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 »

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: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxWebView call C++ function from javascript

Post 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 :-)
I have a bad habbit of not testing the code I post :D
Anil8753
Experienced Solver
Experienced Solver
Posts: 93
Joined: Sat Jan 16, 2016 5:57 am
Location: India

Re: wxWebView call C++ function from javascript

Post 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++?
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: wxWebView call C++ function from javascript

Post 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
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 »

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: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWebView call C++ function from javascript

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