BrowserView.js
[app.webkitpdf] / BrowserView.js
1 Gtk = imports.gi.Gtk;
2 GLib = imports.gi.GLib;
3 WebKit = imports.gi.WebKit;
4 Soup = imports.gi.Soup;
5
6 TabbedBrowser = imports.TabbedBrowser;
7 BrowserSettings = imports.BrowserSettings;
8 BrowserTab = imports.BrowserTab;
9
10 File = imports.File.File;
11
12 base64 = imports.base64.base64;
13
14 BrowserView = new GType({
15     parent: WebKit.WebView.type,
16     name: "BrowserView",
17     init: function ()
18     {
19         // Private
20         
21         var sess  = WebKit.get_default_session();
22         sess.proxy_uri = new Soup.URI.c_new('http://localhost:3128');       
23         var _t = this;
24         
25         var tab = BrowserTab;
26         var browsePage = false;
27         var maxQueue = 0;
28         var injected = {};
29         
30         var current_url = '';
31  
32         var update_title = function (web_view, web_frame, title)
33         {
34             
35             print("calling update title");
36             if(title.length > 25)
37                 title = title.slice(0,25) + "...";
38
39             tab.get_tab_label().label = title;
40         };
41         
42         
43         
44            
45         
46         var traversedom = function(doc, fn, sub ) {
47             print("TRAVERSE DOM?");
48             sub = sub || 0;
49             
50             var ret = '<HTML>';
51             
52             var cb =  function(s) {
53                 //print("CB:" + s);
54                 ret+=s;
55             };
56             cb.fn = fn;
57             cb.sub = sub +1;
58             print(doc);
59             print(doc.head);
60             print (doc.body);
61             
62              //Roo.select('body > div',true).each(function(el) {
63             traverseDOMTree(cb, doc.head, 1);
64             traverseDOMTree(cb, doc.body, 1);
65            //print(ret);   
66             File.write(cb.fn.replace(/\.html$/, '')+ (sub ? ('.' + sub  ) : '') + '.html' , ret +'</HTML>');
67         
68         };
69          
70         
71         
72         var traverseDOMTree = function(cb, currentElement, depth) {
73             if (currentElement) {
74                 
75                 //if (currentElement.class_name.match(/roo-dynamic/)) {
76                 //    return;
77                 //}
78                 //print(currentElement.node_name);
79                 var j;
80                 var nodeName = currentElement.node_name;
81                 var tagName = currentElement.tag_name;
82                 
83                 if  (nodeName == '#text') {
84                     cb(currentElement.node_value);
85                     return;
86                 
87                 }
88                 if(nodeName == 'BR'){
89                     cb("<BR/>");
90                     return;
91                 }
92                
93                 if (nodeName == 'SCRIPT') {
94                      
95                     return;
96                 }
97                 //if (nodeName == 'STYLE') {  return; }
98                 var i = 0;
99               // Prints the node tagName, such as <A>, <IMG>, etc
100                 var outNodeName = nodeName;
101                 if (nodeName == 'IFRAME') {
102                     //outNodeName  = 'DIV';
103                 }
104                 if (tagName) {
105                     
106                     
107                     
108                     
109                     var attr = [];
110                     
111                     for(i = 0; i < currentElement.attributes.length;i++) {
112                         var aname = currentElement.attributes.item(i).name;
113                         //if (aname =='class' || aname == 'style') {
114                         //    continue;
115                         //}
116                         if (aname =='src' && tagName == 'IFRAME') {
117                             continue;
118                         }
119                         
120                         attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
121                     }
122                     if (tagName == 'IFRAME') {
123                         attr.push('src="' + File.basename(cb.fn).replace(/\.html$/, '')+ '.' + cb.sub + '.html"' );
124                     }
125                     
126                     //if (nodeName == 'IFRAME') {
127                     //    attr.push('src="' + currentElement.attributes.item(i).value + embeded_file '"' );
128                     //}
129                     
130                     //var style = elClassToStyle(currentElement);
131                     //if (style.length) {
132                     //    attr.push('style="' + style + '"' );
133                     //}
134                     
135                     
136                     //if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
137                      //   cb("<DIV"+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
138                     //} else {
139                         cb("<"+outNodeName + ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
140                         
141                     //}
142                 } 
143                 else {
144                   cb("[unknown tag]");
145                 }
146                 
147                  if (nodeName == 'IFRAME') {
148                     
149                    
150                     traversedom(currentElement.content_document,  cb.fn,  cb.sub )
151                     cb.sub++;
152                     cb("</IFRAME>");
153                     return;
154                 }
155                 
156                 
157                 // Traverse the tree
158                 i = 0;
159                 var currentElementChild = currentElement.child_nodes.item(i);
160                 var allText = true;
161                 while (currentElementChild) {
162                     // Formatting code (indent the tree so it looks nice on the screen)
163                     
164                     if  (currentElementChild.node_name == '#text') {
165                        // if (currentElementChild.node_value.length) {
166                             cb(currentElementChild.node_value);
167                        // }
168                         
169                         i++;
170                         currentElementChild=currentElement.child_nodes.item(i);
171                         continue;
172                     }   
173                     allText = false;
174                     //cb("\n");
175                     //for (j = 0; j < depth; j++) {
176                       // &#166 is just a vertical line
177                     //  cb("  ");
178                     //}//               
179                     
180                         
181                     // Recursively traverse the tree structure of the child node
182                     traverseDOMTree(cb, currentElementChild, depth+1);
183                     i++;
184                     currentElementChild=currentElement.child_nodes.item(i);
185                 }
186                 if (!allText) {
187                     // The remaining code is mostly for formatting the tree
188                     //cb("\n");
189                     //for (j = 0; j < depth - 1; j++) {
190                       //cb("  ");
191                     //}     
192                 }
193                 if (tagName) {
194                    // if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
195                    //     cb("</DIV>");
196                    // } else {
197                         cb("</"+outNodeName+">");
198                    // }
199                     
200                 }
201             }
202         };
203         
204
205         var update_url = function (web_view, web_frame)
206         {
207             var toolbar = tab.get_toolbar();
208
209             toolbar.set_url(web_frame.get_uri());
210             toolbar.set_can_go_back(web_view.can_go_back());
211             toolbar.set_can_go_forward(web_view.can_go_forward());
212             
213             
214             
215             
216         };
217
218     
219       
220
221         var create_new_tab = function (web_view, web_frame, new_web_view)
222         {
223             new_web_view = new BrowserView();
224             new_web_view.signal.web_view_ready.connect(show_new_tab);
225             return new_web_view;
226         };
227
228         var show_new_tab = function (new_web_view)
229         {
230             TabbedBrowser.browser.new_tab("", new_web_view);
231
232             return false;
233         };
234
235         var hover_link = function (web_view, link, url)
236         {
237             tab.get_statusbar().set_status(url);
238         };
239          
240         /*
241         
242         this.add_inject = function(force)
243         {
244             
245             if (force || (typeof(injected[this.uri]) == 'undefined' )) {
246                 injected[this.uri] = 0;
247             }
248             if (injected[this.uri] > 2) {
249                 return;
250             }
251             injected[this.uri]++;
252             var fn = __script_path__ + "/inject.js";
253             if (File.exists(fn)) {
254 //                print("Adding inject");
255                 var newjs = File.read(__script_path__ + "/inject.js");
256                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
257                     newjs
258                     
259                 );
260             }
261             
262         }
263         */
264         var after_login = false;
265         
266         
267         var load_finished_called = false;
268         
269         var done_print = false;
270         var do_print = function(web_frame) {
271             
272             
273             if (done_print) {
274                 print("do_print called -- while already doing this...");
275                 return;
276             }
277             done_print = true;
278             print("do_print called");
279             
280             var scr_a =  " var r = document.getElementsByTagName('link');" +
281                 "for (var i=0;i < r.length;i++) { " +
282                     "var a = r[i]; " +
283                     "if (  a.getAttribute('media') == '')  { continue; } " +
284                     "if (  a.getAttribute('media') == 'screen')  { "+
285                         "a.removeAttribute('media'); continue;" +
286                     "} " +
287                     //"a.parentNode.removeChild(a);" +
288                 "}";
289             var scr_b =  "var a = document.body.querySelectorAll('*'); "+
290                 "var dnodes=[]; "+
291                 "for(var i =i;i<a.length;i++) { "+
292                 "   var cs = window.getComputedStyle(a[i],null);"+
293                 "   if ('none' == cs.display) {"+
294                 "    dnodes.push(a[i]); "+
295                 "  }"+ 
296                 "}"+
297                 "console.log(dnodes.length);" +
298                 "dnodes.forEach(function(n) { "+
299                 "    try {  n.parentNode.removeChild(n);   } catch(e) { } "+
300                 "});";
301             
302             
303             //TabbedBrowser.browser.current_tab().get_web_view().execute_script(
304             //    scr_a
305             //);
306             //print(scr_b);
307             //TabbedBrowser.browser.current_tab().get_web_view().execute_script(
308             //    scr_b
309             //);
310             
311             if (current_url.match(/\.coconuts\.co\//) ) {
312                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
313                     File.read( __script_path__ + "/domains/coconuts.co.js")
314                 );
315                               
316             }
317               if (current_url.match(/\.itnewsafrica\.com\//) ) {
318                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
319                     File.read( __script_path__ + "/domains/itnewsafrica.com.js")
320                 );
321             }
322             if (current_url.match(/\.indiatimes\.com\//) ) {
323                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
324                     File.read( __script_path__ + "/domains/indiatimes.com.js")
325                 );
326             }
327             
328             if (current_url.match(/\.inquirer\.net\//) ) {
329                 print("add inquirer.net code");
330                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
331                     File.read( __script_path__ + "/domains/inquirer.net.js")
332                 );
333                               
334             }
335             
336             
337             // motorme - requires a scroll to make content appear..
338             if (current_url.match(/\.motorme\.my\//) ) {
339                 TabbedBrowser.browser.current_tab().get_web_view().execute_script("window.scrollTo(0,500);"); 
340                               
341             }
342             
343             if (current_url.match(/\.efytimes\.com\//) ||  current_url.match(/\/efytimes\.com\//) ) {
344                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
345                     File.read( __script_path__ + "/domains/efytimes.com.js")
346                 );
347             }
348             
349            
350             print("Delay:" + BrowserSettings.delay);
351          
352             GLib.timeout_add(GLib.PRIORITY_DEFAULT, BrowserSettings.delay *1, function() {
353                 
354                 try {
355                     
356                     if (current_url.match(/theindependent\.sg\//) ) {
357                         TabbedBrowser.browser.current_tab().get_web_view().execute_script(
358                             File.read( __script_path__ + "/domains/theindependent.sg.js")
359                         );
360                                       
361                     }
362                     print("FETCHING");
363                     //print(web_frame);
364                     if (web_frame &&  BrowserSettings.export_filename_html  ) {
365                         var html = traversedom(web_frame.get_dom_document(), BrowserSettings.export_filename_html);
366                         //File.write(BrowserSettings.export_filename_html, html);
367                         
368                     } 
369                      
370                     if (!BrowserSettings.export_filename) {
371                         if (!BrowserSettings.export_filename_html) {
372                              print("no export filename / and html filename");
373                             return;
374                         }
375                         
376                         Seed.quit();
377                     }
378                     var mf = _t.get_main_frame();
379                      
380                     print("Creating paper");
381                     var ar = Gtk.PaperSize.get_paper_sizes();
382                     var psetup = new Gtk.PageSetup();
383                     for(var i = 0; i < ar.length; i++) {
384                         if (ar[i].get_name() =='iso_a2') {
385                             psetup.set_paper_size(ar[i]);
386                         }
387                     }
388                     print("doing print operation");
389                     var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
390                     print("doing print operation - set page"); 
391
392                     p.set_default_page_setup(psetup);
393                     
394                     print("doing print operation - print_full"); 
395                     mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
396                     print("made image - exiting");
397                     
398                     Seed.quit();
399                     return true;
400                 } catch(e) {
401                     print("error occured");
402                     print(JSON.stringify(e));
403                 }
404             });
405         }
406         
407         
408         var weibo_added = false;
409         
410         var is_weibo = false;
411         
412         
413         var load_finished = function (webkit, web_frame, wb)
414         {
415             
416            
417             
418             if (!is_weibo) {
419                 do_print(web_frame);
420                 return;
421             }
422              
423              
424              print("load finished");
425              if (weibo_added) {
426                  
427                  if (after_login === true) {
428                      print("do print");
429                      do_print();
430                      return;
431                      
432                  }
433                  if (after_login === false) {
434                      return; // not here?
435                  }
436                  print("adding timeout?");
437                  //return;
438                 GLib.timeout_add(GLib.PRIORITY_LOW, 2000, function() {
439                     print("Redirecting after login?" + after_login);
440                     
441                     _t.browse(after_login);
442                     after_login  = true;
443                 });
444                 return;
445              }
446             
447             weibo_added = true;
448             TabbedBrowser.browser.current_tab().get_web_view().execute_script(
449                 File.read( __script_path__ + "/weibo.js") 
450             );
451             print("run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");");
452     
453             TabbedBrowser.browser.current_tab().get_web_view().execute_script(
454                     "run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");"
455             );
456             return;
457     
458             //    return;
459             print(after_login);
460             if (after_login !== false) {
461                 
462 //                return;
463                 // wait a bit then load the real page..
464                 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
465                     print("Redirecting after login?");
466                     _t.browse(after_login);
467                 });
468                 
469                 return;
470             }
471          
472             if (load_finished_called) {
473                 return ;
474             
475             }
476             load_finished_called = true;
477             
478           
479             
480 //            if(document.location.host=='weibo.com') {
481 //                // clear login dialog from weibo.
482 //                //check and hidden the login dialog and overlay .....
483 //                var els = document.querySelectorAll('div[node-type]');
484 //                var bbc = [];
485 //                for (i = 0; i < els.length; i++) {
486 //                    if (els[i].hasAttribute('node-type')) {
487 //                        if(els[i].getAttribute('node-type') == 'outer'){
488 //                            bbc.push(els[i]);
489 //                        }
490 //                    }
491 //                }
492 //                for (i = 0; i < bbc.length; i++) {// hide all the outer.....
493 //                    bbc[i].style.display = 'none';
494 //                }
495 //                
496 //                //try to login 
497 //                if(document.location.pathname == '/login.php'){
498 //                    
499 //                    // fills in the user name and password 
500 //                    var els = document.querySelectorAll('input[node-type]');
501 //                    
502 //                    for (i = 0; i < els.length; i++) {
503 //                        if (els[i].hasAttribute('node-type')) {
504 //                            if(els[i].getAttribute('node-type') == 'username'){
505 //                                els[i].value = BrowserSettings.username;
506 //                            }
507 //                            if(els[i].getAttribute('node-type') == 'password'){
508 //                                els[i].value = BrowserSettings.passwd;
509 //                            }
510 //                        }
511 //                    }
512 //                    
513 //                    
514 //                    // simulating click event to login ....
515 //                    var elss = document.querySelectorAll('a[node-type]');
516 //                    
517 //                    for (i = 0; i < elss.length; i++) {
518 //                        if (elss[i].hasAttribute('node-type')) {
519 //                            if(elss[i].getAttribute('node-type') == 'submitBtn'){
520 //                                elss[i].click();
521 //                            }
522 //                        }
523 //                    }
524 //                    
525 //                    
526 //                }
527 //                
528 //            }
529             
530             
531             // clear login dialog from weibo.
532 //            TabbedBrowser.browser.current_tab().get_web_view().execute_script(
533 //                " if (document.location.host=='weibo.com') { " +
534 //                " try {  " + 
535 //                " var a = document.getElementsByClassName('W_layer')[0]; " + 
536 //                " a.parentNode.removeChild(a.previousSibling); " + 
537 //                " a.parentNode.removeChild(a); " + 
538 //                " } catch(e) { }  } " );
539             
540             
541             return;
542             GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay, function() {
543                  
544                 var mf = _t.get_main_frame();
545                 
546                 var ar = Gtk.PaperSize.get_paper_sizes();
547                 var psetup = new Gtk.PageSetup();
548                 for(var i = 0; i < ar.length; i++) {
549                     if (ar[i].get_name() =='iso_a2') {
550                         psetup.set_paper_size(ar[i]);
551                     }
552                 }
553                 
554                 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
555                 p.set_default_page_setup(psetup);
556                 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
557                 print("made image - exiting");
558                 
559                 Seed.quit();
560                 return true;
561             });
562             
563             return;
564         };
565  
566  
567  
568         
569         var load_committed = function (web_view, web_frame)
570         {
571             print("load commited");
572             
573             update_url(web_view, web_frame);
574              
575             // call load finished after 20seconds??
576             // so even if it never complets we try and print the thing.
577             GLib.timeout_add(GLib.PRIORITY_LOW, 40000, function() {
578                 print("load commited - 3000 ms?");
579                 load_finished();
580             });
581             
582         };
583
584         var clicked_link = function (web_view, web_frame, request,
585                                      action, decision, window)
586         {
587             if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
588                action.get_button() == 2)
589             {
590                 browser.new_tab(request.get_uri(), null);
591                 return true;
592             }
593
594             return false;
595         };
596
597         // Public
598         
599         
600         this.browse = function (url)
601         {
602             if(url.search("://") < 0)
603                 url = "http://" + url;
604             
605             current_url = url;
606             
607             print("BROWSE: " + url);
608             if (url.match(/\/weibo\.com\//) && after_login === false) {
609                 is_weibo = true;
610                 after_login = url;
611                 url = "http://weibo.com/login.php";
612             }
613             else  if (url.match(/\/weibo\.com\//) && after_login !== false) {
614                 after_login = false;
615             }
616             print("BROWSE -really: " + url);
617             this.open(url);
618         };
619
620         this.set_tab = function (new_tab)
621         {
622             tab = new_tab;
623         };
624
625         this.get_tab = function ()
626         {
627             return tab;
628         };
629
630     
631         // Implementation
632         //this.set_scroll_adjustments(null, null);
633         
634         //this.signal.title_changed.connect(update_title);
635         //this.signal.load_committed.connect(load_committed);
636         this.signal.load_finished.connect(load_finished);
637        
638         
639         // For some reason, this segfaults seed in the instance init closure handler
640         // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
641         //this.signal.navigation_policy_decision_requested.connect(clicked_link);
642
643         //this.signal.hovering_over_link.connect(hover_link);
644
645         this.signal.create_web_view.connect(create_new_tab);
646         
647         
648          
649 //        print("ADDing console message sig handler");
650         
651         
652          
653         
654         this.toFilename = function(url)
655         {
656             url = url.replace(/^http[s]*:\/\//, '');
657             var p = url.split('/');
658             p.unshift(storedir+'/output');
659             for (var i =1 ;i < p.length; i++) {
660                 p[i] = encodeURIComponent(p[i]);
661             
662             }
663             p[p.length-1] = decodeURIComponent(p[p.length-1]);
664             ret = p.join('/');
665             var dir = File.dirname(ret);
666             File.mkdirall(dir);
667             return ret;
668             
669         }
670         this.checkdomain = function(comp)
671         {
672             var b = parseUri(this.uri);
673             var d = parseUri(comp);
674             return (d.host == b.host && d.protocol == b.protocol);
675             
676             
677         }
678         
679         this.dupeCheck = function(url)
680         {
681             
682            // order - return highest up the queue first..
683             if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
684                 return downloaddir +'/' + encodeURIComponent(url);
685             }
686              if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
687                 return parsedir +'/' + encodeURIComponent(url);
688             }
689             if (File.exists(donedir +'/' + encodeURIComponent(url))) {
690                 return donedir +'/' + encodeURIComponent(url);
691             }
692             return  false;
693             
694             
695         }
696         this.moveToParse = function(url)
697         {
698             var old = this.dupeCheck(url);
699             var target =parsedir +'/' + encodeURIComponent(url);
700             if (old == target) {
701                 return;
702             }
703             File.write(target, old ? File.read(old) : '');
704             if (old) {
705                 File.remove(old);
706             }
707             
708         }
709         
710         this.moveToDownload= function(url)
711         {
712             var old = this.dupeCheck(url);
713             var target =downloaddir +'/' + encodeURIComponent(url);
714             if (old == target) {
715                 return;
716             }
717             File.write(target, old ? File.read(old) : '');
718             if (old) {
719                 File.remove(old);
720             }
721             
722         }
723         this.moveToDone= function(url)
724         {
725             var old = this.dupeCheck(url);
726             var target = donedir +'/' + encodeURIComponent(url);
727             if (old == target) {
728                 return;
729             }
730             File.write(target, old ? File.read(old) : '');
731             if (old) {
732                 File.remove(old);
733             }
734             
735         }
736         
737     }
738 });
739
740 function parseUri (str) {
741         var     o   = parseUri.options,
742                 m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
743                 uri = {},
744                 i   = 14;
745
746         while (i--) uri[o.key[i]] = m[i] || "";
747
748         uri[o.q.name] = {};
749         uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
750                 if ($1) uri[o.q.name][$1] = $2;
751         });
752
753         return uri;
754 };
755
756 parseUri.options = {
757         strictMode: false,
758         key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
759         q:   {
760                 name:   "queryKey",
761                 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
762         },
763         parser: {
764                 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
765                 loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
766         }
767 };