代码之家  ›  专栏  ›  技术社区  ›  ptomato

如何在Webkit GTK中处理自定义URL方案?

  •  10
  • ptomato  · 技术社区  · 14 年前

    WebKitWebView 在GTK中显示一些静态HTML页面。这些页面使用自定义的URL方案,我们称之为 custom:// . 这个方案表示一个本地文件,它的位置在生成HTML时是未知的。我要做的是连接到 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"> ,显然这些没有通过 信号。

    在我看来,应该有一些方法来注册一个处理程序的自定义网址方案与Webkit。这可能吗?

    2 回复  |  直到 14 年前
        1
  •  5
  •   Emerick Rogul    13 年前

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

        2
  •  3
  •   ptomato    10 年前

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