2 GLib = imports.gi.GLib;
3 WebKit = imports.gi.WebKit;
5 TabbedBrowser = imports.TabbedBrowser;
6 File = imports.File.File;
8 base64 = imports.base64.base64;
10 BrowserView = new GType({
11 parent: WebKit.WebView.type,
21 var browsePage = false;
25 var storedir = '/home/alan/wkqueue';
26 if (!File.exists(storedir)) {
29 var parsedir = storedir + '/parse_queue';
30 var downloaddir = storedir + '/download_queue';
31 var donedir = storedir + '/downloaded_queue';
32 if (!File.exists(parsedir)) {
35 if (!File.exists(downloaddir)) {
36 File.mkdir(downloaddir);
38 if (!File.exists(donedir)) {
42 var update_title = function (web_view, web_frame, title)
45 title = title.slice(0,25) + "...";
47 tab.get_tab_label().label = title;
50 var update_url = function (web_view, web_frame)
52 var toolbar = tab.get_toolbar();
54 toolbar.set_url(web_frame.get_uri());
55 toolbar.set_can_go_back(web_view.can_go_back());
56 toolbar.set_can_go_forward(web_view.can_go_forward());
59 var update_progress = function (bar, progress)
61 tab.get_toolbar().set_progress(progress / 100);
64 var create_new_tab = function (web_view, web_frame, new_web_view)
66 new_web_view = new BrowserView();
67 new_web_view.signal.web_view_ready.connect(show_new_tab);
71 var show_new_tab = function (new_web_view)
73 TabbedBrowser.browser.new_tab("", new_web_view);
78 var hover_link = function (web_view, link, url)
80 tab.get_statusbar().set_status(url);
86 this.add_inject = function(force)
89 if (force || (typeof(injected[this.uri]) == 'undefined' )) {
90 injected[this.uri] = 0;
92 if (injected[this.uri] > 2) {
96 var fn = __script_path__ + "/inject.js";
97 if (File.exists(fn)) {
98 print("Adding inject");
99 var newjs = File.read(__script_path__ + "/inject.js");
100 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
107 this.add_nsinject = function(force)
110 //if (force || (typeof(injected[this.uri]) == 'undefined' )) {
111 // injected[this.uri] = 0;
113 //if (injected[this.uri] > 2) {
116 //injected[this.uri]++;
117 var fn = __script_path__ + "/nsinject.js";
118 //if (File.exists(fn)) {
119 print("Adding inject");
120 var newjs = File.read(__script_path__ + "/nsinject.js");
121 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
133 var load_finished = function ()
135 print("load finished");
137 GLib.timeout_add(GLib.PRIORITY_LOW, 5000, function() {
138 var mf = _t.get_main_frame();
139 var p = new Gtk.PrintOperation({ export_filename : '/home/chris/test_pdf/test2.pdf' });
141 print(mf.print_full(p, Gtk.PrintOperationAction.EXPORT));
146 tab.get_toolbar().set_progress(0);
150 _t.add_nsinject(true);
151 print(typeof(Seed.argv[2]));
152 if (Seed.argv[2] == null) {
157 _t.nsdownloadNext(Seed.argv[2] * 1)
161 print("onload: calling gather links");
162 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
163 'BrowserMirror.gatherlinks();'
172 var load_committed = function (web_view, web_frame)
174 update_url(web_view, web_frame);
177 var clicked_link = function (web_view, web_frame, request,
178 action, decision, window)
180 if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
181 action.get_button() == 2)
183 browser.new_tab(request.get_uri(), null);
193 this.browse = function (url)
195 if(url.search("://") < 0)
196 url = "http://" + url;
201 this.set_tab = function (new_tab)
206 this.get_tab = function ()
212 this.downloadqueue = function()
214 var filesList = File.list(downloaddir);
215 print("DOWNLOAD QUEUE LENGTH: " + filesList.length);
216 if (!maxQueue || maxQueue < filesList.length) {
217 maxQueue = filesList.length;
220 tab.get_toolbar().set_progress(filesList.length / Math.max(maxQueue, filesList.length ));
221 if (filesList == null || filesList.length == 0 ){
225 var url = decodeURIComponent(filesList[0]);
226 if (!this.checkdomain(url)) {
227 print("SKIP (external domain) : " + url);
228 File.remove(downloaddir + '/' + filesList[0]);
229 return this.downloadqueue();
232 this.downloadhead( url);
238 this.queuerun = function()
240 if (this.downloadqueue()) return true;
241 return this.parsequeue();
244 this.parsequeue = function(){
245 var filesList = File.list(parsedir);
246 if (filesList == null || filesList.length == 0 ){
250 browsePage = decodeURIComponent(filesList[0]);
251 print("parsing page:" + browsePage );
252 this.browse(browsePage);
262 this.downloadpage = function(link){
263 print("calling download page: " + link);
264 _t.add_inject(); // just in case..
265 //var url = File.read(__script_path__+"/downloadqueue/"+link);
267 "BrowserMirror.downloadpage(" + JSON.stringify(link) +");"
273 this.downloadhead = function(link){
274 print("calling download head: " + link);
275 _t.add_inject(); // just in case..
276 //var url = File.read(__script_path__+"/downloadqueue/"+link);
278 "BrowserMirror.downloadhead(" + JSON.stringify(link) +");"
283 this.nsdownloadNext = function()
285 /*if (nsqueue === false) {
287 //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,
288 //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,
289 //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,
290 //369,363,242,364,241,243,365,246,368,245,367,377,427,
291 426,250,447,372,249,371,248,370,442,397,129,441,440,116,122,123,125,126,136,115,117,127
295 if (!nsqueue.length) {
299 var pg = nsqueue.shift() ;
304 //print(typeof(Seed.argv[2]));
310 var pg = Seed.argv[2] * 1;
311 if ( File.exists( storedir+'/output/' + pg + '.csv')) {
312 print("DONE : " + storedir+'/output/' + pg + '.csv');
317 var pd = JSON.parse(File.read('/home/alan/.nspasswd'));
319 print("downloadnext : " + pg);
321 'NS.login(' + JSON.stringify(pd.username) + ',' + JSON.stringify(pd.password) + ',' + pg + ' );'
330 //this.set_scroll_adjustments(null, null);
332 this.signal.title_changed.connect(update_title);
333 this.signal.load_committed.connect(load_committed);
334 this.signal.load_finished.connect(load_finished);
335 this.signal.load_progress_changed.connect(update_progress);
337 // For some reason, this segfaults seed in the instance init closure handler
338 // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
339 //this.signal.navigation_policy_decision_requested.connect(clicked_link);
341 this.signal.hovering_over_link.connect(hover_link);
343 this.signal.create_web_view.connect(create_new_tab);
346 print("ADDing console message sig handler");
351 this.signal.console_message.connect(function(wv, msg, line, sid) {
352 // print('BrowserView.js got ' + msg);
356 ret = JSON.parse(msg);
359 print("GOT INVALID message:" + msg)
364 print("got method : " + ret.method);
366 if (ret.method == 'exit') {
371 if (ret.method == 'nsdownloadpage'){
373 var mt = ret.contentType.split(';').shift();
375 print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
382 print("CONSOLE GOT BLANK - call download next?");
384 //_t.nsdownloadNext();
385 // _t.moveToDone(ret.requesturl);
390 var info_f = _t.dupeCheck(ret.requesturl);
392 info = JSON.parse(File.read(info_f));
394 var target = storedir+'/output/' + ret.filename
401 //File.write(target ,decodeURIComponent(escape(base64.decode( ret.data))));
402 print("GOT array sized: " + ret.data.length);
404 File.writeBinaryArray(target,ret.data);
405 print("Wrote to file: " + target);
409 //_t.nsdownloadNext();
418 if (ret.method == 'gatherlinks'){
419 // flag the page as parsed.
422 _t.moveToDone(browsePage);
425 var sourcePage = browsePage;
427 print(typeof(ret.data));
428 if (typeof(ret.data) != 'object' ) {
429 print("GOT INVALID DATA?:" + JSON.stringify(ret,null,4));
433 ret.data.forEach(function(ln) {
434 if (!ln.href.match(/^http[s]*:\/\//)) {
435 print("SKIP link: " + ln.href);
438 ln.href= ln.href.replace(/#.*$/,'');
440 if (!_t.checkdomain(ln.href) ) {
441 print("SKIP link (external domain): " + ln.href);
445 // this is just for our purposes..
447 if (ln.href.match(/\/pages\/[0-9]+$/)) {
448 print("SKIP link (ingore unnamed pages): " + ln.href);
454 var fn = encodeURIComponent(ln.href);
456 var dupe = _t.dupeCheck(ln.href);
459 if (dupe == downloaddir + '/' + fn) {
460 var info = JSON.parse(File.read(dupe));
461 if (info && info.fromUrl && info.fromUrl.length > sourcePage.length) {
462 print("SKIP link (in queue already): " + ln.href);
465 print("found a longer link for url")
467 print("SKIP link (in another queue): " + ln.href);
472 File.write(downloaddir + '/' + fn, JSON.stringify( {
475 })); // write an empyt file indicating it needs downloading..
477 var filesList = File.list(downloaddir);
479 maxQueue = filesList.length;
482 if (ret.method == 'downloadpage'){
483 // got the results from download page:
485 // remove from downloadqueue.
490 var mt = ret.contentType.split(';').shift();
492 print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
499 _t.moveToDone(ret.requesturl);
504 var info_f = _t.dupeCheck(ret.requesturl);
506 info = JSON.parse(File.read(info_f));
509 _t.moveToDone(ret.requesturl);
513 var target = _t.toFilename(ret.requesturl);
514 if (info && info.fromURL) {
515 var bn = decodeURIComponent(File.basename(target));
516 target = _t.toFilename(info.fromURL+'/'+ bn);
522 //File.write(target ,decodeURIComponent(escape(base64.decode( ret.data))));
523 print("GOT array sized: " + ret.data.length);
525 File.writeBinaryArray(target,ret.data);
526 print("Wrote to file: " + target);
530 // if it's HTML then add it to parse queue
531 // otehrwise save it.. and run the queue again.
534 //File.write(storedir+"/parsequeue/"+Math.random(), msg);
538 if (ret.method == 'downloadhead'){
539 // got the results from download page:
541 // remove from downloadqueue.
545 var mt = ret.contentType.split(';').shift();
547 print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
553 // add to parse QUEUE..
554 print("moving to parse queue");
555 _t.moveToParse(ret.requesturl);
557 // stuf we do not care about..
558 case 'application/atom+xml':
560 print("moving to done queue");
561 _t.moveToDone(ret.requesturl);
565 print("calling download file");
566 _t.downloadpage( ret.requesturl );
567 // keep it on the queue..
568 // do not run the queue..
573 // if it's HTML then add it to parse queue
574 // otehrwise save it.. and run the queue again.
577 //File.write(storedir+"/parsequeue/"+Math.random(), msg);
585 this.toFilename = function(url)
587 url = url.replace(/^http[s]*:\/\//, '');
588 var p = url.split('/');
589 p.unshift(storedir+'/output');
590 for (var i =1 ;i < p.length; i++) {
591 p[i] = encodeURIComponent(p[i]);
594 p[p.length-1] = decodeURIComponent(p[p.length-1]);
596 var dir = File.dirname(ret);
601 this.checkdomain = function(comp)
603 var b = parseUri(this.uri);
604 var d = parseUri(comp);
605 return (d.host == b.host && d.protocol == b.protocol);
610 this.dupeCheck = function(url)
613 // order - return highest up the queue first..
614 if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
615 return downloaddir +'/' + encodeURIComponent(url);
617 if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
618 return parsedir +'/' + encodeURIComponent(url);
620 if (File.exists(donedir +'/' + encodeURIComponent(url))) {
621 return donedir +'/' + encodeURIComponent(url);
627 this.moveToParse = function(url)
629 var old = this.dupeCheck(url);
630 var target =parsedir +'/' + encodeURIComponent(url);
634 File.write(target, old ? File.read(old) : '');
641 this.moveToDownload= function(url)
643 var old = this.dupeCheck(url);
644 var target =downloaddir +'/' + encodeURIComponent(url);
648 File.write(target, old ? File.read(old) : '');
654 this.moveToDone= function(url)
656 var old = this.dupeCheck(url);
657 var target = donedir +'/' + encodeURIComponent(url);
661 File.write(target, old ? File.read(old) : '');
671 function parseUri (str) {
672 var o = parseUri.options,
673 m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
677 while (i--) uri[o.key[i]] = m[i] || "";
680 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
681 if ($1) uri[o.q.name][$1] = $2;
689 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
692 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
695 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
696 loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/