gpt4 book ai didi

c - Webkit GTK::如何检测下载何时完成?

转载 作者:太空狗 更新时间:2023-10-29 16:09:26 25 4
gpt4 key购买 nike

我想知道是否有办法知道 webkit 下载何时完成且没有错误。我正在使用 C API。我希望会发出一个信号(类似于下载开始时),但事实似乎并非如此。

搜索 webkit API 索引,我看不到任何相关的信号: http://webkitgtk.org/reference/index-all.html

我真的必须轮询每个 webkit 下载直到它完成吗?

干杯

最佳答案

下载完成后会发出一个信号。信号是WebKitWebView“load-finished”,你也可以在对象WebKitWebView中监听“load-status”的属性变化。

以下 C 代码向您展示了如何:

  #include <gtk/gtk.h>
#include <webkit/webkit.h>

static void destroy_cb(GtkWidget* widget, gpointer data) {
gtk_main_quit();
}

static void load_finished_cb(WebKitWebView *web_view, WebKitWebFrame *web_frame, gpointer data) {
printf("Finished downloading %s\n", webkit_web_view_get_uri(web_view));
}

static void load_status_cb(GObject* object, GParamSpec* pspec, gpointer data) {
WebKitWebView *web_view;
WebKitLoadStatus status;
const gchar *uri;

web_view = WEBKIT_WEB_VIEW(object);
status = webkit_web_view_get_load_status(web_view);
uri = webkit_web_view_get_uri(web_view);

switch (status) {
case WEBKIT_LOAD_PROVISIONAL:
printf("Load provisional: %s\n", uri);
break;
case WEBKIT_LOAD_COMMITTED:
printf("Load commited: %s\n", uri);
break;
case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
printf("Load first visually non empty layout: %s\n", uri);
break;
case WEBKIT_LOAD_FINISHED:
printf("Load finished: %s\n", uri);
break;
default:
g_assert_not_reached();
}
}

int main(int argc, char* argv[]) {
const char *uri;
GtkWidget* window;
WebKitWebView* web_view;

gtk_init(&argc, &argv);

if (argc == 1) {
printf("Usage: URI\n");
return 1;
}
uri = argv[1];

if(!g_thread_supported())
g_thread_init(NULL);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
g_signal_connect(window, "destroy", G_CALLBACK(destroy_cb), NULL);

web_view = web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
webkit_web_view_set_transparent(web_view, TRUE);

/* Register a callback that gets invoked each time that a page is finished downloading */
g_signal_connect(web_view, "load-finished", G_CALLBACK(load_finished_cb), NULL);

/* Register a callback that gets invoked each time that the load status changes */
g_object_connect(web_view, "signal::notify::load-status", G_CALLBACK(load_status_cb), NULL);

webkit_web_view_load_uri(web_view, uri);

gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(web_view));
gtk_widget_grab_focus(GTK_WIDGET(web_view));
gtk_widget_show_all(window);
gtk_main();
return 0;
}

关于c - Webkit GTK::如何检测下载何时完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5724088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com