[webkit-gtk] Rendering HTML from JavaScript

Carlos Garcia Campos cgarcia at igalia.com
Thu Oct 30 00:05:35 PDT 2014

El mié, 29-10-2014 a las 23:33 +0200, Anatoly Zaretsky escribió:
> Hello!
> I am migrating a WebKit1-based application to the WebKit2 API. The
> application
> used the WebKitWebView's "window-object-cleared" signal and the
> JavaScriptCore
> C API to extend the window object with the function
>     void renderHTML(String content, String baseURI, Callback
> contentRendered)
> When called the function would:
> - create a new WebKitWebView;
> - place it inside a newly created GtkOffscreenWindow;
> - load the supplied content with webkit_web_view_load_string;
> - subscribe to the view's "notify::load-status" signal;
> - get the pixbuf from the GtkOffscreenWindow on WEBKIT_LOAD_FINISHED;
> - finally, invoke the contentRendered callback passing it the contents
> of the extracted pixbuf (particular pixbuf representation used is not
> of interest here).
> Is it possible to do the same with WebKit2? In particular can a web
> process extension use webkit_web_view_new/webkit_web_view_load_html?

No, WebView is the UI process API.

> The reference manual does not explicitly forbid doing so, though
> including
> both <webkit2/webkit2.h> and <webkit2/webkit-web-extension.h> in the
> same source forces compilation error.
> Also, are there other ways to programmatically convert some arbitrary
> html text into an image?

If I understood correctly what you want, I think it would be something

In the web extension you connect to the window-object-cleared signal,
and with the exactly same code you currently has extends the window
object to add renderHTML. In the callback you would need to send a
message to the UI process, using DBus or any other IPC mechanism you

In the UI process, when the message is received you create a WebView,
connects to load-changed, and loads the HTML using
webkit_web_view_load_html. In the load-changed signal when the load
event is finished, you can call webkit_web_view_get_snpashot to
asynchronously get a cairo image surface with the web view contents
rendered. Now you need to send another message to the web extension with
the results.

I know it's a bit more complex, because JavaScript is in the WebProcess.
We are about to land a patch that would simplify this, you wouldn't need
to extend the window object nor send an IPC message to the UI process.
Your JavaScript code could do something like
window.webkit.messageHandlers.renderer.postMessage() and the message
would be received in the UI process as a signal with a
WebKitJavaScriptResult object to deserialize.
See https://bugs.webkit.org/show_bug.cgi?id=133730

> --
> Tolik
> _______________________________________________
> webkit-gtk mailing list
> webkit-gtk at lists.webkit.org
> https://lists.webkit.org/mailman/listinfo/webkit-gtk

Carlos Garcia Campos
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: This is a digitally signed message part
URL: <https://lists.webkit.org/pipermail/webkit-gtk/attachments/20141030/8aad0673/attachment.sig>

More information about the webkit-gtk mailing list