1 static BrowserWindow _BrowserWindow;
3 public class BrowserWindow : Object
6 private BrowserWindow _this;
8 public static BrowserWindow singleton()
10 if (_BrowserWindow == null) {
11 _BrowserWindow= new BrowserWindow();
13 return _BrowserWindow;
15 public Xcls_scrolled_window scrolled_window;
16 public Xcls_view view;
21 public BrowserWindow()
24 this.el = new Gtk.Window( Gtk.WindowType.TOPLEVEL );
29 var child_0 = new Xcls_scrolled_window( _this );
31 this.el.add ( child_0.el );
34 this.el.destroy.connect( () => {
37 this.el.show.connect( () => {
39 print("resizing window\n");
40 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
41 var h = webkitpdf.opt_height > 0 ? webkitpdf.opt_height : 500;
46 // we have to do silly stuff with blogspot!!?!?
47 var blog = webkitpdf.opt_url;
50 /* Do not redirect if the domain is .com already */
51 if (/\.blogspot\.com/.match(blog)) {
52 var r = new Regex ("\\.blogspot\\..*?/");
53 blog = r.replace(blog, blog.length, 0, ".blogspot.com/ncr/");
56 print("loading url %s\n",blog);
57 this.view.el.load_uri(blog);
59 // if we have not finished in 25 seconds
61 var cd = webkitpdf.opt_delay > 25 ? webkitpdf.opt_delay : 25;
62 GLib.Timeout.add_seconds(1, () => {
64 print("timeout %d seconds :%d", (int)( webkitpdf.opt_delay > 25 ? webkitpdf.opt_delay : 25), (int)cd);
66 print("calling printit - timed out");
78 // user defined functions
79 public class Xcls_scrolled_window : Object
81 public Gtk.ScrolledWindow el;
82 private BrowserWindow _this;
88 public Xcls_scrolled_window(BrowserWindow _owner )
91 _this.scrolled_window = this;
92 this.el = new Gtk.ScrolledWindow( null, null );
97 var child_0 = new Xcls_view( _this );
99 this.el.add ( child_0.el );
104 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
108 // user defined functions
110 public class Xcls_view : Object
112 public WebKit.WebView el;
113 private BrowserWindow _this;
117 public string cookie_file;
118 public bool print_started;
122 public Xcls_view(BrowserWindow _owner )
126 this.el = new WebKit.WebView();
129 this.print_started = false;
132 // set gobject values
140 this.el.load_changed.connect( (ev ) => {
141 if (ev != WebKit.LoadEvent.FINISHED) {
150 this.el.resource_load_started.connect( (resource, request) => {
151 print("Adding referrer header %s\n" , request.get_uri());
153 if (request.get_http_headers() != null) {
154 request.get_http_headers().remove("Referer");
155 request.get_http_headers().append("Referer", request.get_uri());
158 var settings = this.el.get_settings();
159 settings.set_user_agent( "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36" );
160 WebKit.WebContext.get_default().set_tls_errors_policy( WebKit.TLSErrorsPolicy.IGNORE );
164 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
165 // print("resource req. started %s\n", netreq.uri);
169 this.el.load_finished.connect( ( ) => {
175 WebKit.get_default_session().request_started.connect((message, socket) => {
176 Soup.URI uri = message.get_uri();
178 var host = uri.get_host ();
180 print("Adding header http://%s/\n", host);
181 message.request_headers.remove("Referer");
182 message.request_headers.append("Referer", "http://"+host+"/");
186 var sess = WebKit.get_default_session();
187 sess.user_agent= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36";
188 sess.ssl_strict = false;
192 this.cookie_file = "";
193 if (webkitpdf.opt_cookies != null) {
194 print("setting cookie %s\n",webkitpdf.opt_cookies);
196 var uri = new Soup.URI (webkitpdf.opt_url);
198 string[] cookies = webkitpdf.opt_cookies.split ("=", 2);
200 if(cookies.length > 1 ) {
203 var context = WebKit.WebContext.get_default();
204 var cookie_manager = context.get_cookie_manager();
205 cookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.NEVER);
206 string scheme = uri.get_scheme ();
209 FileIOStream iostream;
210 File file = File.new_tmp ("cookie-XXXXXX.txt", out iostream);
211 this.cookie_file = file.get_path ();
212 print("cookie tmp file name: %s\n", file.get_path ());
214 OutputStream ostream = iostream.output_stream;
215 DataOutputStream dostream = new DataOutputStream (ostream);
216 dostream.put_string ("%s\tTRUE\t/\t%s\t2147483647\t%s\t%s".printf(uri.get_host(),scheme == "http" ? "FALSE" : "TRUE",cookies[0],cookies[1]));
217 cookie_manager.set_persistent_storage(file.get_path(), WebKit.CookiePersistentStorage.TEXT);
219 stdout.printf ("Error: %s\n", e.message);
222 var cookie_jar = new Soup.CookieJar();
223 var cookie = new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1);
224 if(uri.get_scheme() =="https") {
225 cookie.set_secure(true);
227 cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.NEVER);
228 cookie_jar.add_cookie(cookie);
229 WebKit.get_default_session().add_feature(cookie_jar);
238 // user defined functions
239 public void printit () {
240 print("load_finsihed\n");
242 if (this.print_started) {
243 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
252 this.print_started = true; // flag to stop double call.
255 // inject any javascript if needed..
256 if (webkitpdf.opt_inject_js != null) {
258 print("injecting javascript files \n");
259 foreach(var inject_js in webkitpdf.opt_inject_js) {
260 print("inject? %s\n", inject_js);
263 FileUtils.get_contents( inject_js, out str);
264 } catch(FileError e) {
265 print("Failed to inject %s\n", inject_js);
269 GLib.MainLoop mainloop = new GLib.MainLoop();
270 this.el.run_javascript.begin(str, null, (obj,res) => {
276 this.el.execute_script(str);
281 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
282 if (webkitpdf.opt_target_pdf != null) {
283 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
284 print("delay done URL: %s\n", this.el.uri);
289 }, GLib.Priority.DEFAULT);
295 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
296 print("delay done URL: %s\n", this.el.uri);
299 }, GLib.Priority.DEFAULT);
303 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
306 // ----------------- JPEG
307 // resize the window...
308 // window.document.documentElement.scrollHeight
309 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
310 print("Scroll height %d\n", scroll_height);
311 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
312 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
313 var h = int.min(scroll_height, 6000);
314 _this.scrolled_window.el.set_size_request( w, h);
315 _this.el.resize (w+50, h+50);
316 print("Resize to %d, %d\n", w,h);
317 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
320 }, GLib.Priority.DEFAULT);
325 }, GLib.Priority.DEFAULT);
331 public bool printpdf () {
335 var list = Gtk.PaperSize.get_paper_sizes(false);
336 var psetup = new Gtk.PageSetup();
337 for(var i = 0; i < list.length(); i++ ) {
338 var entry = list.nth_data(i).copy();
339 if (entry.get_name() == "iso_a2") {
340 psetup.set_paper_size(entry);
347 var pe = new WebKit.PrintOperation(this.el);
348 pe.set_page_setup(psetup);
349 pe.finished.connect( () => {
350 print("print completed\n");
351 this.delete_cookiefile();
356 var ps = new Gtk.PrintSettings();
357 ps.set_printer("Print to File");
358 ps.set("output-file-format", "pdf");
359 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
362 pe.set_print_settings(ps);
369 var pe = new Gtk.PrintOperation();
370 pe.export_filename = webkitpdf.opt_target_pdf;
371 print("got callback for print\n");
373 /*pe.failed.connect(() => {
374 print("print failed\n");
379 pe.done.connect(() => {
380 print("print completed\n");
381 this.delete_cookiefile();
386 //pe.run_dialog(_this.el);
392 pe.set_default_page_setup(psetup);
393 print("Calling Print?\n");
394 //pe.run_dialog(_this.el);
401 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
410 public void printpng () {
411 //var filename = "/tmp/test.pdf";
412 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
414 //if (le != LoadEvent.FINISHED) {
418 // what size is the documet.
420 print("making screenshot\n");
422 GLib.MainLoop mainloop = new GLib.MainLoop();
423 _this.view.el.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
424 var sf = _this.view.el.get_snapshot.end(res);
425 sf.write_to_png(webkitpdf.opt_target_png);
426 print("Saved to %s\n", webkitpdf.opt_target_png);
434 // fix vapi - get_snapshot add '?' to all null.
435 var pixmap = _this.view.el.get_snapshot( null );
441 // add out to gdkpixmap.get_size
442 pixmap.get_size( out w , out h);
443 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
444 print("pixbuf size: %d x %d\n ", w,h);
448 pixbuf.save(webkitpdf.opt_target_png, "png");
449 print("Saved to %s\n", webkitpdf.opt_target_png);
450 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
451 // var sf = view.get_snapshot.end(res);
453 // sf.write_to_png(Browser.opt_target_png);
456 this.delete_cookiefile();
465 public void delete_cookiefile () {
466 if(this.cookie_file.length < 1 ){
469 print("deleting tmp file %s\n",this.cookie_file);
470 File file = File.new_for_path (this.cookie_file);
472 if (file.query_exists () == true) {
476 stdout.printf ("Error: %s\n", e.message);