From 919670c3431e3f3bd0eef04ee3e37d0b9e4634a5 Mon Sep 17 00:00:00 2001 From: Alan Knowles Date: Thu, 26 Mar 2020 12:10:21 +0800 Subject: [PATCH] honour delay in webkit2 printing --- BrowserWindow.bjs | 2 +- BrowserWindow.vala | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/BrowserWindow.bjs b/BrowserWindow.bjs index 20afbdc..d8ddb60 100644 --- a/BrowserWindow.bjs +++ b/BrowserWindow.bjs @@ -27,7 +27,7 @@ "id" : "view", "string cookie_file" : "", "* init" : "\n\n{\n\t\tthis.load_try = 0;\n\n#if GTK3\n\t\t this.el.load_changed.connect( (ev ) => {\n \t\tif (ev != WebKit.LoadEvent.FINISHED) {\n\t\t \t\treturn;\t \n \t\t }\n \t\t \n this.printit(); \n\t \n \n });\n \n\t\t\tthis.el.resource_load_started.connect( (resource, request) => {\n\t\t\t\tprint(\"Adding referrer header %s\\n\" , request.get_uri());\n\t\t\t\t\n\t\t\t\tif (request.get_http_headers() != null) {\n\t \t\t\t\trequest.get_http_headers().remove(\"Referer\");\t\t\n\t \t\t\t\trequest.get_http_headers().append(\"Referer\", request.get_uri());\n\t\t\t\t}\n });\n var settings = this.el.get_settings();\n\t\t\tsettings.set_user_agent( \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36\" );\n\t\t\t\t\n \n#else\n //listeners\n // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {\n // print(\"resource req. started %s\\n\", netreq.uri);\n \n // });\n \n this.el.load_finished.connect( ( ) => {\n \t\t\n \t\t\n this.printit(); \n \n });\n WebKit.get_default_session().request_started.connect((message, socket) => {\n \t Soup.URI uri = message.get_uri();\n\t\t\t\n\t\t\t\tvar host = uri.get_host ();\n\n\t\t\t\tprint(\"Adding header http://%s/\\n\", host);\n\t\t\t\tmessage.request_headers.remove(\"Referer\");\t\t\t\t\n\t\t\t\tmessage.request_headers.append(\"Referer\", \"http://\"+host+\"/\");\n\t\t\t\t\n \n });\n var sess = WebKit.get_default_session();\n\t\t\tsess.user_agent= \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36\";\n\t\t\t\n \n#endif\n}\n\t \tthis.cookie_file = \"\";\n\t\tif (webkitpdf.opt_cookies != null) {\n\t\t\tprint(\"setting cookie %s\\n\",webkitpdf.opt_cookies);\n\n\t\t\tvar uri = new Soup.URI (webkitpdf.opt_url);\n\t\t\t\n\t\t\tstring[] cookies = webkitpdf.opt_cookies.split (\"=\", 2);\n\n\t\t\tif(cookies.length > 1 ) {\n\t\t\t\t\t \n\t\t\t#if GTK3\t\n\t\t\t\tvar context = WebKit.WebContext.get_default();\t \n\t\t\t\tvar cookie_manager = context.get_cookie_manager();\n\t\t\t\tcookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.NEVER);\n\t\t\t\tstring scheme = uri.get_scheme ();\n\t\t\t\n\t\t\t\ttry { \n\t\t\t\t FileIOStream iostream;\t\t \n\t\t\t\t File file = File.new_tmp (\"cookie-XXXXXX.txt\", out iostream);\n\t\t\t\t this.cookie_file = file.get_path (); \n\t\t\t\t print(\"cookie tmp file name: %s\\n\", file.get_path ());\n\t\t \t \n\t\t\t\t OutputStream ostream = iostream.output_stream;\n\t\t\t\t DataOutputStream dostream = new DataOutputStream (ostream);\t\t\t\t\t \n\t\t\t\t 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]));\t\t \n\t\t\t\t cookie_manager.set_persistent_storage(file.get_path(), WebKit.CookiePersistentStorage.TEXT);\t\t \n\t\t\t\t} catch (Error e) {\n\t\t\t\t stdout.printf (\"Error: %s\\n\", e.message);\n\t\t\t\t} \t\t\n\t\t\t#else\n\t\t\t\t\tvar cookie_jar = new Soup.CookieJar();\t\t\n\t\t\t\tvar cookie = new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), \"/\", -1);\n\t\t\t\tif(uri.get_scheme() ==\"https\") {\n\t\t\t\t cookie.set_secure(true); \n\t\t\t\t}\t \n\t\t\t\tcookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.NEVER);\t\t\t\t \n\t\t\t\tcookie_jar.add_cookie(cookie);\t\t\t \n\t\t\t\tWebKit.get_default_session().add_feature(cookie_jar);\n\t\t\t#endif\n\t\t\t\t\t \t\t\t \t\t\t \n\t\t\t}\n\n\t\t} \n // init method", - "| void printit" : "() {\n print(\"load_finsihed\\n\");\n \n if (this.print_started) {\n print(\"load_finsihed - already printing..%d %s\\n\", this.load_try, this.el.uri);\n \n \n \n \n return;\n }\n \n \n this.print_started = true; // flag to stop double call.\n \n \n\t// inject any javascript if needed..\n\tif (webkitpdf.opt_inject_js != null) {\n\t\n\t\tprint(\"injecting javascript files \\n\");\n\t\tforeach(var inject_js in webkitpdf.opt_inject_js) {\n\t\tprint(\"inject? %s\\n\", inject_js);\n\t\t\tstring str;\n\t\t\ttry {\n\t\t\t\tFileUtils.get_contents( inject_js, out str);\n\t\t\t} catch(FileError e) {\n\t\t\t\tprint(\"Failed to inject %s\\n\", inject_js);\n\t\t\t\tcontinue;\n\t\t\t}\n#if GTK3\n\t\t\tGLib.MainLoop mainloop = new GLib.MainLoop();\n\t\t\tthis.el.run_javascript.begin(str, null, (obj,res) => {\n\t\t\t\tmainloop.quit();\n\t\t\t});\n\t\t\tmainloop.run();\n\t\t\t\n#else \t\t\t\n\t\t\tthis.el.execute_script(str);\n#endif\n\t\t}\n\t}\n\t \n\tprint(\"calling print in %d seconds\\n\", (int)webkitpdf.opt_delay);\n\tif (webkitpdf.opt_target_pdf != null) {\n\t\tGLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n\t\t\tprint(\"delay done URL: %s\\n\", this.el.uri);\n\t\t\n\t\t\t \n\t\t\tthis.printpdf();\n\t\t\t return false;\n\t\t}, GLib.Priority.DEFAULT);\n\t\treturn;\n\t}\n\t\t\n\n#if GTK3\n\tthis.printpng();\t\n\t//print(\"PNG not supported in webkit2\\n\");\n\treturn;\n#else\n\t\t\n GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n \n\t\t\n\t\t// ----------------- JPEG \n\t\t// resize the window...\n\t\t// window.document.documentElement.scrollHeight\n\t\tvar scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;\n\t\tprint(\"Scroll height %d\\n\", scroll_height);\n\t\tif (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {\n\t\t\tvar w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;\n\t\t\tvar h = int.min(scroll_height, 6000);\n\t\t\t_this.scrolled_window.el.set_size_request( w, h); \n\t\t\t_this.el.resize (w+50, h+50);\n\t\t\tprint(\"Resize to %d, %d\\n\", w,h);\n\t\t GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { \n\t\t\t\tthis.printpng();\n\t\t\t\treturn false;\n\t\t\t }, GLib.Priority.DEFAULT);\n \t\t return false;\n\t\t}\n\t\tthis.printpng();\t\t\n\t\treturn false;\n\t }, GLib.Priority.DEFAULT);\n\n\n#endif \n \n}\n", + "| void printit" : "() {\n print(\"load_finsihed\\n\");\n \n if (this.print_started) {\n print(\"load_finsihed - already printing..%d %s\\n\", this.load_try, this.el.uri);\n \n \n \n \n return;\n }\n \n \n this.print_started = true; // flag to stop double call.\n \n \n\t// inject any javascript if needed..\n\tif (webkitpdf.opt_inject_js != null) {\n\t\n\t\tprint(\"injecting javascript files \\n\");\n\t\tforeach(var inject_js in webkitpdf.opt_inject_js) {\n\t\tprint(\"inject? %s\\n\", inject_js);\n\t\t\tstring str;\n\t\t\ttry {\n\t\t\t\tFileUtils.get_contents( inject_js, out str);\n\t\t\t} catch(FileError e) {\n\t\t\t\tprint(\"Failed to inject %s\\n\", inject_js);\n\t\t\t\tcontinue;\n\t\t\t}\n#if GTK3\n\t\t\tGLib.MainLoop mainloop = new GLib.MainLoop();\n\t\t\tthis.el.run_javascript.begin(str, null, (obj,res) => {\n\t\t\t\tmainloop.quit();\n\t\t\t});\n\t\t\tmainloop.run();\n\t\t\t\n#else \t\t\t\n\t\t\tthis.el.execute_script(str);\n#endif\n\t\t}\n\t}\n\t \n\tprint(\"calling print in %d seconds\\n\", (int)webkitpdf.opt_delay);\n\tif (webkitpdf.opt_target_pdf != null) {\n\t\tGLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n\t\t\tprint(\"delay done URL: %s\\n\", this.el.uri);\n\t\t\n\t\t\t \n\t\t\tthis.printpdf();\n\t\t\t return false;\n\t\t}, GLib.Priority.DEFAULT);\n\t\treturn;\n\t}\n\t\t\n\n#if GTK3\n\tGLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n\t\t\tprint(\"delay done URL: %s\\n\", this.el.uri);\n\t\t\tthis.printpng();\t\n\t\t\treturn false;\n\t\t}, GLib.Priority.DEFAULT);\n\t\treturn;\n#else\n\t\t\n GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n \n\t\t\n\t\t// ----------------- JPEG \n\t\t// resize the window...\n\t\t// window.document.documentElement.scrollHeight\n\t\tvar scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;\n\t\tprint(\"Scroll height %d\\n\", scroll_height);\n\t\tif (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {\n\t\t\tvar w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;\n\t\t\tvar h = int.min(scroll_height, 6000);\n\t\t\t_this.scrolled_window.el.set_size_request( w, h); \n\t\t\t_this.el.resize (w+50, h+50);\n\t\t\tprint(\"Resize to %d, %d\\n\", w,h);\n\t\t GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { \n\t\t\t\tthis.printpng();\n\t\t\t\treturn false;\n\t\t\t }, GLib.Priority.DEFAULT);\n \t\t return false;\n\t\t}\n\t\tthis.printpng();\t\t\n\t\treturn false;\n\t }, GLib.Priority.DEFAULT);\n\n\n#endif \n \n}\n", "* pack" : "add", "xtype" : "WebView", "| bool printpdf" : "() {\n\n\n\n\t\tvar list = Gtk.PaperSize.get_paper_sizes(false);\n\t var psetup = new Gtk.PageSetup();\n\t for(var i = 0; i < list.length(); i++ ) {\n\t var entry = list.nth_data(i).copy();\n\t if (entry.get_name() == \"iso_a2\") {\n\t psetup.set_paper_size(entry);\n\t }\n\t }\n\t \n\n#if GTK3\t \n\n\t\tvar pe = new WebKit.PrintOperation(this.el);\n\t\tpe.set_page_setup(psetup);\n\t\tpe.finished.connect( () => {\n\t\t\tprint(\"print completed\\n\");\n this.delete_cookiefile(); \n\t\t\t Gtk.main_quit();\n \t\t\n\t\t});\n\t\t\n\t\tvar ps = new Gtk.PrintSettings();\n\t ps.set_printer(\"Print to File\");\n\t ps.set(\"output-file-format\", \"pdf\");\n\t ps.set(\"output-uri\", \"file://\" + webkitpdf.opt_target_pdf);\n\n\t\t\n\t\tpe.set_print_settings(ps);\n\t\t\n\n\t\tpe.print();\n \t\t \n\n#else\t \n\t\tvar pe = new Gtk.PrintOperation();\n\t\tpe.export_filename = webkitpdf.opt_target_pdf;\n\t\tprint(\"got callback for print\\n\");\n\t\tpe.ref();\n\t\t/*pe.failed.connect(() => {\n\t\t\tprint(\"print failed\\n\");\n\t\t\t Gtk.main_quit();\n \t\t\n\t\t})\n\t\t*/;\n\t\t pe.done.connect(() => {\n\t\t\tprint(\"print completed\\n\");\n this.delete_cookiefile(); \n\t\t\t Gtk.main_quit();\n \t\t\n\t\t});\n\t\t\n\t\t//pe.run_dialog(_this.el);\n\t \n\t\t\n\t \n\n\n\t pe.set_default_page_setup(psetup);\n\t print(\"Calling Print?\\n\");\n\t //pe.run_dialog(_this.el);\n\t \n\t \n\n\n\n\t try {\n\t\t this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);\n\t } catch (Error e) {\n\t\t\t// print failed...\n\t\t}\n#endif\t \n\t \n\t \n\t\treturn false;\n}\n", diff --git a/BrowserWindow.vala b/BrowserWindow.vala index 3bd2bcd..280047f 100644 --- a/BrowserWindow.vala +++ b/BrowserWindow.vala @@ -292,9 +292,12 @@ public class BrowserWindow : Object #if GTK3 - this.printpng(); - //print("PNG not supported in webkit2\n"); - return; + GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { + print("delay done URL: %s\n", this.el.uri); + this.printpng(); + return false; + }, GLib.Priority.DEFAULT); + return; #else GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { -- 2.39.2