optional javascript for screenshots
[app.webkitpdf] / webkitpdf.vala
1
2 /*
3  sudo valac   --vapidir=. --thread  webkitpdf.vala  BrowserWindow.vala  --vapidir=./vapi \
4     --pkg glib-2.0 --pkg webkit-1.0 --pkg gtk+-2.0 -o /usr/bin/webkitpdf --target-glib=2.32 
5   
6  sudo valac -D GTK3   webkitpdf.vala  BrowserWindow.vala  --vapidir=./vapi \
7     --pkg glib-2.0 --pkg webkit2gtk-4.0 --pkg gtk+-3.0 --pkg gio-2.0 -o /usr/bin/webkitpdf2 --target-glib=2.32 
8   
9   
10     
11 */
12 public class webkitpdf {
13  
14         public static bool opt_disable_javascript = false;
15     public static string? opt_url = null;
16         public static uint opt_delay = 0;
17         public static int opt_width = 0;
18         public static int opt_height = 0;
19         public static string? opt_target_png = null;
20         public static string? opt_target_pdf = null;    
21         [CCode (array_length = false, array_null_terminated = true)]
22         public static string[] opt_inject_js;
23         public static string? opt_cookies = null;
24         
25         const OptionEntry[] options = {
26                 
27                         { "width", 0, 0, OptionArg.INT, ref opt_width, "Width (default 1200)", null },                  
28                         { "height", 0, 0, OptionArg.INT, ref opt_height, "Height (PNG only) (default to expand to fit)", null },                                                
29                         { "url", 0, 0, OptionArg.STRING, ref opt_url, "Url to grab", null },
30                         { "delay", 0, 0, OptionArg.INT, ref opt_delay, "Delay before snapshot in seconds", null },
31                         { "png", 0, 0, OptionArg.STRING, ref opt_target_png, "File to write (PNG)", null },
32                         { "inject", 0, 0, OptionArg.STRING_ARRAY, ref opt_inject_js, "Inject Javascript file(s)", null },
33                         { "pdf", 0, 0, OptionArg.STRING, ref opt_target_pdf, "File to write (PDF)", null },
34                         { "cookies", 0, 0, OptionArg.STRING, ref opt_cookies, "Inject Cookie string", null },
35                         { "disable-javascript", 0, 0, OptionArg.NONE, ref opt_disable_javascript, "Disable Javascript..", null },
36                         { null }
37         }; 
38     public static int main(string[] args) 
39     {
40         Gtk.init(ref args);
41                 
42                 
43 #if GTK3                
44         // this is a horrific hack from https://bugs.webkit.org/show_bug.cgi?id=128674
45                 Environment.unset_variable("GNOME_DESKTOP_SESSION_ID");
46                 Environment.unset_variable("DESKTOP_SESSION");          
47                 // it should now fallb ack to http prox!?
48                 
49                 
50 #else
51                 unowned string proxy = Environment.get_variable ("http_proxy");
52                 if (proxy != null && proxy.length > 0) {
53                         var sess = WebKit.get_default_session();
54                         sess.proxy_uri = new Soup.URI(proxy);
55                 }
56 #endif          
57         var opt_context = new OptionContext ("webkitpdf");
58         
59                 
60                 try {
61                         opt_context.set_help_enabled (true);
62                         opt_context.add_main_entries (options, null);
63                         opt_context.parse (ref args);
64                         
65             if (webkitpdf.opt_url == null) {
66                 throw new OptionError.BAD_VALUE("missing url");
67             }
68                         if (webkitpdf.opt_target_png == null && webkitpdf.opt_target_pdf == null) {
69                 throw new OptionError.BAD_VALUE("missing pdf or png filename");
70             }
71             
72         
73             stdout.printf ( """Width: %d
74             Height: %d
75             URL: %s
76             Delay: %d
77             png: %s
78             inject: %s
79             PDF: %s
80             Cookie: %s
81             """,
82               opt_width,
83               opt_height,
84               opt_url,
85               (int) opt_delay,
86               opt_target_png == null ? "--empty--" : opt_target_png,
87               opt_inject_js.length < 1? "--empty--" : string.joinv(", ", opt_inject_js),
88               opt_target_pdf == null ? "--empty--" : opt_target_pdf,
89               opt_cookies == null ? "--empty--" : opt_cookies              
90               
91             
92             );
93             
94
95                 } catch (OptionError e) {
96                         stdout.printf ("error: %s\n", e.message);
97                         stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s", 
98                                          args[0], opt_context.get_help(true,null));
99                         return 0;
100                 }
101   
102         var browser = new BrowserWindow();
103         browser.el.show_all();
104  /*
105                 var ses = WebKit.get_default_session();
106                 ses.request_queued.connect((msg) => {
107                         print("queued request %s\n", msg.uri.to_string(false));
108                         msg.got_headers.connect(() => {
109                                 print("got conent type %s\n", msg.response_headers.get_content_type (null));
110                                 if (msg.response_headers.get_content_type (null) == "image/jpeg") {
111                                         ses.cancel_message(msg,Soup.KnownStatusCode.CANCELLED);
112                                 }
113                         });                     
114                 });
115  */
116  
117         Gtk.main();
118  
119         return 0;
120     }
121 }
122
123
124  
125