Domanda

I am trying to print name of nodes from DOM with WebKitGTK with the following code:

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

And I have the following webpage:

<!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>

And the output of my program is:

tag: HTML
tag: HEAD
tag: BODY

I don't understand why it output that, the output should be:

tag: HTML
tag: HEAD
tag: BODY
tag: VIDEO
tag: SOURCE
tag: VIDEO
tag: SOURCE
tag: INPUT

Anyone know what's happend?

Thanks

Edit: My complete code

#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;
}
È stato utile?

Soluzione

Your code is correct, but the timing is wrong. You have to wait until the page loading is finished.

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

Have a look at "Webkit GTK :: How to detect when a download has finished?".

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top