我正在尝试使用以下代码使用WebKitGTK从DOM打印节点名称:
WebKitDOMDocument *dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webView));
WebKitDOMNodeList* video = webkit_dom_element_get_elements_by_tag_name(dom, "*");
gulong element_count = webkit_dom_node_list_get_length(video);
int i = 0;
for (i = 0; i < element_count; i++)
{
WebKitDOMNode *element = webkit_dom_node_list_item(video, i);
printf("tag: %s\n",webkit_dom_node_get_node_name (element));
}
我有以下网页:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<video id="video" autoplay preload="none" width="320" height="240">
<source src="./movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
<video id="video2" width="320" height="240" autoplay preload="none">
<source src="./movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
<input type="hidden" id="prueba" name="finish" value="false">
</body>
</html>
我的程序输出是:
tag: HTML
tag: HEAD
tag: BODY
我不明白为什么输出那个,输出应该是:
tag: HTML
tag: HEAD
tag: BODY
tag: VIDEO
tag: SOURCE
tag: VIDEO
tag: SOURCE
tag: INPUT
任何人都知道发生了什么事?
由于
编辑:我的完整代码
#include <gtk/gtk.h>
#include <webkit/webkit.h>
void
on_window_destroy (GtkWidget *object, gpointer user_data)
{
gtk_main_quit();
}
int
main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "webkit.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
gtk_window_set_decorated(window, 0);
gtk_window_set_default_size(GTK_WINDOW(window), 670, 480);
gtk_window_move(window,0,0);
// create webkit
WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
WebKitWebView *webView1 = WEBKIT_WEB_VIEW(webkit_web_view_new());
GtkWidget *scrolledWindow = GTK_WIDGET (gtk_builder_get_object (builder, "scrolledwindow1"));
GtkWidget *scrolledWindow1 = GTK_WIDGET (gtk_builder_get_object (builder, "scrolledwindow2"));
//add webkit to scrolledwindows
gtk_container_add(GTK_CONTAINER(scrolledWindow), GTK_WIDGET(webView));
gtk_container_add(GTK_CONTAINER(scrolledWindow1), GTK_WIDGET(webView1));
// load page
webkit_web_view_load_uri(webView, "http://localhost/");
webkit_web_view_load_uri(webView1, "http://localhost/");
// set focus.
gtk_widget_grab_focus(GTK_WIDGET(webView1));
// show components
gtk_widget_show_all(window);
// get DOM
WebKitDOMDocument *dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(webView));
WebKitDOMNodeList* video = webkit_dom_document_get_elements_by_tag_name(dom, "*");
gulong element_count = webkit_dom_node_list_get_length(video);
int i = 0;
for (i = 0; i < element_count; i++)
{
WebKitDOMNode *element = webkit_dom_node_list_item(video, i);
printf("Elemento: %s\n",webkit_dom_node_get_node_name (element));
}
printf("La longitud es: %d\n", element_count);
gtk_main ();
gtk_builder_connect_signals (builder, NULL);
g_object_unref (G_OBJECT (builder));
return 0;
}
答案 0 :(得分:1)
您的代码是正确的,但时机错误。您必须等到页面加载完成。
static void load_status_cb(GObject* object, GParamSpec* pspec, gpointer data)
{
WebKitWebView *webView;
WebKitLoadStatus status;
webView = WEBKIT_WEB_VIEW(object);
status = webkit_web_view_get_load_status(webView);
if (status == WEBKIT_LOAD_FINISHED) {
// read the DOM after the page has finished loading
WebKitDOMDocument *dom = webkit_web_view_get_dom_document(webView);
WebKitDOMNodeList* video = webkit_dom_document_get_elements_by_tag_name(dom, "*");
gulong element_count = webkit_dom_node_list_get_length(video);
int i = 0;
for (i = 0; i < element_count; i++)
{
WebKitDOMNode *element = webkit_dom_node_list_item(video, i);
printf("Elemento: %s\n",webkit_dom_node_get_node_name (element));
}
}
}
int
main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "webkit.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
gtk_window_set_decorated(window, 0);
gtk_window_set_default_size(GTK_WINDOW(window), 670, 480);
gtk_window_move(window,0,0);
// create webkit
WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
WebKitWebView *webView1 = WEBKIT_WEB_VIEW(webkit_web_view_new());
GtkWidget *scrolledWindow = GTK_WIDGET (gtk_builder_get_object (builder, "scrolledwindow1"));
GtkWidget *scrolledWindow1 = GTK_WIDGET (gtk_builder_get_object (builder, "scrolledwindow2"));
//add webkit to scrolledwindows
gtk_container_add(GTK_CONTAINER(scrolledWindow), GTK_WIDGET(webView));
gtk_container_add(GTK_CONTAINER(scrolledWindow1), GTK_WIDGET(webView1));
// load page
webkit_web_view_load_uri(webView, "http://localhost/");
webkit_web_view_load_uri(webView1, "http://localhost/");
// set focus.
gtk_widget_grab_focus(GTK_WIDGET(webView1));
// show components
gtk_widget_show_all(window);
// connect callback to load-status change signal
g_object_connect(webView, "signal::notify::load-status", G_CALLBACK(load_status_cb), NULL);
gtk_main ();
gtk_builder_connect_signals (builder, NULL);
g_object_unref (G_OBJECT (builder));
return 0;
}
查看“Webkit GTK :: How to detect when a download has finished?”。