dfed43b64e380cebac52f815d14eb3da5580cab9
[app.wkmirror] / BrowserView.js
1 Gtk = imports.gi.Gtk;
2 WebKit = imports.gi.WebKit;
3
4 TabbedBrowser = imports.TabbedBrowser;
5 File = imports.File.File;
6
7 base64 = imports.base64.base64;
8
9 BrowserView = new GType({
10     parent: WebKit.WebView.type,
11     name: "BrowserView",
12     init: function ()
13     {
14         // Private
15         
16         
17         var _t = this;
18         
19         var tab;
20         var browsePage = false;
21         var maxQueue = 0;
22         var injected = {};
23         
24         var storedir = '/home/alan/wkqueue';
25         if (!File.exists(storedir)) {
26             File.mkdir(storedir);
27         }
28         var parsedir = storedir + '/parse_queue';
29         var downloaddir = storedir + '/download_queue';
30         var donedir = storedir + '/downloaded_queue';
31         if (!File.exists(parsedir)) {
32             File.mkdir(parsedir);
33         }
34         if (!File.exists(downloaddir)) {
35             File.mkdir(downloaddir);
36         }
37         if (!File.exists(donedir)) {
38             File.mkdir(donedir);
39         }
40         
41         var update_title = function (web_view, web_frame, title)
42         {
43             if(title.length > 25)
44                 title = title.slice(0,25) + "...";
45
46             tab.get_tab_label().label = title;
47         };
48
49         var update_url = function (web_view, web_frame)
50         {
51             var toolbar = tab.get_toolbar();
52
53             toolbar.set_url(web_frame.get_uri());
54             toolbar.set_can_go_back(web_view.can_go_back());
55             toolbar.set_can_go_forward(web_view.can_go_forward());
56         };
57
58         var update_progress = function (bar, progress)
59         {
60             tab.get_toolbar().set_progress(progress / 100);
61         };
62
63         var create_new_tab = function (web_view, web_frame, new_web_view)
64         {
65             new_web_view = new BrowserView();
66             new_web_view.signal.web_view_ready.connect(show_new_tab);
67             return new_web_view;
68         };
69
70         var show_new_tab = function (new_web_view)
71         {
72             TabbedBrowser.browser.new_tab("", new_web_view);
73
74             return false;
75         };
76
77         var hover_link = function (web_view, link, url)
78         {
79             tab.get_statusbar().set_status(url);
80         };
81
82         
83         
84         
85         this.add_inject = function(force)
86         {
87             
88             if (force || (typeof(injected[this.uri]) == 'undefined' )) {
89                 injected[this.uri] = 0;
90             }
91             if (injected[this.uri] > 2) {
92                 return;
93             }
94             injected[this.uri]++;
95             var fn = __script_path__ + "/inject.js";
96             if (File.exists(fn)) {
97                 print("Adding inject");
98                 var newjs = File.read(__script_path__ + "/inject.js");
99                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
100                     newjs
101                     
102                 );
103             }
104             
105         }
106          this.add_nsinject = function(force)
107         {
108             
109             //if (force || (typeof(injected[this.uri]) == 'undefined' )) {
110             //    injected[this.uri] = 0;
111             //}
112             //if (injected[this.uri] > 2) {
113             //    return;
114             //}
115             //injected[this.uri]++;
116             var fn = __script_path__ + "/nsinject.js";
117             //if (File.exists(fn)) {
118                 print("Adding inject");
119                 var newjs = File.read(__script_path__ + "/nsinject.js");
120                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
121                     newjs
122                     
123                 );
124             //}
125             
126         }
127         
128         
129         
130         
131         
132         var load_finished = function ()
133         {
134             print("load finished");
135             tab.get_toolbar().set_progress(0);
136             
137             
138             
139             
140             _t.add_inject(true);
141           
142             _t.add_nsinject(true);
143               print(typeof(Seed.argv[2]));
144             if (Seed.argv[2] == null) {
145                 print("NEED ID!");
146                 Seed.quit();
147             
148             }
149             _t.nsdownloadNext(Seed.argv[2]  * 1)
150           
151             
152             if (browsePage) {
153                 print("onload: calling gather links");
154                 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
155                     'BrowserMirror.gatherlinks();'
156                     
157                 );
158                 
159             }
160             
161             
162         };
163
164         var load_committed = function (web_view, web_frame)
165         {
166             update_url(web_view, web_frame);
167         };
168
169         var clicked_link = function (web_view, web_frame, request,
170                                      action, decision, window)
171         {
172             if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
173                action.get_button() == 2)
174             {
175                 browser.new_tab(request.get_uri(), null);
176                 return true;
177             }
178
179             return false;
180         };
181
182         // Public
183         
184         
185         this.browse = function (url)
186         {
187             if(url.search("://") < 0)
188                 url = "http://" + url;
189
190             this.open(url);
191         };
192
193         this.set_tab = function (new_tab)
194         {
195             tab = new_tab;
196         };
197
198         this.get_tab = function ()
199         {
200             return tab;
201         };
202
203
204         this.downloadqueue = function()
205         {
206             var filesList = File.list(downloaddir);
207             print("DOWNLOAD QUEUE LENGTH: " + filesList.length);
208             if (!maxQueue || maxQueue < filesList.length) {
209                 maxQueue  = filesList.length;
210             }
211             
212             tab.get_toolbar().set_progress(filesList.length / Math.max(maxQueue, filesList.length ));
213             if (filesList == null || filesList.length == 0  ){
214                 
215                 return false;
216             }  
217             var url = decodeURIComponent(filesList[0]);
218             if (!this.checkdomain(url)) {
219                 print("SKIP (external domain) : " + url);
220                 File.remove(downloaddir + '/' + filesList[0]);
221                 return this.downloadqueue();
222                 
223             }
224             this.downloadhead( url);
225             return true;
226             
227         };
228         
229     
230         this.queuerun = function()
231         {
232             if (this.downloadqueue()) return true;
233             return this.parsequeue();
234         };
235         
236         this.parsequeue = function(){
237             var filesList = File.list(parsedir);
238             if (filesList == null || filesList.length == 0  ){
239                 return false;
240             } 
241             
242             browsePage = decodeURIComponent(filesList[0]);
243             print("parsing page:" + browsePage );
244             this.browse(browsePage);
245             
246              
247             
248             
249             
250             return true;
251         };
252         
253         
254         this.downloadpage = function(link){
255             print("calling download page: " + link);
256             _t.add_inject(); // just in case..
257                 //var url = File.read(__script_path__+"/downloadqueue/"+link);
258             this.execute_script(
259                 "BrowserMirror.downloadpage(" + JSON.stringify(link) +");"
260             );          
261                 
262             
263         };
264  
265         this.downloadhead = function(link){
266             print("calling download head: " + link);
267             _t.add_inject(); // just in case..
268                 //var url = File.read(__script_path__+"/downloadqueue/"+link);
269             this.execute_script(
270                 "BrowserMirror.downloadhead(" + JSON.stringify(link) +");"
271             );          
272                  
273         };
274         var nsqueue = false;
275         this.nsdownloadNext = function()
276         {
277             /*if (nsqueue === false) {
278                 nsqueue = [
279                            //282,273,285,395,271,272,278,394,402,279,432,280,284,281,433,437,404,444,151,152,1,283,287,288,418,407,398,449,147,150,131,149,443,148,411,405,415,417,416,406,408,399,410,409,412,400,155,133,124,157,118,183,450,434,301,435,130,286,162,292,431,161,290,146,145,158,159,120,137,128,139,163,293,160,289,291,141,140,138,142,144,110,401,156,154,153,170,167,295,166,294,277,425,164,165,168,296,169,297,113,171,119,180,300,176,175,172,302,299,
280                            //298,421,187,307,428,275,269,304,181,182,174,173,184,303,112,179,114,305,177,178,186,306,403,420,111,109,309,188,308,446,445,310,311,53,189,52,312,419,270,190,191,192,193,54,194,108,268,121,195,206,396,205,207,324,220,340,212,331,219,339,222,342,436,209,327,424,208,326,210,328,330,325,211,329,218,337,338,224,223,217,336,214,333,213,332,216,335,221,341,215,334,197,314,
281                             //196,313,198,316,201,320,317,204,323,203,322,202,321,200,319,199,318,346,58,267,393,265,391,438,264,390,261,387,262,388,260,386,266,392,413,414,263,389,347,225,343,227,345,226,344,422,228,348,237,358,231,352,229,349,351,233,354,238,359,360,239,361,230,350,232,353,234,355,439,236,357,240,362,235,356,254,380,257,383,259,385,255,381,256,448,382,258,384,374,252,375,376,251,373,378,429,430,379,253,244,366,247,
282                            //369,363,242,364,241,243,365,246,368,245,367,377,427,
283                            426,250,447,372,249,371,248,370,442,397,129,441,440,116,122,123,125,126,136,115,117,127
284                         ];
285             }
286             
287             if (!nsqueue.length) {
288                 print("DONE");
289                 return;
290             }
291             var pg = nsqueue.shift() ;
292             
293             
294             */
295             
296             //print(typeof(Seed.argv[2]));
297             if (!Seed.argv[2]) {
298                 print("NEED ID!");
299                 Seed.quit();
300             
301             }
302             var pg = Seed.argv[2]  * 1;
303             if ( File.exists( storedir+'/output/' + pg + '.csv')) {
304                 print("DONE : " + storedir+'/output/' + pg + '.csv');
305                 Seed.quit();
306                 return;
307             }
308             
309             var pd = JSON.parse(File.read('/home/alan/.nspasswd'));
310             
311             print("downloadnext : " + pg);
312             this.execute_script(
313                 'NS.login(' +  JSON.stringify(pd.username) + ',' + JSON.stringify(pd.password) + ',' + pg + ' );'
314                 
315             );
316             
317             
318             
319         }
320
321         // Implementation
322         //this.set_scroll_adjustments(null, null);
323
324         this.signal.title_changed.connect(update_title);
325         this.signal.load_committed.connect(load_committed);
326         this.signal.load_finished.connect(load_finished);
327         this.signal.load_progress_changed.connect(update_progress);
328         print(WebKit.WebFrame);
329         var mv = WebKit.WebFrame.webkit_web_view_get_main_frame(WebKit.WebView);
330 //var db = WebKit.get_web_plugin_database ();
331 //var pg = db.get_plugins();
332 print(mv);
333 print("plugins");
334 //for(var i = 0 ; i < pg.length; i++) {
335
336 //print(pg[i].get_name());
337 //print(pg[i].get_enabled() *1);
338 //print(pg[i].get_description() *1);
339
340 //}
341         // For some reason, this segfaults seed in the instance init closure handler
342         // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
343         //this.signal.navigation_policy_decision_requested.connect(clicked_link);
344
345         this.signal.hovering_over_link.connect(hover_link);
346
347         this.signal.create_web_view.connect(create_new_tab);
348         
349          
350         print("ADDing console message sig handler");
351         
352         
353         
354         
355         this.signal.console_message.connect(function(wv, msg, line, sid) {
356             // print('BrowserView.js got ' + msg);
357             var methodname;
358             var ret;
359             try {
360                 ret = JSON.parse(msg);
361             
362             } catch(e) {
363                 print("GOT INVALID message:" + msg)
364                 return true;
365                 
366                 
367             }
368             print("got method : " + ret.method);
369             
370             if (ret.method == 'exit') {
371                 Seed.quit();
372             }
373             
374             
375             if (ret.method == 'nsdownloadpage'){
376                 try { 
377                     var mt = ret.contentType.split(';').shift();
378                 } catch( e) {
379                     print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
380                     mt='';
381                 }
382                 
383                 switch(mt) {
384                    
385                     case '':
386                         print("CONSOLE GOT BLANK - call download next?");
387                           Seed.quit();
388                          //_t.nsdownloadNext();
389                        // _t.moveToDone(ret.requesturl);
390                         break;
391                     
392                     default:
393                         var info = false;
394                         var info_f = _t.dupeCheck(ret.requesturl);
395                         if (info_f) {
396                             info  = JSON.parse(File.read(info_f));
397                         }
398                          var target  = storedir+'/output/' + ret.filename 
399                          // flag it as done..
400                        
401                         
402                         
403                         
404                         
405                         //File.write(target ,decodeURIComponent(escape(base64.decode( ret.data))));
406                         print("GOT array sized: " + ret.data.length);
407                         
408                         File.writeBinaryArray(target,ret.data);
409                         print("Wrote to file: " + target);
410                         // get next..
411                         return true;
412                         //Seed.quit();
413                         //_t.nsdownloadNext();
414                         
415                         
416                         
417                         break;
418                 }
419                 return true; //???
420             }
421             
422             if (ret.method == 'gatherlinks'){
423                 // flag the page as parsed.
424                 
425                 if (browsePage) {
426                     _t.moveToDone(browsePage);
427                     
428                 }
429                 var sourcePage = browsePage;
430                 browsePage = false;
431                 print(typeof(ret.data));
432                 if (typeof(ret.data) != 'object' ) {
433                     print("GOT INVALID DATA?:" + JSON.stringify(ret,null,4));
434                     ret.data= [];
435                     
436                 }
437                 ret.data.forEach(function(ln) {
438                     if (!ln.href.match(/^http[s]*:\/\//)) {
439                         print("SKIP link: " + ln.href);
440                         return;
441                     }
442                     ln.href= ln.href.replace(/#.*$/,'');
443                     
444                     if (!_t.checkdomain(ln.href) ) {
445                         print("SKIP link (external domain): " + ln.href);
446                         return;
447                     }
448                     
449                     // this is just for our purposes..
450                     
451                     if (ln.href.match(/\/pages\/[0-9]+$/)) {
452                         print("SKIP link (ingore unnamed pages): " + ln.href);
453                         return;
454                     }
455                     
456                     
457                     
458                     var fn = encodeURIComponent(ln.href);
459                     
460                     var dupe  = _t.dupeCheck(ln.href);
461                     if (dupe) {
462                         
463                         if (dupe == downloaddir + '/'  + fn) {
464                             var info = JSON.parse(File.read(dupe));
465                             if (info && info.fromUrl && info.fromUrl.length > sourcePage.length) {
466                                 print("SKIP link (in queue already): " + ln.href);
467                                 return;
468                             }
469                             print("found a longer link for url")
470                         } else {
471                             print("SKIP link (in another queue): " + ln.href);
472                             return;
473                         }
474                     }
475                      
476                     File.write(downloaddir + '/'  + fn, JSON.stringify( {
477                         label : ln.label,
478                         fromURL : sourcePage
479                     })); // write an empyt file indicating it needs downloading..
480                 });         
481                 var filesList = File.list(downloaddir);
482             
483                 maxQueue =  filesList.length;
484             }
485             
486             if (ret.method == 'downloadpage'){
487                 // got the results from download page:
488                 // requesturl 
489                 // remove from downloadqueue.
490              
491                 
492                 //
493                 try { 
494                     var mt = ret.contentType.split(';').shift();
495                 } catch( e) {
496                     print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
497                     mt='';
498                 }
499                 
500                 switch(mt) {
501                    
502                     case '':
503                         _t.moveToDone(ret.requesturl);
504                         break;
505                     
506                     default:
507                         var info = false;
508                         var info_f = _t.dupeCheck(ret.requesturl);
509                         if (info_f) {
510                             info  = JSON.parse(File.read(info_f));
511                         }
512                     
513                         _t.moveToDone(ret.requesturl);
514                         // flag it as done..
515                        
516                         // write it...
517                         var target  = _t.toFilename(ret.requesturl);
518                         if (info && info.fromURL) {
519                             var bn = decodeURIComponent(File.basename(target));
520                             target = _t.toFilename(info.fromURL+'/'+ bn);
521                         }
522                         
523                         
524                         
525                         
526                         //File.write(target ,decodeURIComponent(escape(base64.decode( ret.data))));
527                         print("GOT array sized: " + ret.data.length);
528                         
529                         File.writeBinaryArray(target,ret.data);
530                         print("Wrote to file: " + target);
531                         break;
532                 }
533                 
534                 // if it's HTML then add it to parse queue
535                 // otehrwise save it.. and run the queue again.
536                
537                 
538                 //File.write(storedir+"/parsequeue/"+Math.random(), msg);
539             }
540             
541            
542             if (ret.method == 'downloadhead'){
543                 // got the results from download page:
544                 // requesturl 
545                 // remove from downloadqueue.
546                 
547                 //
548                 try { 
549                     var mt = ret.contentType.split(';').shift();
550                 } catch( e) {
551                     print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
552                     mt='';
553                 }
554                 
555                 switch(mt) {
556                     case 'text/html':
557                         // add to parse QUEUE..
558                         print("moving to parse queue");
559                         _t.moveToParse(ret.requesturl);
560                         break;
561                     // stuf we do not care about..
562                     case 'application/atom+xml':
563                     case '':
564                         print("moving to done queue");
565                          _t.moveToDone(ret.requesturl);
566                         break;
567                     
568                     default:
569                         print("calling download file");
570                         _t.downloadpage( ret.requesturl );
571                         // keep it on the queue..
572                         // do not run the queue..
573                         return true;
574                      
575                 }
576                 
577                 // if it's HTML then add it to parse queue
578                 // otehrwise save it.. and run the queue again.
579                
580                 
581                 //File.write(storedir+"/parsequeue/"+Math.random(), msg);
582             }
583             _t.queuerun();
584             
585             return true;
586         });
587         
588         
589         this.toFilename = function(url)
590         {
591             url = url.replace(/^http[s]*:\/\//, '');
592             var p = url.split('/');
593             p.unshift(storedir+'/output');
594             for (var i =1 ;i < p.length; i++) {
595                 p[i] = encodeURIComponent(p[i]);
596             
597             }
598             p[p.length-1] = decodeURIComponent(p[p.length-1]);
599             ret = p.join('/');
600             var dir = File.dirname(ret);
601             File.mkdirall(dir);
602             return ret;
603             
604         }
605         this.checkdomain = function(comp)
606         {
607             var b = parseUri(this.uri);
608             var d = parseUri(comp);
609             return (d.host == b.host && d.protocol == b.protocol);
610             
611             
612         }
613         
614         this.dupeCheck = function(url)
615         {
616             
617            // order - return highest up the queue first..
618             if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
619                 return downloaddir +'/' + encodeURIComponent(url);
620             }
621              if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
622                 return parsedir +'/' + encodeURIComponent(url);
623             }
624             if (File.exists(donedir +'/' + encodeURIComponent(url))) {
625                 return donedir +'/' + encodeURIComponent(url);
626             }
627             return  false;
628             
629             
630         }
631         this.moveToParse = function(url)
632         {
633             var old = this.dupeCheck(url);
634             var target =parsedir +'/' + encodeURIComponent(url);
635             if (old == target) {
636                 return;
637             }
638             File.write(target, old ? File.read(old) : '');
639             if (old) {
640                 File.remove(old);
641             }
642             
643         }
644         
645         this.moveToDownload= function(url)
646         {
647             var old = this.dupeCheck(url);
648             var target =downloaddir +'/' + encodeURIComponent(url);
649             if (old == target) {
650                 return;
651             }
652             File.write(target, old ? File.read(old) : '');
653             if (old) {
654                 File.remove(old);
655             }
656             
657         }
658         this.moveToDone= function(url)
659         {
660             var old = this.dupeCheck(url);
661             var target = donedir +'/' + encodeURIComponent(url);
662             if (old == target) {
663                 return;
664             }
665             File.write(target, old ? File.read(old) : '');
666             if (old) {
667                 File.remove(old);
668             }
669             
670         }
671         
672     }
673 });
674
675 function parseUri (str) {
676         var     o   = parseUri.options,
677                 m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
678                 uri = {},
679                 i   = 14;
680
681         while (i--) uri[o.key[i]] = m[i] || "";
682
683         uri[o.q.name] = {};
684         uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
685                 if ($1) uri[o.q.name][$1] = $2;
686         });
687
688         return uri;
689 };
690
691 parseUri.options = {
692         strictMode: false,
693         key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
694         q:   {
695                 name:   "queryKey",
696                 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
697         },
698         parser: {
699                 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
700                 loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
701         }
702 };