11

假设我想WebKitWebView在 GTK 中使用 a 来显示一些静态 HTML 页面。这些页面使用自定义 URL 方案,我们称之为custom://. 此方案表示在生成 HTML 时事先不知道其位置的本地文件。我要做的是连接到navigation-requestedwebview 的信号,然后这样做:

const gchar *uri = webkit_network_request_get_uri(request);
gchar *scheme = g_uri_parse_scheme(uri); 

if(strcmp(scheme, "custom") == 0) {
    /* DO FILE LOCATING MAGIC HERE */
    webkit_web_view_open(webview, real_location_of_file);
    return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
}
/* etc. */

这似乎工作正常,除非该方案在<img>标签中使用,例如:<img src="custom://myfile.png">,显然这些不通过navigation-requested信号。

在我看来,应该有某种方法可以使用 Webkit 为自定义 URL 方案注册处理程序。这可能吗?

4

2 回答 2

5

我对 WebKit 的 Chromium 端口比较熟悉,但我相信您可能需要使用webkit_web_resource_get_uri(参见webkitwebresource.h)来处理图像等资源。

于 2010-07-13T16:43:57.707 回答
3

在 WebKit GTK 2 中,有一个更官方的路线:

WebKitWebContext *context = webkit_web_context_get_default();
webkit_web_context_register_uri_scheme(context, "custom",
    (WebKitURISchemeRequestCallback)handle_custom,
    NULL, NULL);

/* ... */

static void
handle_custom(WebKitURISchemeRequest *request)
{
    /* DO FILE LOCATING MAGIC HERE */
    GFile *file = g_file_new_for_path(real_location_of_file);
    GFileInputStream *stream = g_file_read(file, NULL, NULL);
    g_object_unref(file);

    webkit_uri_scheme_request_finish(request, stream, -1, NULL);
    g_object_unref(stream);
}
于 2014-12-21T05:27:04.140 回答