Fix #7287 - upgrade to newer webkit
[app.webkitpdf] / BrowserWindow.bjs
1 {
2  "build_module" : "webkitpdf",
3  "items" : [
4   {
5    "$ xns" : "Gtk",
6    "Gtk.WindowType type" : "Gtk.WindowType.TOPLEVEL",
7    "items" : [
8     {
9      "$ xns" : "Gtk",
10      "* init" : [
11       "{",
12       "\tthis.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);",
13       "}",
14       ""
15      ],
16      "* pack" : "add",
17      "id" : "scrolled_window",
18      "items" : [
19       {
20        "# bool print_started" : false,
21        "# int load_try" : 0,
22        "# string cookie_file" : "",
23        "$ xns" : "WebKit",
24        "* init" : [
25         "",
26         "",
27         "{",
28         "\t\tthis.load_try = 0;",
29         "",
30         "",
31         "",
32         "",
33         "#if GTK3",
34         "\t\t this.el.load_changed.connect( (ev ) => {",
35         "    \t\tif (ev != WebKit.LoadEvent.FINISHED) {",
36         "\t\t    \t\treturn;\t ",
37         "    \t\t  }",
38         "    \t\t  ",
39         "               this.printit(); ",
40         "\t          ",
41         "             ",
42         "            });",
43         "            ",
44         "\t\t\tthis.el.resource_load_started.connect( (resource, request) => {",
45         "\t\t\t//\tprint(\"Adding referrer header %s\\n\" , request.get_uri());",
46         "\t\t\t\t",
47         "\t\t\t\tif (request.get_http_headers() != null) {",
48         "\t \t\t\t\trequest.get_http_headers().remove(\"Referer\");\t\t",
49         "\t \t\t\t\trequest.get_http_headers().append(\"Referer\", request.get_uri());",
50         "\t\t\t\t}",
51         "            });",
52         "            var settings = this.el.get_settings();",
53         "\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\" );",
54         "\t\t\tsettings.hardware_acceleration_policy = WebKit.HardwareAccelerationPolicy.NEVER;",
55         "\t\t\tsettings.enable_accelerated_2d_canvas = false;",
56         "\t\t\tif (webkitpdf.opt_disable_javascript) { ",
57         "\t \t\t\tsettings.enable_javascript  = false;",
58         " \t\t\t}",
59         "",
60         "\t\t\tWebKit.WebContext.get_default().set_tls_errors_policy(\tWebKit.TLSErrorsPolicy.IGNORE );",
61         "           ",
62         "#else",
63         " //listeners",
64         "          //  this.el.resource_request_starting.connect( (p0, webres, netreq ) => {",
65         "            //  print(\"resource req. started %s\\n\", netreq.uri);",
66         "                 ",
67         "          //  });",
68         "            ",
69         "            this.el.load_finished.connect( ( ) => {",
70         "        \t\t ",
71         "                   this.printit(); ",
72         "             ",
73         "            });",
74         "            WebKit.get_default_session().request_started.connect((message, socket) => {",
75         "          \t    Soup.URI uri = message.get_uri();",
76         "\t\t\t",
77         "\t\t\t\tvar host = uri.get_host ();",
78         "\t\t\t\tif (host.contains(\"google-analytics\") || host.contains(\"googlesyndication\") || host.contains(\"facebook\") ) {",
79         "\t\t\t\t\tprint(\"DISCONNECTING: %s\\n\", host);",
80         "\t\t\t\t\tsocket.disconnect();",
81         "\t\t\t\t\treturn;",
82         "\t\t\t\t}",
83         "",
84         "\t\t\t\tprint(\"Adding header: %s\\n\", uri.to_string(false));",
85         "\t\t\t\tmessage.request_headers.remove(\"Referer\");\t\t\t\t",
86         "\t\t\t\tmessage.request_headers.append(\"Referer\", \"http://\"+host+\"/\");",
87         "\t\t\t\t",
88         "            ",
89         "            });",
90         "            var sess = WebKit.get_default_session();",
91         "\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\";",
92         "\t\t\tsess.ssl_strict = false;",
93         "\t\t\t",
94         "            var settings = this.el.get_settings();",
95         "            ",
96         "\t\t\tsettings.enable_webgl = false;",
97         "\t\t\tsettings.enable_plugins  = false;",
98         "\t\t\tsettings.enable_java_applet  = false;",
99         "\t\t\tif (webkitpdf.opt_disable_javascript) { ",
100         "\t \t\t\tsettings.enable_scripts  = false;",
101         " \t\t\t}",
102         "\t\t\t",
103         "\t\t\t",
104         "\t\t\tthis.el.console_message.connect((msg, line, sid) => {",
105         "\t\t\t\tprint(\"CONSOLE: %s:%d %s\\n\", sid, line, msg);",
106         "\t\t\t\treturn false;",
107         "\t\t\t});",
108         "\t\t\tthis.el.create_web_view.connect((frame) => {",
109         "\t\t\t\tprint(\"WEBFRAME:%s\\n\", frame.uri);",
110         " \t\t\t\treturn new WebKit.WebView();",
111         "\t\t\t});",
112         "            ",
113         "#endif",
114         "}",
115         "\t \tthis.cookie_file = \"\";",
116         "\t\tif (webkitpdf.opt_cookies != null) {",
117         "\t\t\tprint(\"setting cookie %s\\n\",webkitpdf.opt_cookies);",
118         "",
119         "\t\t\tvar uri =   GLib.Uri.parse (webkitpdf.opt_url, GLib.UriFlags.NONE);",
120         "\t\t\t",
121         "\t\t\tstring[] cookies = webkitpdf.opt_cookies.split (\"=\", 2);",
122         "",
123         "\t\t\tif(cookies.length > 1 ) {",
124         "\t\t",
125         "#if GTK3\t",
126         "\t\t\t\tvar context = WebKit.WebContext.get_default();\t         ",
127         "\t\t\t\tvar cookie_manager = context.get_cookie_manager();",
128         "\t\t\t\tcookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.NEVER);",
129         "\t\t\t\tstring scheme = uri.get_scheme ();",
130         "\t\t\t",
131         "\t\t\t\ttry {            ",
132         "\t\t\t\t    FileIOStream iostream;\t\t              ",
133         "\t\t\t\t    File file = File.new_tmp (\"cookie-XXXXXX.txt\", out iostream);",
134         "\t\t\t\t    this.cookie_file = file.get_path ();        ",
135         "\t\t\t\t    print(\"cookie tmp file name: %s\\n\", file.get_path ());",
136         "\t\t \t ",
137         "\t\t\t\t    OutputStream ostream = iostream.output_stream;",
138         "\t\t\t\t    DataOutputStream dostream = new DataOutputStream (ostream);\t\t\t\t\t     ",
139         "\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              ",
140         "\t\t\t\t    cookie_manager.set_persistent_storage(file.get_path(), WebKit.CookiePersistentStorage.TEXT);\t\t              ",
141         "\t\t\t\t} catch (Error e) {",
142         "\t\t\t\t    stdout.printf (\"Error: %s\\n\", e.message);",
143         "\t\t\t\t} \t\t",
144         "#else",
145         "\t\t\t\tvar cookie_jar = new Soup.CookieJar();\t\t",
146         "\t\t\t\tvar cookie = new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), \"/\", -1);",
147         "\t\t\t\tif(uri.get_scheme() ==\"https\") {",
148         "\t\t\t\t    cookie.set_secure(true);                    ",
149         "\t\t\t\t}\t    ",
150         "\t\t\t\tcookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.NEVER);\t\t\t\t ",
151         "\t\t\t\tcookie_jar.add_cookie(cookie);\t\t\t    ",
152         "\t\t\t\tWebKit.get_default_session().add_feature(cookie_jar);",
153         "#endif",
154         "\t\t\t\t\t        \t\t\t    \t\t\t    ",
155         "\t\t\t}",
156         "",
157         "\t\t} ",
158         "            // init method"
159        ],
160        "* pack" : "add",
161        "id" : "view",
162        "xtype" : "WebView",
163        "| bool printpdf" : [
164         "() {",
165         "",
166         "",
167         "",
168         "\t\tvar list = Gtk.PaperSize.get_paper_sizes(false);",
169         "\t    var psetup = new Gtk.PageSetup();",
170         "\t    for(var i = 0; i < list.length(); i++ ) {",
171         "\t        var entry = list.nth_data(i).copy();",
172         "\t        if (entry.get_name() == \"iso_a2\") {",
173         "\t            psetup.set_paper_size(entry);",
174         "\t        }",
175         "\t    }",
176         "\t    ",
177         "",
178         "#if GTK3\t ",
179         "",
180         "\t\tvar  pe = new  WebKit.PrintOperation(this.el);",
181         "\t\tpe.set_page_setup(psetup);",
182         "\t\tpe.finished.connect( () => {",
183         "\t\t\tprint(\"print completed\\n\");",
184         "            this.delete_cookiefile(); ",
185         "\t\t\t Gtk.main_quit();",
186         "    \t\t",
187         "\t\t});",
188         "\t\t",
189         "\t\tvar ps = new Gtk.PrintSettings();",
190         "\t    ps.set_printer(\"Print to File\");",
191         "\t    ps.set(\"output-file-format\", \"pdf\");",
192         "\t    ps.set(\"output-uri\", \"file://\" + webkitpdf.opt_target_pdf);",
193         "",
194         "\t\t",
195         "\t\tpe.set_print_settings(ps);",
196         "\t\t",
197         "",
198         "\t\tpe.print();",
199         "            \t\t  ",
200         "",
201         "#else\t    ",
202         "\t\tvar  pe = new  Gtk.PrintOperation();",
203         "\t\tpe.export_filename = webkitpdf.opt_target_pdf;",
204         "\t\tprint(\"got callback for print\\n\");",
205         "\t\tpe.ref();",
206         "\t\t/*pe.failed.connect(() => {",
207         "\t\t\tprint(\"print failed\\n\");",
208         "\t\t\t Gtk.main_quit();",
209         "    \t\t",
210         "\t\t})",
211         "\t\t*/;",
212         "\t\t pe.done.connect(() => {",
213         "\t\t\tprint(\"print completed\\n\");",
214         "            this.delete_cookiefile(); ",
215         "\t\t\t Gtk.main_quit();",
216         "    \t\t",
217         "\t\t});",
218         "\t\t",
219         "\t\t//pe.run_dialog(_this.el);",
220         "\t ",
221         "\t\t",
222         "\t        ",
223         "",
224         "",
225         "\t    pe.set_default_page_setup(psetup);",
226         "\t    print(\"Calling Print?\\n\");",
227         "\t    //pe.run_dialog(_this.el);",
228         "\t    ",
229         "\t    ",
230         "",
231         "",
232         "",
233         "\t    try {",
234         "\t\t    this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);",
235         "\t    } catch (Error e) {",
236         "\t\t\t// print failed...",
237         "\t\t}",
238         "#endif\t    ",
239         "\t    ",
240         "\t     ",
241         "\t\treturn false;",
242         "}",
243         ""
244        ],
245        "| void delete_cookiefile" : [
246         "() {",
247         " \tif(this.cookie_file.length < 1 ){",
248         " \t\treturn;",
249         "\t}",
250         "    print(\"deleting tmp file %s\\n\",this.cookie_file);",
251         "    File file = File.new_for_path (this.cookie_file);",
252         "    try {",
253         "        if (file.query_exists () == true) {",
254         "           file.delete ();\t             ",
255         "        }",
256         "    } catch (Error e) {",
257         "        stdout.printf (\"Error: %s\\n\", e.message);",
258         "    }",
259         "",
260         "}",
261         ""
262        ],
263        "| void printit" : [
264         "() {",
265         "   print(\"load_finsihed\\n\");",
266         "   ",
267         "   if (this.print_started) {",
268         "       print(\"load_finsihed - already printing..%d %s\\n\", this.load_try, this.el.uri);",
269         "   ",
270         "      ",
271         "      ",
272         "      ",
273         "      return;",
274         "  }",
275         "   ",
276         " ",
277         "    this.print_started = true; // flag to stop double call.",
278         "    ",
279         "    ",
280         "\t// inject any javascript if needed..",
281         "\tif (webkitpdf.opt_inject_js != null) {",
282         "\t",
283         "\t\tprint(\"injecting javascript files \\n\");",
284         "\t\tforeach(var inject_js in  webkitpdf.opt_inject_js) {",
285         "\t\tprint(\"inject? %s\\n\", inject_js);",
286         "\t\t\tstring str;",
287         "\t\t\ttry {",
288         "\t\t\t\tFileUtils.get_contents( inject_js, out str);",
289         "\t\t\t} catch(FileError e) {",
290         "\t\t\t\tprint(\"Failed to inject %s\\n\",  inject_js);",
291         "\t\t\t\tcontinue;",
292         "\t\t\t}",
293         "#if GTK3",
294         "\t\t\tGLib.MainLoop mainloop = new GLib.MainLoop();",
295         "\t\t\tthis.el.run_javascript.begin(str, null, (obj,res) => {",
296         "\t\t\t\tmainloop.quit();",
297         "\t\t\t});",
298         "\t\t\tmainloop.run();",
299         "\t\t\t",
300         "#else \t\t\t",
301         "\t\t\tthis.el.execute_script(str);",
302         "#endif",
303         "\t\t}",
304         "\t}",
305         "\t  ",
306         "\tprint(\"calling print in %d seconds\\n\", (int)webkitpdf.opt_delay);",
307         "\tif (webkitpdf.opt_target_pdf != null) {",
308         "\t\tGLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { ",
309         "\t\t\tprint(\"delay done URL: %s\\n\", this.el.uri);",
310         "\t\t",
311         "\t\t\t ",
312         "\t\t\tthis.printpdf();",
313         "\t\t\t return false;",
314         "\t\t}, GLib.Priority.DEFAULT);",
315         "\t\treturn;",
316         "\t}",
317         "\t\t",
318         "",
319         "#if GTK3",
320         "\tGLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { ",
321         "\t\t\tprint(\"delay done URL: %s\\n\", this.el.uri);",
322         "\t\t\tthis.printpng();\t",
323         "\t\t\treturn false;",
324         "\t\t}, GLib.Priority.DEFAULT);",
325         "\t\treturn;",
326         "#else",
327         "\t\t",
328         "    GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { ",
329         "   ",
330         "\t\t",
331         "\t\t// ----------------- JPEG ",
332         "\t\t// resize the window...",
333         "\t\t// window.document.documentElement.scrollHeight",
334         "\t\tvar scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;",
335         "\t\tprint(\"Scroll height %d\\n\", scroll_height);",
336         "\t\tif (scroll_height> 1024 || webkitpdf.opt_width > 0  || webkitpdf.opt_height > 0  ) {",
337         "\t\t\tvar w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;",
338         "\t\t\tvar h = int.min(scroll_height, 6000);",
339         "\t\t\t_this.scrolled_window.el.set_size_request( w, h); ",
340         "\t\t\t_this.el.resize (w+50, h+50);",
341         "\t\t\tprint(\"Resize to %d, %d\\n\", w,h);",
342         "\t\t    GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { ",
343         "\t\t\t\tthis.printpng();",
344         "\t\t\t\treturn false;",
345         "\t\t\t  }, GLib.Priority.DEFAULT);",
346         "  \t\t    return false;",
347         "\t\t}",
348         "\t\tthis.printpng();\t\t",
349         "\t\treturn false;",
350         "\t  }, GLib.Priority.DEFAULT);",
351         "",
352         "",
353         "#endif    ",
354         "    ",
355         "}",
356         ""
357        ],
358        "| void printpng" : [
359         "() {",
360         "    //var filename = \"/tmp/test.pdf\";",
361         "    //print(\"load_changed %d ?= %d\\n\", le, LoadEvent.FINISHED);",
362         "",
363         "    //if (le != LoadEvent.FINISHED) {",
364         "    //    return;",
365         "   // }",
366         "   ",
367         "// what size is the documet.",
368         "",
369         "     print(\"making screenshot\\n\");",
370         "#if GTK3",
371         "GLib.MainLoop mainloop = new GLib.MainLoop();",
372         "  _this.view.el.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {",
373         "  \t\t var sf = _this.view.el.get_snapshot.end(res);",
374         "  \t\t sf.write_to_png(webkitpdf.opt_target_png);",
375         "     \tprint(\"Saved to %s\\n\", webkitpdf.opt_target_png);",
376         "  \t\t mainloop.quit();",
377         "  \t\t ",
378         "  });",
379         "",
380         "\tmainloop.run();",
381         "",
382         "#else     ",
383         "    // fix vapi - get_snapshot add '?' to all null.",
384         "    var pixmap = _this.view.el.get_snapshot( null );",
385         "",
386         "    ",
387         "    ",
388         "    int w,h;",
389         "    ",
390         "    // add out to gdkpixmap.get_size",
391         "\tpixmap.get_size( out  w , out   h);",
392         "\tvar pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);",
393         "\tprint(\"pixbuf size: %d x %d\\n \", w,h);",
394         "\t ",
395         "    ",
396         "    ",
397         "     pixbuf.save(webkitpdf.opt_target_png, \"png\");",
398         "     print(\"Saved to %s\\n\", webkitpdf.opt_target_png);",
399         "    //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {",
400         "    //    var sf = view.get_snapshot.end(res);",
401         "",
402         "    //    sf.write_to_png(Browser.opt_target_png);",
403         "    //});",
404         "#endif       ",
405         "   this.delete_cookiefile(); ",
406         "   ",
407         "    Gtk.main_quit();",
408         "     ",
409         "",
410         "    ",
411         "    ",
412         "",
413         "}",
414         ""
415        ]
416       }
417      ],
418      "xtype" : "ScrolledWindow"
419     }
420    ],
421    "listeners" : {
422     "destroy" : [
423      "() => {",
424      "    Gtk.main_quit();",
425      "}"
426     ],
427     "show" : [
428      "() => {",
429      "\t",
430      "\tprint(\"resizing window\\n\");",
431      "\tvar w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;",
432      "\tvar h = webkitpdf.opt_height > 0 ?  webkitpdf.opt_height  :  500;",
433      "\t\t\t",
434      "\t",
435      "\tthis.el.resize(w,h);",
436      "",
437      "\t// we have to do silly stuff with blogspot!!?!?",
438      "\tvar blog = webkitpdf.opt_url;",
439      "",
440      "",
441      "  /* Do not redirect if the domain is .com already */",
442      "  if (/\\.blogspot\\.com/.match(blog)) {",
443      "\t\tvar r =  new Regex (\"\\\\.blogspot\\\\..*?/\");",
444      "\t\tblog = r.replace(blog, blog.length, 0, \".blogspot.com/ncr/\");",
445      "  }",
446      "\t",
447      "\tprint(\"loading url %s\\n\",blog);\t",
448      "\tthis.view.el.load_uri(blog);",
449      "\t ",
450      "\t// if we have not finished in 25 seconds",
451      "\t// call printit...",
452      "\tvar cd = webkitpdf.opt_delay > 25 ? webkitpdf.opt_delay : 25;",
453      "\t  GLib.Timeout.add_seconds(1, () => { ",
454      "\t\tcd--;",
455      "\t\tprint(\"timeout %d seconds :%d\", (int)( webkitpdf.opt_delay > 25 ? webkitpdf.opt_delay : 25), (int)cd);",
456      "\t\tif (cd < 1) {",
457      "\t\t\tprint(\"calling printit - timed out\");",
458      "\t\t\t_this.view.printit();",
459      "\t\t\treturn false;\t\t\t",
460      "\t\t}",
461      "\t\treturn true;",
462      "",
463      "\t});",
464      "\t",
465      "",
466      "}"
467     ]
468    },
469    "xtype" : "Window"
470   }
471  ],
472  "modOrder" : "",
473  "name" : "BrowserWindow",
474  "parent" : "",
475  "path" : "/home/alan/gitlive/app.webkitpdf/BrowserWindow.bjs",
476  "permname" : "",
477  "title" : ""
478 }