我正在尝试使用以下代码使用 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;
}