Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created June 30, 2009 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellbind/138117 to your computer and use it in GitHub Desktop.
Save bellbind/138117 to your computer and use it in GitHub Desktop.
[c][gtk][webkit] Print entire HTML text after processed JavaScript
/*
* Print entire HTML text after processed JavaScript
*
* build:
* FLAGS=`pkg-config --cflags --libs gtk+-x11-2.0 glib-2.0 webkit-1.0`
* gcc -Wall $FLAGS -lgc getbodytext_gc.c -o getbodytext_gc
*
* usage:
* /usr/bin/xvfb-run ./getbodytext test.html
*
*/
#include <gc/gc.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <webkit/webkit.h>
#include <JavaScriptCore/JavaScript.h>
static const gchar code[] =
"(function () {"
" var scripts = document.getElementsByTagName(\"script\");"
" for (var i = 0; i < scripts.length; i += 1) {"
" scripts[i].innerHTML = \"\";"
" }"
" var range = document.createRange();"
" range.selectNodeContents(document.body);"
" return range.toString();"
"})()";
/* const gchar code[] = "document.body.innerHTML"; */
static GString *
toGString(JSStringRef jsstr)
{
size_t size;
gchar * buf;
size = JSStringGetMaximumUTF8CStringSize(jsstr);
buf = g_alloca(size);
JSStringGetUTF8CString(jsstr, buf, size);
return g_string_new(buf);
}
/*
* see: http://webkitgtk.org/reference/webkitgtk-WebKitWebView.html signals
*/
static void
loaded(WebKitWebView * view, WebKitWebFrame * frame, gpointer data)
{
/*
const gchar * title;
*/
JSGlobalContextRef gc;
JSStringRef script;
JSValueRef result;
/*
title = webkit_web_frame_get_title(frame);
g_printf("%s\n", title);
*/
gc = webkit_web_frame_get_global_context(frame);
script = JSStringCreateWithUTF8CString(code);
result = JSEvaluateScript(gc, script, NULL, NULL, 0, NULL);
/* JSStringRelease(script); */
if (JSValueIsString(gc, result)) {
JSStringRef jsstr;
GString * gstr;
jsstr = JSValueToStringCopy(gc, result, NULL);
gstr = toGString(jsstr);
/* JSStringRelease(jsstr); */
g_printf("%s\n", gstr->str);
/* g_string_free(gstr, TRUE); */
}
gtk_main_quit();
}
static void
init_gc(GMemVTable * memvtable)
{
/* see: http://manpages.ubuntu.com/manpages/jaunty/man3/gc.3.html */
GC_INIT();
/* see: GMemVTable and g_mem_set_vtable
* http://library.gnome.org/devel/glib/stable/glib-Memory-Allocation.html
*/
memvtable->malloc = GC_malloc;
memvtable->realloc = GC_realloc;
memvtable->free = GC_free;
memvtable->calloc = NULL;
memvtable->try_malloc = NULL;
memvtable->try_realloc = NULL;
g_mem_set_vtable(memvtable);
}
int
main(int argc, char * argv[])
{
GMemVTable memvtable;
GtkWidget * window;
GtkWidget * webview;
GString * url;
init_gc(&memvtable);
g_thread_init(NULL);
gdk_threads_init();
gdk_threads_enter();
gtk_init(&argc, &argv);
url = g_string_new(argv[1]);
if (!g_str_has_prefix(url->str, "http")) {
url = g_string_prepend(url, "file://");
}
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
webview = webkit_web_view_new();
gtk_container_add(GTK_CONTAINER(window), webview);
gtk_signal_connect(GTK_OBJECT(webview), "load-finished",
GTK_SIGNAL_FUNC(loaded), NULL);
webkit_web_view_open(WEBKIT_WEB_VIEW(webview), url->str);
/* g_string_free(url, TRUE); */
gtk_widget_show_all(window);
gtk_main();
gdk_threads_leave();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment