JavaScriptCore in WebKit2Gtk with C?

2.2k Views Asked by At

I am having trouble getting the JavaScriptCore to work with WebKit2Gtk. In my C application I am using WebKit2Gtk as a frontend for my application which I want to be able to access C functions.

There are a couple of examples with WebKitGtk (not the WebKit2 version). However in the mailing lists there are some information that given the threaded nature of WebKit2Gtk, it is more complicated. Unfortunately I have not found any example.

I wrote this code in the load callback:

JSGlobalContextRef jsContext = webkit_web_view_get_javascript_global_context
                                           (WEBKIT_WEB_VIEW(gtk_webview));

JSEvaluateScript(jsContext,JSStringCreateWithUTF8CString
                      ("$('#debuginfo').html('JS API OK')"),0,0,0,0);

Unfortunately absolutely nothing happens in WebKit2Gtk. However if I use the new function defined in the API docs and execute the following, I get the output as desired.

webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(gtk_webview),
                "$('#debuginfo').html('JS API OK')",NULL,NULL,NULL);

The new API with the run-javascript-function is ok to quickly execute JS-code, but I want to allow the JS code inside WebKit2Gtk to execute functions from JS that I can handle in my C code.

Looking forward to your help as I am kind of stuck with this thing and before reverting back to WebKitGtk (not the WebKit2Gtk version, but the previous), I quickly want to check if you guys have any idea on how to achieve it.

1

There are 1 best solutions below

0
On BEST ANSWER

WebKitGTK version 1 was single-process which allowed you to listen to the window-object-cleared signal and get the global javascript context and add your javascript object to the global context.

WebKitGTK version 2 is multi-process and so getting the global javascript context from the same process that you created the WebKitWebView object on will no longer work since the javascript is evaluated in another process. In order to extend Javascript you have to create a WebKitWebExtension (shared library) that your WebKitWebView instance loads.

The likely reason for why it is working in your case when you call webkit_web_view_run_javascript is that your javascript object is only being created and evaluated in the current process.