-
Notifications
You must be signed in to change notification settings - Fork 4
/
screenshot.c
107 lines (83 loc) · 2.71 KB
/
screenshot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <libsoup/soup.h>
#include <cairo-pdf.h>
#include <glib/gprintf.h>
#include <stdlib.h>
typedef struct IdleData_ {
WebKitWebView *web_view;
const char *filename;
} IdleData;
static void
save_as_pdf (GtkWidget *widget, const char *filename) {
GtkAllocation allocation;
g_printf("Saving PDF to file %s\n", filename);
gtk_widget_get_allocation(widget, &allocation);
cairo_surface_t *surface = cairo_pdf_surface_create(
filename,
1.0 * allocation.width,
1.0 * allocation.height
);
cairo_t *cr = cairo_create(surface);
gtk_widget_draw(widget, cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
static gboolean
idle_cb (gpointer data) {
IdleData *idle_data = (IdleData *) data;
save_as_pdf(GTK_WIDGET(idle_data->web_view), idle_data->filename);
g_free(idle_data);
gtk_main_quit();
return FALSE;
}
static void
load_status_cb (GObject* object, GParamSpec* pspec, gpointer data) {
WebKitWebView *web_view = WEBKIT_WEB_VIEW(object);
WebKitLoadStatus status = webkit_web_view_get_load_status(web_view);
if (status != WEBKIT_LOAD_FINISHED) {
return;
}
g_printf("Downloaded\n");
IdleData *idle_data = g_new0(IdleData, 1);
idle_data->web_view = web_view;
idle_data->filename = (const char*) data;
g_timeout_add(2000, idle_cb, (gpointer) idle_data);
}
int
main (int argc, gchar* argv[]) {
gtk_init(&argc, &argv);
if (argc < 2) {
printf("Usage: URI [filename]\n");
return 1;
}
const gchar *uri = argv[1];
const gchar *filename = argc > 2 ? argv[2] : "a.pdf";
if (!g_thread_supported()) {g_thread_init(NULL);}
g_printf("Running against webkit %d.%d.%d\n",
webkit_major_version(),
webkit_minor_version(),
webkit_micro_version()
);
gchar *proxy = getenv("http_proxy");
if (proxy == NULL) proxy = getenv("HTTP_PROXY");
if (proxy != NULL) {
SoupSession *session = webkit_get_default_session();
SoupURI *proxy_uri = soup_uri_new(proxy);
g_printf("Using proxy: %s", proxy);
g_object_set(
session,
SOUP_SESSION_PROXY_URI, proxy_uri,
NULL
);
}
WebKitWebView *web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
g_signal_connect(web_view, "notify::load-status", G_CALLBACK(load_status_cb), (gpointer) filename);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 640);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(web_view));
gtk_widget_show_all(window);
webkit_web_view_load_uri(web_view, uri);
gtk_main();
return 0;
}