WebKit2GTK+ "window-object-cleared" 未清除

WebKit2GTK+ "window-object-cleared" not clearing

这是对以下内容的跟进:

这是我的分机:

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

static void window_object_cleared_callback(WebKitScriptWorld* world, WebKitWebPage* webPage, WebKitFrame* frame, gpointer userData)
{
  std::cout << "Callback reached\n";

  JSCContext *jsCtx = webkit_frame_get_js_context_for_script_world(frame, world);
  JSCValue* title = jsc_context_evaluate(jsCtx, "document.title;", -1);

  std::cout << "Title: " << jsc_value_to_string(title) << "\n";
}

static void web_page_created_callback(WebKitWebExtension* extension, WebKitWebPage* webPage, gpointer UserData)
{
  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);
  g_signal_connect(webkit_script_world_get_default(), "window-object-cleared", G_CALLBACK(window_object_cleared_callback), NULL);
}

唯一打印到控制台的是 "Page 1 created for (null)"。当没有创建任何内容时,将调用创建页面的回调,winow-object-cleared 是否相同?

我不知道为什么 window_object_cleared 对象现在显示出工作的迹象。

这些是我在信号中发现的:https://webkitgtk.org/reference/webkit2gtk/stable/WebKitScriptWorld.html#WebKitScriptWorld-window-object-cleared

window-object-cleared 回调是一种注入 javascript 的方式。我认为它只有在你至少添加 "some content" 到 WebKitWebView 时才会执行。您应该调用 load_uri 或类似的函数。

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

static void js_handler(WebKitWebPage* webPage)
{
  JSCContext *jsCtx = webkit_frame_get_js_context(webkit_web_page_get_main_frame(webPage));
  JSCValue* title = jsc_context_evaluate(jsCtx, "document.title;", -1);

  std::cout << "Title: " << jsc_value_to_string(title) << "\n";
}

static void document_loaded_callback(WebKitWebPage* webPage, gpointer userData)
{
  g_print("Page %lu created for %s\n", webkit_web_page_get_id(webPage), webkit_web_page_get_uri(webPage));
  js_handler(webPage);
}

static void web_page_created_callback(WebKitWebExtension* extension, WebKitWebPage* webPage, gpointer userData)
{
  g_signal_connect(webPage, "document-loaded", G_CALLBACK(document_loaded_callback), NULL);
}

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);
}

我想解决方案是使用 webkit 框架获取 JSCContext,并使用 "document-loaded" 的信号而不是 "window-object-cleared"