【问题标题】:How to handle a custom URL scheme in Webkit GTK?如何在 Webkit GTK 中处理自定义 URL 方案?
【发布时间】:2011-03-15 09:17:51
【问题描述】:

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

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 方案注册一个处理程序。这可能吗?

【问题讨论】:

    标签: webkit gtk url-scheme


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这是我需要的正确方向的指针。为了完整起见,答案是连接到 webview 的 resource-request-starting 信号,并在该处理程序中使用 webkit_web_resource_get_uri() 进行操作。 (请注意,这只适用于 webkit >= 1.1.14。)
    【解决方案2】:

    In WebKit GTK 2, there is a more official route for this:

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

    【讨论】:

    • 这是一个更好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多