C ++ call from JavaScript / HTML using GTK + Webkit Webview (and Qt WebKit bridge)

I am currently evaluating various approaches / solutions for calling C ++ functions from JavaScript code embedded in an HTML page. The application must be running on the Ubuntu 12.04 LTS server.

I am not an expert in Linux development. This is the first time I'm trying to work on something to interact between JavaScript and C ++ code.

C ++ code is mainly located in .so files (dynamic libraries) that provide interface methods for accessing certain equipment and the file system. The GUI should be in HTML, and I'm looking for various possible solutions that C ++ can call from HTML. I searched and decided to solve two approaches using the WebKit mechanism.

Approach 1: Using Qt 5.0.2 Webkit Bridge - A WebView Control
The GUI framework will be based on Qt, the main application window will contain a webview control that will run HTML code, which in turn will contain JavaScript code.
The interface between the web page and C ++ is done using the addToJavaScriptWindowObject () function.

I created a sample application and tested this solution and it works fine.

Approach 2: Using GTK + WebKit WebView
I downloaded and installed the GTK 3.0 library.
I got webkitgtk 2.0.1 and installed it.
I created a test application with GTK without webkit, it works well.
I am trying to create webkit web control using GTK.

When I tried my approach 1 with Qt, there was a good enough set of documentation and samples to do what I wanted. But starting from approach 2, using GTK +, I feel like I'm moving slower compared. I personally think that part of the documentation is not so straightforward for the application I'm trying to develop.

Other approaches:
I also want to try to check whether you can use applets (directly access .so files) or use the JavaScript V8 engine for interaction between JavaScript and C ++ ( https://code.google.com/p/v8/ )

I tried the following resources:
http://webkitgtk.org/
http://www.webkit.org/
https://live.gnome.org/WebKitGtk/ProgrammingGuide/Tutorial

I want to know exactly how to make this part of the interface for calling C ++ functions (in .so files) when a button is clicked on an HTML web page containing JavaScript. What signal should I look for. If I use a WebkitWebview control, how do I display a button, click the C ++ button?

Can someone point me in the right direction?

I would really appreciate your time and knowledge.

Sincerely.

+4
source share
3 answers

If you need to use C ++ code or your own applications in your web application, you can try to create a service using C ++ code and access it through the REST API (for example).

You can use a common web application framework (Spring / Java, Django / Python, etc.) to develop your web application and use Apache Thrift to interact with your library.

+2
source

Webkit GTK 2 has changed significantly in terms of API. So I'm not sure if this will work with Webkit GTK 2. However, it will definitely work in versions of Webkit GTK 1. *. I don't know anything about QT.

For your need to connect an html view with a C / C ++ part of the world, you can use two approaches. Please take a look at the webkit_dom_event_target_add_event_listener function. Example: https://live.gnome.org/WebKitGtk/ProgrammingGuide/Cookbook

Another approach you can take is to use a warning when a button is pressed and send a string as information. On the C side, you can hook up an alert listener and parse the message and decide what needs to be done. I wrote a lot of code in python that takes this approach, since the above call is not exposed to python.

I agree that the documentation is a bit sparse for webkit gtk. However, if you know how you can learn something in javascript, you can usually map the control and the DOM control of javascript on the C side. This includes generating elements dynamically, managing events like onclick, etc. You just need to break through the header files and find the appropriate call.

+2
source

the best solution for you is g-xml, it is a good solution from GAMA, but it is a pity that it is not free.

0
source

Source: https://habr.com/ru/post/1480644/


All Articles