-
-
Save khajavi/5425244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 getbodytext.c -o getbodytext | |
* | |
* usage: | |
* /usr/bin/xvfb-run ./getbodytext test.html | |
* | |
*/ | |
#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(); | |
} | |
int | |
main(int argc, char * argv[]) | |
{ | |
GtkWidget * window; | |
GtkWidget * webview; | |
GString * url; | |
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