domains/remove_print_css.js
[app.webkitpdf] / BrowserWindow.vala
index f914131..e1f421c 100644 (file)
@@ -37,9 +37,40 @@ public class BrowserWindow : Object
         this.el.show.connect( () => {
                
                print("resizing window\n");
-               this.el.resize(1200,500);
-               print("loading url\n");
-               this.view.el.load_uri(webkitpdf.opt_url);
+               var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
+               var h = webkitpdf.opt_height > 0 ?  webkitpdf.opt_height  :  500;
+                               
+               
+               this.el.resize(w,h);
+        
+               // we have to do silly stuff with blogspot!!?!?
+               var blog = webkitpdf.opt_url;
+        
+        
+          /* Do not redirect if the domain is .com already */
+          if (/\.blogspot\.com/.match(blog)) {
+                       var r =  new Regex ("\\.blogspot\\..*?/");
+                       blog = r.replace(blog, blog.length, 0, ".blogspot.com/ncr/");
+          }
+               
+               print("loading url %s\n",blog); 
+               this.view.el.load_uri(blog);
+                
+               // if we have not finished in 25 seconds
+               // call printit...
+               var cd = webkitpdf.opt_delay > 25 ? webkitpdf.opt_delay : 25;
+                 GLib.Timeout.add_seconds(1, () => { 
+                       cd--;
+                       print("timeout %d seconds :%d", (int)( webkitpdf.opt_delay > 25 ? webkitpdf.opt_delay : 25), (int)cd);
+                       if (cd < 1) {
+                               print("calling printit - timed out");
+                               _this.view.printit();
+                               return false;                   
+                       }
+                       return true;
+        
+               });
+               
         
         });
     }
@@ -83,6 +114,9 @@ public class BrowserWindow : Object
 
 
             // my vars (def)
+        public string cookie_file;
+        public bool print_started;
+        public int load_try;
 
         // ctor
         public Xcls_view(BrowserWindow _owner )
@@ -92,50 +126,175 @@ public class BrowserWindow : Object
             this.el = new WebKit.WebView();
 
             // my vars (dec)
+            this.print_started = false;
+            this.load_try = 0;
 
             // set gobject values
 
-            //listeners
-            this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
-              print("resource req. started %s\n", netreq.uri);
-              
-              
-            });
-            this.el.load_finished.connect( ( ) => {
-                        
-                   this.printit();
-             
-            });
+            // init method
+
+            {
+                       this.load_try = 0;
+            
+            #if GTK3
+                        this.el.load_changed.connect( (ev ) => {
+                               if (ev != WebKit.LoadEvent.FINISHED) {
+                                       return;  
+                                 }
+                                 
+                           this.printit(); 
+                         
+                         
+                        });
+                        
+                               this.el.resource_load_started.connect( (resource, request) => {
+                                       print("Adding referrer header %s\n" , request.get_uri());
+                                       
+                                       if (request.get_http_headers() != null) {
+                                               request.get_http_headers().remove("Referer");           
+                                               request.get_http_headers().append("Referer", request.get_uri());
+                                       }
+                        });
+                        var settings = this.el.get_settings();
+                               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" );
+                                       
+                       
+            #else
+             //listeners
+                      //  this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
+                        //  print("resource req. started %s\n", netreq.uri);
+                             
+                      //  });
+                        
+                        this.el.load_finished.connect( ( ) => {
+                               
+                               
+                               this.printit(); 
+                         
+                        });
+                        WebKit.get_default_session().request_started.connect((message, socket) => {
+                           Soup.URI uri = message.get_uri();
+                               
+                                       var host = uri.get_host ();
+            
+                                       print("Adding header http://%s/\n", host);
+                                       message.request_headers.remove("Referer");                              
+                                       message.request_headers.append("Referer", "http://"+host+"/");
+                                       
+                        
+                        });
+                        var sess = WebKit.get_default_session();
+                               sess.user_agent= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36";
+                               
+                        
+            #endif
+            }
+                       this.cookie_file = "";
+                       if (webkitpdf.opt_cookies != null) {
+                               print("setting cookie %s\n",webkitpdf.opt_cookies);
+            
+                               var uri = new Soup.URI (webkitpdf.opt_url);
+                               
+                               string[] cookies = webkitpdf.opt_cookies.split ("=", 2);
+            
+                               if(cookies.length > 1 ) {
+                                                    
+                               #if GTK3        
+                                       var context = WebKit.WebContext.get_default();           
+                                       var cookie_manager = context.get_cookie_manager();
+                                       cookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.NEVER);
+                                       string scheme = uri.get_scheme ();
+                               
+                                       try {            
+                                           FileIOStream iostream;                            
+                                           File file = File.new_tmp ("cookie-XXXXXX.txt", out iostream);
+                                           this.cookie_file = file.get_path ();        
+                                           print("cookie tmp file name: %s\n", file.get_path ());
+                                
+                                           OutputStream ostream = iostream.output_stream;
+                                           DataOutputStream dostream = new DataOutputStream (ostream);                                      
+                                           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]));                             
+                                           cookie_manager.set_persistent_storage(file.get_path(), WebKit.CookiePersistentStorage.TEXT);                              
+                                       } catch (Error e) {
+                                           stdout.printf ("Error: %s\n", e.message);
+                                       }               
+                               #else
+                                               var cookie_jar = new Soup.CookieJar();          
+                                       var cookie = new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1);
+                                       if(uri.get_scheme() =="https") {
+                                           cookie.set_secure(true);                    
+                                       }           
+                                       cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.NEVER);                          
+                                       cookie_jar.add_cookie(cookie);                      
+                                       WebKit.get_default_session().add_feature(cookie_jar);
+                               #endif
+                                                                                                           
+                               }
+            
+                       } 
+                        // init method
         }
 
         // user defined functions
         public void printit () {
            print("load_finsihed\n");
+           
+           if (this.print_started) {
+               print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
+           
+              
+              
+              
+              return;
+          }
+           
          
+            this.print_started = true; // flag to stop double call.
+            
+            
                // inject any javascript if needed..
                if (webkitpdf.opt_inject_js != null) {
-                       for(var i =0; i < webkitpdf.opt_inject_js.length;i++) {
+               
+                       print("injecting javascript files \n");
+                       foreach(var inject_js in  webkitpdf.opt_inject_js) {
+                       print("inject? %s\n", inject_js);
                                string str;
                                try {
-                                       FileUtils.get_contents( webkitpdf.opt_inject_js[i], out str);
+                                       FileUtils.get_contents( inject_js, out str);
                                } catch(FileError e) {
+                                       print("Failed to inject %s\n",  inject_js);
                                        continue;
                                }
+        #if GTK3
+                               GLib.MainLoop mainloop = new GLib.MainLoop();
+                               this.el.run_javascript.begin(str, null, (obj,res) => {
+                                       mainloop.quit();
+                               });
+                               mainloop.run();
+                               
+        #else                  
                                this.el.execute_script(str);
+        #endif
                        }
                }
-               
-               
-               
+                 
                print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
                if (webkitpdf.opt_target_pdf != null) {
                        GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
-        
+                               print("delay done URL: %s\n", this.el.uri);
+                       
+                                
                                this.printpdf();
                                 return false;
                        }, GLib.Priority.DEFAULT);
+                       return;
                }
                        
+        
+        #if GTK3
+               print("PNG not supported in webkit2\n");
+               return;
+        #else
                        
             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
            
@@ -145,27 +304,67 @@ public class BrowserWindow : Object
                        // window.document.documentElement.scrollHeight
                        var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
                        print("Scroll height %d\n", scroll_height);
-                       if (scroll_height> 1024) {
-                               _this.scrolled_window.el.set_size_request(1200, int.min(scroll_height, 6000));
-                               print("Resize to %d\n", scroll_height);
-                           GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
+                       if (scroll_height> 1024 || webkitpdf.opt_width > 0  || webkitpdf.opt_height > 0  ) {
+                               var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
+                               var h = int.min(scroll_height, 6000);
+                               _this.scrolled_window.el.set_size_request( w, h); 
+                               _this.el.resize (w+50, h+50);
+                               print("Resize to %d, %d\n", w,h);
+                           GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { 
                                        this.printpng();
                                        return false;
                                  }, GLib.Priority.DEFAULT);
+                           return false;
                        }
                        this.printpng();                
                        return false;
                  }, GLib.Priority.DEFAULT);
         
         
-            
+        #endif    
             
         }
         public bool printpdf () {
-                
+        
+        
+        
+                       var list = Gtk.PaperSize.get_paper_sizes(false);
+                   var psetup = new Gtk.PageSetup();
+                   for(var i = 0; i < list.length(); i++ ) {
+                       var entry = list.nth_data(i).copy();
+                       if (entry.get_name() == "iso_a2") {
+                           psetup.set_paper_size(entry);
+                       }
+                   }
+                   
+        
+        #if GTK3        
+        
+                       var  pe = new  WebKit.PrintOperation(this.el);
+                       pe.set_page_setup(psetup);
+                       pe.finished.connect( () => {
+                               print("print completed\n");
+                    this.delete_cookiefile(); 
+                                Gtk.main_quit();
+                       
+                       });
+                       
+                       var ps = new Gtk.PrintSettings();
+                   ps.set_printer("Print to File");
+                   ps.set("output-file-format", "pdf");
+                   ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
+        
+                       
+                       pe.set_print_settings(ps);
+                       
+        
+                       pe.print();
+                                 
+        
+        #else      
                        var  pe = new  Gtk.PrintOperation();
                        pe.export_filename = webkitpdf.opt_target_pdf;
-                       print("got callback for print");
+                       print("got callback for print\n");
                        pe.ref();
                        /*pe.failed.connect(() => {
                                print("print failed\n");
@@ -175,6 +374,7 @@ public class BrowserWindow : Object
                        */;
                         pe.done.connect(() => {
                                print("print completed\n");
+                    this.delete_cookiefile(); 
                                 Gtk.main_quit();
                        
                        });
@@ -182,26 +382,23 @@ public class BrowserWindow : Object
                        //pe.run_dialog(_this.el);
                 
                        
-                   var list = Gtk.PaperSize.get_paper_sizes(false);
-                   var psetup = new Gtk.PageSetup();
-                   for(var i = 0; i < list.length(); i++ ) {
-                       var entry = list.nth_data(i).copy();
-                       if (entry.get_name() == "iso_a2") {
-                           psetup.set_paper_size(entry);
-                       }
-                   }
                        
         
         
                    pe.set_default_page_setup(psetup);
                    print("Calling Print?\n");
                    //pe.run_dialog(_this.el);
+                   
+                   
+        
+        
+        
                    try {
                            this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
                    } catch (Error e) {
                                // print failed...
                        }
-                   
+        #endif     
                    
                     
                        return false;
@@ -217,7 +414,9 @@ public class BrowserWindow : Object
         // what size is the documet.
         
              print("making screenshot\n");
-             
+        #if GTK3
+        
+        #else     
             // fix vapi - get_snapshot add '?' to all null.
             var pixmap = _this.view.el.get_snapshot( null );
         
@@ -239,12 +438,28 @@ public class BrowserWindow : Object
         
             //    sf.write_to_png(Browser.opt_target_png);
             //});
+           this.delete_cookiefile(); 
             Gtk.main_quit();
              
-           
+        #endif   
             
             
         
+        }
+        public void delete_cookiefile () {
+               if(this.cookie_file.length < 1 ){
+                       return;
+               }
+            print("deleting tmp file %s\n",this.cookie_file);
+            File file = File.new_for_path (this.cookie_file);
+            try {
+                if (file.query_exists () == true) {
+                   file.delete ();                  
+                }
+            } catch (Error e) {
+                stdout.printf ("Error: %s\n", e.message);
+            }
+        
         }
     }