2010-07-13 69 views
10

比方说,我想在GTK中使用WebKitWebView来显示一些静态HTML页面。这些页面使用自定义URL方案,我们称之为custom://。这个方案代表了一个本地文件,在生成HTML时,它的位置并不是事先知道的。我要做的就是连接到网页视图的navigation-requested信号,而做到这一点:如何处理Webkit GTK中的自定义URL方案?

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

回答

5

我更熟悉WebKit的Chromium端口,但我相信您可能需要使用webkit_web_resource_get_uri(请参阅webkitwebresource.h)来处理资源(如图像)。

+3

谢谢,这是我需要正确的方向指针。为了完整起见,答案是连接到webview的'resource-request-starting'信号,并使用该处理程序中的'webkit_web_resource_get_uri()'进行操作。 (请注意,这仅适用于webkit> = 1.1.14。) – ptomato 2010-07-18 13:48:50

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

这是一个更好的答案。 – clee 2018-02-20 06:37:58