2 GLib = imports.gi.GLib;
3 WebKit = imports.gi.WebKit;
4 Soup = imports.gi.Soup;
6 TabbedBrowser = imports.TabbedBrowser;
7 BrowserSettings = imports.BrowserSettings;
8 BrowserTab = imports.BrowserTab;
10 File = imports.File.File;
12 base64 = imports.base64.base64;
14 BrowserView = new GType({
15 parent: WebKit.WebView.type,
21 var sess = WebKit.get_default_session();
22 sess.proxy_uri = new Soup.URI.c_new('http://localhost:3128');
26 var browsePage = false;
32 var update_title = function (web_view, web_frame, title)
35 print("calling update title");
37 title = title.slice(0,25) + "...";
39 tab.get_tab_label().label = title;
46 var traversedom = function(doc, fn, sub ) {
47 print("TRAVERSE DOM?");
52 var cb = function(s) {
62 //Roo.select('body > div',true).each(function(el) {
63 traverseDOMTree(cb, doc.head, 1);
64 traverseDOMTree(cb, doc.body, 1);
66 File.write(cb.fn.replace(/\.html$/, '')+ (sub ? ('.' + sub ) : '') + '.html' , ret +'</HTML>');
72 var traverseDOMTree = function(cb, currentElement, depth) {
75 //if (currentElement.class_name.match(/roo-dynamic/)) {
78 //print(currentElement.node_name);
80 var nodeName = currentElement.node_name;
81 var tagName = currentElement.tag_name;
83 if (nodeName == '#text') {
84 cb(currentElement.node_value);
93 if (nodeName == 'SCRIPT') {
97 //if (nodeName == 'STYLE') { return; }
99 // Prints the node tagName, such as <A>, <IMG>, etc
100 var outNodeName = nodeName;
101 if (nodeName == 'IFRAME') {
102 //outNodeName = 'DIV';
111 for(i = 0; i < currentElement.attributes.length;i++) {
112 var aname = currentElement.attributes.item(i).name;
113 //if (aname =='class' || aname == 'style') {
116 if (aname =='src' && tagName == 'IFRAME') {
120 attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
122 if (tagName == 'IFRAME') {
123 attr.push('src="' + File.basename(cb.fn).replace(/\.html$/, '')+ '.' + cb.sub + '.html"' );
126 //if (nodeName == 'IFRAME') {
127 // attr.push('src="' + currentElement.attributes.item(i).value + embeded_file '"' );
130 //var style = elClassToStyle(currentElement);
131 //if (style.length) {
132 // attr.push('style="' + style + '"' );
136 //if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
137 // cb("<DIV"+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
139 cb("<"+outNodeName + ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
147 if (nodeName == 'IFRAME') {
150 traversedom(currentElement.content_document, cb.fn, cb.sub )
159 var currentElementChild = currentElement.child_nodes.item(i);
161 while (currentElementChild) {
162 // Formatting code (indent the tree so it looks nice on the screen)
164 if (currentElementChild.node_name == '#text') {
165 // if (currentElementChild.node_value.length) {
166 cb(currentElementChild.node_value);
170 currentElementChild=currentElement.child_nodes.item(i);
175 //for (j = 0; j < depth; j++) {
176 // ¦ is just a vertical line
181 // Recursively traverse the tree structure of the child node
182 traverseDOMTree(cb, currentElementChild, depth+1);
184 currentElementChild=currentElement.child_nodes.item(i);
187 // The remaining code is mostly for formatting the tree
189 //for (j = 0; j < depth - 1; j++) {
194 // if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
197 cb("</"+outNodeName+">");
205 var update_url = function (web_view, web_frame)
207 var toolbar = tab.get_toolbar();
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());
221 var create_new_tab = function (web_view, web_frame, new_web_view)
223 new_web_view = new BrowserView();
224 new_web_view.signal.web_view_ready.connect(show_new_tab);
228 var show_new_tab = function (new_web_view)
230 TabbedBrowser.browser.new_tab("", new_web_view);
235 var hover_link = function (web_view, link, url)
237 tab.get_statusbar().set_status(url);
242 this.add_inject = function(force)
245 if (force || (typeof(injected[this.uri]) == 'undefined' )) {
246 injected[this.uri] = 0;
248 if (injected[this.uri] > 2) {
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(
264 var after_login = false;
267 var load_finished_called = false;
269 var done_print = false;
270 var do_print = function(web_frame) {
274 print("do_print called -- while already doing this...");
278 print("do_print called");
280 var scr_a = " var r = document.getElementsByTagName('link');" +
281 "for (var i=0;i < r.length;i++) { " +
283 "if ( a.getAttribute('media') == '') { continue; } " +
284 "if ( a.getAttribute('media') == 'screen') { "+
285 "a.removeAttribute('media'); continue;" +
287 //"a.parentNode.removeChild(a);" +
289 var scr_b = "var a = document.body.querySelectorAll('*'); "+
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]); "+
297 "console.log(dnodes.length);" +
298 "dnodes.forEach(function(n) { "+
299 " try { n.parentNode.removeChild(n); } catch(e) { } "+
303 //TabbedBrowser.browser.current_tab().get_web_view().execute_script(
307 //TabbedBrowser.browser.current_tab().get_web_view().execute_script(
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")
317 if (current_url.match(/\.inquirer\.net\//) ) {
318 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
319 File.read( __script_path__ + "/domains/inquirer.net.js")
325 // motorme - requires a scroll to make content appear..
326 if (current_url.match(/\.motorme\.my\//) ) {
327 TabbedBrowser.browser.current_tab().get_web_view().execute_script("window.scrollTo(0,500);");
331 if (current_url.match(/\.efytimes\.com\//) || current_url.match(/\/efytimes\.com\//) ) {
332 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
333 File.read( __script_path__ + "/domains/efytimes.com.js")
338 print("Delay:" + BrowserSettings.delay);
340 GLib.timeout_add(GLib.PRIORITY_DEFAULT, BrowserSettings.delay *1, function() {
344 if (current_url.match(/theindependent\.sg\//) ) {
345 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
346 File.read( __script_path__ + "/domains/theindependent.sg.js")
352 if (web_frame && BrowserSettings.export_filename_html ) {
353 var html = traversedom(web_frame.get_dom_document(), BrowserSettings.export_filename_html);
354 //File.write(BrowserSettings.export_filename_html, html);
358 if (!BrowserSettings.export_filename) {
359 if (!BrowserSettings.export_filename_html) {
360 print("no export filename / and html filename");
366 var mf = _t.get_main_frame();
368 print("Creating paper");
369 var ar = Gtk.PaperSize.get_paper_sizes();
370 var psetup = new Gtk.PageSetup();
371 for(var i = 0; i < ar.length; i++) {
372 if (ar[i].get_name() =='iso_a2') {
373 psetup.set_paper_size(ar[i]);
376 print("doing print operation");
377 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
378 print("doing print operation - set page");
380 p.set_default_page_setup(psetup);
382 print("doing print operation - print_full");
383 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
384 print("made image - exiting");
389 print("error occured");
390 print(JSON.stringify(e));
396 var weibo_added = false;
398 var is_weibo = false;
401 var load_finished = function (webkit, web_frame, wb)
412 print("load finished");
415 if (after_login === true) {
421 if (after_login === false) {
424 print("adding timeout?");
426 GLib.timeout_add(GLib.PRIORITY_LOW, 2000, function() {
427 print("Redirecting after login?" + after_login);
429 _t.browse(after_login);
436 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
437 File.read( __script_path__ + "/weibo.js")
439 print("run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");");
441 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
442 "run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");"
448 if (after_login !== false) {
451 // wait a bit then load the real page..
452 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
453 print("Redirecting after login?");
454 _t.browse(after_login);
460 if (load_finished_called) {
464 load_finished_called = true;
468 // if(document.location.host=='weibo.com') {
469 // // clear login dialog from weibo.
470 // //check and hidden the login dialog and overlay .....
471 // var els = document.querySelectorAll('div[node-type]');
473 // for (i = 0; i < els.length; i++) {
474 // if (els[i].hasAttribute('node-type')) {
475 // if(els[i].getAttribute('node-type') == 'outer'){
480 // for (i = 0; i < bbc.length; i++) {// hide all the outer.....
481 // bbc[i].style.display = 'none';
485 // if(document.location.pathname == '/login.php'){
487 // // fills in the user name and password
488 // var els = document.querySelectorAll('input[node-type]');
490 // for (i = 0; i < els.length; i++) {
491 // if (els[i].hasAttribute('node-type')) {
492 // if(els[i].getAttribute('node-type') == 'username'){
493 // els[i].value = BrowserSettings.username;
495 // if(els[i].getAttribute('node-type') == 'password'){
496 // els[i].value = BrowserSettings.passwd;
502 // // simulating click event to login ....
503 // var elss = document.querySelectorAll('a[node-type]');
505 // for (i = 0; i < elss.length; i++) {
506 // if (elss[i].hasAttribute('node-type')) {
507 // if(elss[i].getAttribute('node-type') == 'submitBtn'){
519 // clear login dialog from weibo.
520 // TabbedBrowser.browser.current_tab().get_web_view().execute_script(
521 // " if (document.location.host=='weibo.com') { " +
523 // " var a = document.getElementsByClassName('W_layer')[0]; " +
524 // " a.parentNode.removeChild(a.previousSibling); " +
525 // " a.parentNode.removeChild(a); " +
526 // " } catch(e) { } } " );
530 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay, function() {
532 var mf = _t.get_main_frame();
534 var ar = Gtk.PaperSize.get_paper_sizes();
535 var psetup = new Gtk.PageSetup();
536 for(var i = 0; i < ar.length; i++) {
537 if (ar[i].get_name() =='iso_a2') {
538 psetup.set_paper_size(ar[i]);
542 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
543 p.set_default_page_setup(psetup);
544 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
545 print("made image - exiting");
557 var load_committed = function (web_view, web_frame)
559 print("load commited");
561 update_url(web_view, web_frame);
563 // call load finished after 20seconds??
564 // so even if it never complets we try and print the thing.
565 GLib.timeout_add(GLib.PRIORITY_LOW, 40000, function() {
566 print("load commited - 3000 ms?");
572 var clicked_link = function (web_view, web_frame, request,
573 action, decision, window)
575 if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
576 action.get_button() == 2)
578 browser.new_tab(request.get_uri(), null);
588 this.browse = function (url)
590 if(url.search("://") < 0)
591 url = "http://" + url;
595 print("BROWSE: " + url);
596 if (url.match(/\/weibo\.com\//) && after_login === false) {
599 url = "http://weibo.com/login.php";
601 else if (url.match(/\/weibo\.com\//) && after_login !== false) {
604 print("BROWSE -really: " + url);
608 this.set_tab = function (new_tab)
613 this.get_tab = function ()
620 //this.set_scroll_adjustments(null, null);
622 //this.signal.title_changed.connect(update_title);
623 //this.signal.load_committed.connect(load_committed);
624 this.signal.load_finished.connect(load_finished);
627 // For some reason, this segfaults seed in the instance init closure handler
628 // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
629 //this.signal.navigation_policy_decision_requested.connect(clicked_link);
631 //this.signal.hovering_over_link.connect(hover_link);
633 this.signal.create_web_view.connect(create_new_tab);
637 // print("ADDing console message sig handler");
642 this.toFilename = function(url)
644 url = url.replace(/^http[s]*:\/\//, '');
645 var p = url.split('/');
646 p.unshift(storedir+'/output');
647 for (var i =1 ;i < p.length; i++) {
648 p[i] = encodeURIComponent(p[i]);
651 p[p.length-1] = decodeURIComponent(p[p.length-1]);
653 var dir = File.dirname(ret);
658 this.checkdomain = function(comp)
660 var b = parseUri(this.uri);
661 var d = parseUri(comp);
662 return (d.host == b.host && d.protocol == b.protocol);
667 this.dupeCheck = function(url)
670 // order - return highest up the queue first..
671 if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
672 return downloaddir +'/' + encodeURIComponent(url);
674 if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
675 return parsedir +'/' + encodeURIComponent(url);
677 if (File.exists(donedir +'/' + encodeURIComponent(url))) {
678 return donedir +'/' + encodeURIComponent(url);
684 this.moveToParse = function(url)
686 var old = this.dupeCheck(url);
687 var target =parsedir +'/' + encodeURIComponent(url);
691 File.write(target, old ? File.read(old) : '');
698 this.moveToDownload= function(url)
700 var old = this.dupeCheck(url);
701 var target =downloaddir +'/' + encodeURIComponent(url);
705 File.write(target, old ? File.read(old) : '');
711 this.moveToDone= function(url)
713 var old = this.dupeCheck(url);
714 var target = donedir +'/' + encodeURIComponent(url);
718 File.write(target, old ? File.read(old) : '');
728 function parseUri (str) {
729 var o = parseUri.options,
730 m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
734 while (i--) uri[o.key[i]] = m[i] || "";
737 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
738 if ($1) uri[o.q.name][$1] = $2;
746 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
749 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
752 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
753 loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/