2 WebKit = imports.gi.WebKit;
4 TabbedBrowser = imports.TabbedBrowser;
5 File = imports.File.File;
7 base64 = imports.base64.base64;
9 BrowserView = new GType({
10 parent: WebKit.WebView.type,
20 var browsePage = false;
24 var storedir = '/home/alan/wkqueue';
25 if (!File.exists(storedir)) {
28 var parsedir = storedir + '/parse_queue';
29 var downloaddir = storedir + '/download_queue';
30 var donedir = storedir + '/downloaded_queue';
31 if (!File.exists(parsedir)) {
34 if (!File.exists(downloaddir)) {
35 File.mkdir(downloaddir);
37 if (!File.exists(donedir)) {
41 var update_title = function (web_view, web_frame, title)
44 title = title.slice(0,25) + "...";
46 tab.get_tab_label().label = title;
49 var update_url = function (web_view, web_frame)
51 var toolbar = tab.get_toolbar();
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());
58 var update_progress = function (bar, progress)
60 tab.get_toolbar().set_progress(progress / 100);
63 var create_new_tab = function (web_view, web_frame, new_web_view)
65 new_web_view = new BrowserView();
66 new_web_view.signal.web_view_ready.connect(show_new_tab);
70 var show_new_tab = function (new_web_view)
72 TabbedBrowser.browser.new_tab("", new_web_view);
77 var hover_link = function (web_view, link, url)
79 tab.get_statusbar().set_status(url);
85 this.add_inject = function(force)
88 if (force || (typeof(injected[this.uri]) == 'undefined' )) {
89 injected[this.uri] = 0;
91 if (injected[this.uri] > 2) {
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(
106 this.add_nsinject = function(force)
109 //if (force || (typeof(injected[this.uri]) == 'undefined' )) {
110 // injected[this.uri] = 0;
112 //if (injected[this.uri] > 2) {
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(
132 var load_finished = function ()
134 print("load finished");
135 tab.get_toolbar().set_progress(0);
142 _t.add_nsinject(true);
143 print(typeof(Seed.argv[2]));
144 if (Seed.argv[2] == null) {
149 _t.nsdownloadNext(Seed.argv[2] * 1)
153 print("onload: calling gather links");
154 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
155 'BrowserMirror.gatherlinks();'
164 var load_committed = function (web_view, web_frame)
166 update_url(web_view, web_frame);
169 var clicked_link = function (web_view, web_frame, request,
170 action, decision, window)
172 if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
173 action.get_button() == 2)
175 browser.new_tab(request.get_uri(), null);
185 this.browse = function (url)
187 if(url.search("://") < 0)
188 url = "http://" + url;
193 this.set_tab = function (new_tab)
198 this.get_tab = function ()
204 this.downloadqueue = function()
206 var filesList = File.list(downloaddir);
207 print("DOWNLOAD QUEUE LENGTH: " + filesList.length);
208 if (!maxQueue || maxQueue < filesList.length) {
209 maxQueue = filesList.length;
212 tab.get_toolbar().set_progress(filesList.length / Math.max(maxQueue, filesList.length ));
213 if (filesList == null || filesList.length == 0 ){
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();
224 this.downloadhead( url);
230 this.queuerun = function()
232 if (this.downloadqueue()) return true;
233 return this.parsequeue();
236 this.parsequeue = function(){
237 var filesList = File.list(parsedir);
238 if (filesList == null || filesList.length == 0 ){
242 browsePage = decodeURIComponent(filesList[0]);
243 print("parsing page:" + browsePage );
244 this.browse(browsePage);
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);
259 "BrowserMirror.downloadpage(" + JSON.stringify(link) +");"
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);
270 "BrowserMirror.downloadhead(" + JSON.stringify(link) +");"
275 this.nsdownloadNext = function()
277 /*if (nsqueue === false) {
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
287 if (!nsqueue.length) {
291 var pg = nsqueue.shift() ;
296 //print(typeof(Seed.argv[2]));
302 var pg = Seed.argv[2] * 1;
303 if ( File.exists( storedir+'/output/' + pg + '.csv')) {
304 print("DONE : " + storedir+'/output/' + pg + '.csv');
309 var pd = JSON.parse(File.read('/home/alan/.nspasswd'));
311 print("downloadnext : " + pg);
313 'NS.login(' + JSON.stringify(pd.username) + ',' + JSON.stringify(pd.password) + ',' + pg + ' );'
322 //this.set_scroll_adjustments(null, null);
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);
329 this.signal.create_plugin_widget.connect(function(wv, mt, uri, parm) {
330 print("got plugin requrest");
333 var mv = this.signal.webkit_web_view_get_main_frame();
334 //var db = WebKit.get_web_plugin_database ();
335 //var pg = db.get_plugins();
337 //for(var i = 0 ; i < pg.length; i++) {
339 //print(pg[i].get_name());
340 //print(pg[i].get_enabled() *1);
341 //print(pg[i].get_description() *1);
344 // For some reason, this segfaults seed in the instance init closure handler
345 // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
346 //this.signal.navigation_policy_decision_requested.connect(clicked_link);
348 this.signal.hovering_over_link.connect(hover_link);
350 this.signal.create_web_view.connect(create_new_tab);
353 print("ADDing console message sig handler");
358 this.signal.console_message.connect(function(wv, msg, line, sid) {
359 // print('BrowserView.js got ' + msg);
363 ret = JSON.parse(msg);
366 print("GOT INVALID message:" + msg)
371 print("got method : " + ret.method);
373 if (ret.method == 'exit') {
378 if (ret.method == 'nsdownloadpage'){
380 var mt = ret.contentType.split(';').shift();
382 print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
389 print("CONSOLE GOT BLANK - call download next?");
391 //_t.nsdownloadNext();
392 // _t.moveToDone(ret.requesturl);
397 var info_f = _t.dupeCheck(ret.requesturl);
399 info = JSON.parse(File.read(info_f));
401 var target = storedir+'/output/' + ret.filename
408 //File.write(target ,decodeURIComponent(escape(base64.decode( ret.data))));
409 print("GOT array sized: " + ret.data.length);
411 File.writeBinaryArray(target,ret.data);
412 print("Wrote to file: " + target);
416 //_t.nsdownloadNext();
425 if (ret.method == 'gatherlinks'){
426 // flag the page as parsed.
429 _t.moveToDone(browsePage);
432 var sourcePage = browsePage;
434 print(typeof(ret.data));
435 if (typeof(ret.data) != 'object' ) {
436 print("GOT INVALID DATA?:" + JSON.stringify(ret,null,4));
440 ret.data.forEach(function(ln) {
441 if (!ln.href.match(/^http[s]*:\/\//)) {
442 print("SKIP link: " + ln.href);
445 ln.href= ln.href.replace(/#.*$/,'');
447 if (!_t.checkdomain(ln.href) ) {
448 print("SKIP link (external domain): " + ln.href);
452 // this is just for our purposes..
454 if (ln.href.match(/\/pages\/[0-9]+$/)) {
455 print("SKIP link (ingore unnamed pages): " + ln.href);
461 var fn = encodeURIComponent(ln.href);
463 var dupe = _t.dupeCheck(ln.href);
466 if (dupe == downloaddir + '/' + fn) {
467 var info = JSON.parse(File.read(dupe));
468 if (info && info.fromUrl && info.fromUrl.length > sourcePage.length) {
469 print("SKIP link (in queue already): " + ln.href);
472 print("found a longer link for url")
474 print("SKIP link (in another queue): " + ln.href);
479 File.write(downloaddir + '/' + fn, JSON.stringify( {
482 })); // write an empyt file indicating it needs downloading..
484 var filesList = File.list(downloaddir);
486 maxQueue = filesList.length;
489 if (ret.method == 'downloadpage'){
490 // got the results from download page:
492 // remove from downloadqueue.
497 var mt = ret.contentType.split(';').shift();
499 print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
506 _t.moveToDone(ret.requesturl);
511 var info_f = _t.dupeCheck(ret.requesturl);
513 info = JSON.parse(File.read(info_f));
516 _t.moveToDone(ret.requesturl);
520 var target = _t.toFilename(ret.requesturl);
521 if (info && info.fromURL) {
522 var bn = decodeURIComponent(File.basename(target));
523 target = _t.toFilename(info.fromURL+'/'+ bn);
529 //File.write(target ,decodeURIComponent(escape(base64.decode( ret.data))));
530 print("GOT array sized: " + ret.data.length);
532 File.writeBinaryArray(target,ret.data);
533 print("Wrote to file: " + target);
537 // if it's HTML then add it to parse queue
538 // otehrwise save it.. and run the queue again.
541 //File.write(storedir+"/parsequeue/"+Math.random(), msg);
545 if (ret.method == 'downloadhead'){
546 // got the results from download page:
548 // remove from downloadqueue.
552 var mt = ret.contentType.split(';').shift();
554 print("INVALID CONTENT TYPE? \n" + JSON.stringify(ret));
560 // add to parse QUEUE..
561 print("moving to parse queue");
562 _t.moveToParse(ret.requesturl);
564 // stuf we do not care about..
565 case 'application/atom+xml':
567 print("moving to done queue");
568 _t.moveToDone(ret.requesturl);
572 print("calling download file");
573 _t.downloadpage( ret.requesturl );
574 // keep it on the queue..
575 // do not run the queue..
580 // if it's HTML then add it to parse queue
581 // otehrwise save it.. and run the queue again.
584 //File.write(storedir+"/parsequeue/"+Math.random(), msg);
592 this.toFilename = function(url)
594 url = url.replace(/^http[s]*:\/\//, '');
595 var p = url.split('/');
596 p.unshift(storedir+'/output');
597 for (var i =1 ;i < p.length; i++) {
598 p[i] = encodeURIComponent(p[i]);
601 p[p.length-1] = decodeURIComponent(p[p.length-1]);
603 var dir = File.dirname(ret);
608 this.checkdomain = function(comp)
610 var b = parseUri(this.uri);
611 var d = parseUri(comp);
612 return (d.host == b.host && d.protocol == b.protocol);
617 this.dupeCheck = function(url)
620 // order - return highest up the queue first..
621 if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
622 return downloaddir +'/' + encodeURIComponent(url);
624 if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
625 return parsedir +'/' + encodeURIComponent(url);
627 if (File.exists(donedir +'/' + encodeURIComponent(url))) {
628 return donedir +'/' + encodeURIComponent(url);
634 this.moveToParse = function(url)
636 var old = this.dupeCheck(url);
637 var target =parsedir +'/' + encodeURIComponent(url);
641 File.write(target, old ? File.read(old) : '');
648 this.moveToDownload= function(url)
650 var old = this.dupeCheck(url);
651 var target =downloaddir +'/' + encodeURIComponent(url);
655 File.write(target, old ? File.read(old) : '');
661 this.moveToDone= function(url)
663 var old = this.dupeCheck(url);
664 var target = donedir +'/' + encodeURIComponent(url);
668 File.write(target, old ? File.read(old) : '');
678 function parseUri (str) {
679 var o = parseUri.options,
680 m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
684 while (i--) uri[o.key[i]] = m[i] || "";
687 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
688 if ($1) uri[o.q.name][$1] = $2;
696 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
699 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
702 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
703 loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/