How to get the list of hyperlinks of a webpage in Webkit/Gtk?
In here a window is being created and a webpage is generated:
int main(int argc, char* argv[])
{
WebKitWebView *webView;
GtkWidget *main_window;
gtk_init(&argc, &argv);
if (!g_thread_supported())
g_thread_init(NULL);
#ifndef GTK_API_VERSION_2
disablePlugin("Shockwave Flash");
#endif
main_window = createWindow(&webView);
gchar *uri =(gchar*)(argc > 1 ? argv[1] : "http://www.google.com/");
gchar *fileURL = filenameToURL(uri);
webkit_web_view_load_uri(webView, fileURL ? fileURL : uri)开发者_运维技巧;
g_free(fileURL);
gtk_widget_grab_focus(GTK_WIDGET(webView));
gtk_widget_show_all(main_window);
gtk_main();
return 0;
}
And here is where a notification says that a page is being loaded:
void FrameLoaderClient::postProgressFinishedNotification()
{
WebKitWebView* webView = getViewFromFrame(m_frame);
WebKitWebViewPrivate* privateData = webView->priv;
if (!privateData->disposing)
g_signal_emit_by_name(webView, "load-finished", m_frame);
}
Now after the page is loaded, I want to get the list of focusable nodes such as hyperlinks, checkbox.
how can i do it?
What you want to look at is how to access the DOM from WebKit. WebKit has a webkit_web_view_get_dom_document()
(http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitwebview.html#webkit-web-view-get-dom-document) which returns an instance of WebKitDOMDocument
(https://live.gnome.org/WebKitGtk/ProgrammingGuide/Reference). This will give you direct access to the DOM and has methods like webkit_dom_document_get_elements_by_tag_name()
to query the DOM. You could do something like this:
WebKitDomDocument *dom = webkit_web_view_get_dom_document(webview);
WebKitDOMNodeList *elements = webkit_dom_document_get_elements_by_tag_name(dom, "a");
int i = 0;
WebKitDOMElement *anchor = NULL;
for (; i < webkit_dom_node_list_get_length(elements); i++) {
anchor = (WebKitDOMElement *)webkit_dom_node_list_item(elements, i);
}
This is not perfect code, but you should get the idea from it. If you want to take a look at a somewhat comprehensive sample look here: http://www.opensource.apple.com/source/WebKit/WebKit-7533.16/gtk/tests/testdomdocument.c. Hope that helps.
精彩评论