WebKit2WebExtension.WebExtension

g GObject.Object GObject.Object WebKit2WebExtension.WebExtension WebKit2WebExtension.WebExtension GObject.Object->WebKit2WebExtension.WebExtension

Subclasses:

None

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

get_page (page_id)

send_message_to_context (message, cancellable, callback, *user_data)

send_message_to_context_finish (result)

Virtual Methods

Inherited:

GObject.Object (7)

Properties

None

Signals

Inherited:

GObject.Object (1)

Name

Short Description

page-created

This signal is emitted when a new WebKit2WebExtension.WebPage is created in the Web Process.

user-message-received

This signal is emitted when a WebKit2WebExtension.UserMessage is received from the #WebKitWebContext corresponding to extension.

Fields

Inherited:

GObject.Object (1)

Name

Type

Access

Description

parent

GObject.Object

r

Class Details

class WebKit2WebExtension.WebExtension(**kwargs)
Bases:

GObject.Object

Abstract:

No

Structure:

WebKit2WebExtension.WebExtensionClass

Represents an extension of the WebProcess.

WebKit2WebExtension.WebExtension is a loadable module for the WebProcess. It allows you to execute code in the WebProcess and being able to use the DOM API, to change any request or to inject custom JavaScript code, for example.

To create a WebKit2WebExtension.WebExtension you should write a module with an initialization function that could be either webkit_web_extension_initialize() with prototype WebKit2WebExtension.WebExtensionInitializeFunction or webkit_web_extension_initialize_with_user_data() with prototype WebKit2WebExtension.WebExtensionInitializeWithUserDataFunction. This function has to be public and it has to use the #G_MODULE_EXPORT macro. It is called when the web process is initialized.

```c static void web_page_created_callback (WebKit2WebExtension.WebExtension *extension, WebKit2WebExtension.WebPage *web_page, object user_data) { g_print (“Page %d created for %s\n”, WebKit2WebExtension.WebPage.get_id (web_page), WebKit2WebExtension.WebPage.get_uri (web_page)); }

G_MODULE_EXPORT void webkit_web_extension_initialize (WebKit2WebExtension.WebExtension *extension) { g_signal_connect (extension, “page-created”, G_CALLBACK (web_page_created_callback), None); } ```

The previous piece of code shows a trivial example of an extension that notifies when a WebKit2WebExtension.WebPage is created.

WebKit has to know where it can find the created WebKit2WebExtension.WebExtension. To do so you should use the webkit_web_context_set_web_extensions_directory() function. The signal #WebKitWebContext::initialize-web-extensions is the recommended place to call it.

To provide the initialization data used by the webkit_web_extension_initialize_with_user_data() function, you have to call webkit_web_context_set_web_extensions_initialization_user_data() with the desired data as parameter. You can see an example of this in the following piece of code:

```c #define WEB_EXTENSIONS_DIRECTORY // …

static void initialize_web_extensions (WebKitWebContext *context, object user_data) { // Web Extensions get a different ID for each Web Process static guint32 unique_id = 0;

webkit_web_context_set_web_extensions_directory ( context, WEB_EXTENSIONS_DIRECTORY); webkit_web_context_set_web_extensions_initialization_user_data ( context, GLib.Variant.new_uint32 (unique_id++)); }

int main (int argc, str **argv) { g_signal_connect (webkit_web_context_get_default (), “initialize-web-extensions”, G_CALLBACK (initialize_web_extensions), None);

Gtk.Widget *view = webkit_web_view_new ();

// … } ```

get_page(page_id)
Parameters:

page_id (int) – the identifier of the WebKit2WebExtension.WebPage to get

Returns:

the WebKit2WebExtension.WebPage for the given page_id, or None if the identifier doesn’t correspond to an existing web page.

Return type:

WebKit2WebExtension.WebPage

Get the web page of the given page_id.

send_message_to_context(message, cancellable, callback, *user_data)
Parameters:

Send message to the #WebKitWebContext corresponding to self. If message is floating, it’s consumed.

If you don’t expect any reply, or you simply want to ignore it, you can pass None as calback. When the operation is finished, callback will be called. You can then call WebKit2WebExtension.WebExtension.send_message_to_context_finish() to get the message reply.

New in version 2.28.

send_message_to_context_finish(result)
Parameters:

result (Gio.AsyncResult) – a Gio.AsyncResult

Raises:

GLib.Error

Returns:

a WebKit2WebExtension.UserMessage with the reply or None in case of error.

Return type:

WebKit2WebExtension.UserMessage

Finish an asynchronous operation started with WebKit2WebExtension.WebExtension.send_message_to_context().

New in version 2.28.

Signal Details

WebKit2WebExtension.WebExtension.signals.page_created(web_extension, web_page)
Signal Name:

page-created

Flags:

RUN_LAST

Parameters:

This signal is emitted when a new WebKit2WebExtension.WebPage is created in the Web Process.

WebKit2WebExtension.WebExtension.signals.user_message_received(web_extension, message)
Signal Name:

user-message-received

Flags:

RUN_LAST

Parameters:

This signal is emitted when a WebKit2WebExtension.UserMessage is received from the #WebKitWebContext corresponding to extension. Messages sent by #WebKitWebContext are always broadcasted to all WebKit2WebExtension.WebExtension s and they can’t be replied to. Calling WebKit2WebExtension.UserMessage.send_reply() will do nothing.

New in version 2.28.