WebKit2GTK+ "page-created" extension only working once

435 views Asked by At

This is a follow-up question from How to debug WebKit2GTK+ extensions. While there is more code there for reference, I have changed it up a little.

I have the following webkit extension:

#include <webkit2/webkit-web-extension.h>
#include <iostream>

static void web_page_created_callback(WebKitWebExtension* extension, WebKitWebPage* webPage, gpointer userData)
{
  std::cout << "Page " << webkit_web_page_get_id(webPage) << " created for " << webkit_web_page_get_uri(webPage) << "\n";
  g_print("Page %lu created for %s\n", webkit_web_page_get_id(webPage), webkit_web_page_get_uri(webPage));
}

extern "C" G_MODULE_EXPORT void webkit_web_extension_initialize(WebKitWebExtension* extension)
{
  g_signal_connect(extension, "page-created", G_CALLBACK(web_page_created_callback), NULL);
}

According to https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebExtension.html and https://blogs.igalia.com/carlosgc/2013/09/10/webkit2gtk-web-process-extensions/, this should print out a message everytime a page is created. I am using the implementation of the extension the same way as the webkit reference doc, where it is a callback for "initialize-web-extension".

I do not know if this makes a difference, but my WebKitWebView object is packed in a GTKWidget box object. This is the output I get:

Page 1 created for Page 1 created for (null)

And this never gets printed again, even when I open a new file (I have a file choose dialog that opens up a file, that's why the WebKitWebView is packed into a GTK_BOX).

Here is what the WebKit2GTK docs say for the "page-created" handler:

void user_function (WebKitWebExtension *extension, WebKitWebPage *web_page, gpointer user_data)

This signal is emitted when a new WebKitWebPage is created in the Web Process. Parameters

extension: the WebKitWebExtension on which the signal is emitted

web_page: the WebKitWebPage created

user_data: user data set when the signal handler was connected.

Flags: Run Last

If this gives the WebPage created, then why can't I get the uri with webkit_web_page_get_uri()? Why does this only give an output (albeit messed up) for only the first page that is loaded?

Edit: I am loading the page with webkit_web_view_load_uri()

2

There are 2 answers

0
lb90 On BEST ANSWER

I have just touched the surface of WebKitGtk and I'm not much knowledgeable. But to my understanding, WebKitWebPage (the name is misleading) just reflects instances of WebKitWebView widgets, it's not the web site you are currently viewing.

Anytime the application creates a new WebKitWebView widget, extensions get notified by the "page-created" signal, and a WebKitWebPage object is passed for this new widget.

For example, for tabbed browsing, you create a WebKitWebView widget for each tab. Then "page-created" is emitted on the extension upon creation of a new tab

If you want to know when the web site (technically: uri) has changed you can do:

#include <webkit2/webkit-web-extension.h>

static void
on_uri_changed (WebKitWebPage *web_page,
                GParamSpec *pspec,
                gpointer user_data)
{
  g_print ("changed uri: %s\n", webkit_web_page_get_uri (web_page));
}

static void
web_page_created_callback (WebKitWebExtension *extension,
                           WebKitWebPage      *web_page,
                           gpointer            user_data)
{
    g_signal_connect (web_page,
                      "notify::uri",
                      G_CALLBACK (on_uri_changed),
                      NULL);
    g_print ("created new web_view widget with id %d created\n",
             webkit_web_page_get_id (web_page));
}

G_MODULE_EXPORT void
webkit_web_extension_initialize (WebKitWebExtension *extension)
{
    g_signal_connect (extension, "page-created", 
                      G_CALLBACK (web_page_created_callback), 
                      NULL);
}
1
lb90 On

Are you using webkit_web_view_load_uri to change the web page?