2 GLib = imports.gi.GLib;
3 WebKit = imports.gi.WebKit;
5 TabbedBrowser = imports.TabbedBrowser;
6 BrowserSettings = imports.BrowserSettings;
7 BrowserTab = imports.BrowserTab;
9 File = imports.File.File;
11 base64 = imports.base64.base64;
13 BrowserView = new GType({
14 parent: WebKit.WebView.type,
24 var browsePage = false;
30 var update_title = function (web_view, web_frame, title)
33 print("calling update title");
35 title = title.slice(0,25) + "...";
37 tab.get_tab_label().label = title;
51 var traversedom = function(web_frame) {
52 //print("TRAVERSE DOM?");
54 var dom = web_frame.get_dom_document();
57 var cb = function(s) {
62 curdoc = web_frame.get_dom_document();
63 //Roo.select('body > div',true).each(function(el) {
64 traverseDOMTree(cb, dom.head, 1);
65 traverseDOMTree(cb, dom.body, 1);
67 return ret +'</HTML>';
74 var elClassToStyle = function(el)
82 var def = bd.document_element.append_child( bd.create_element(el.node_name));
85 var par_ds = el.node_name == 'BODY' ? false : bd.get_default_view().get_computed_style(el.parent_node, '');
87 var ds = bd.get_default_view().get_computed_style(def, '');
88 var cs = el.owner_document.default_view.get_computed_style(el, '');
94 for (var i = 0; i < cs.length; i++ ) {
97 //print([ k, cs.get_property_value(k) , ds.get_property_value(k) ]);
100 if (!cs.get_property_value(k).length) {
103 if (par_ds && (cs.get_property_value(k) == ds.get_property_value(k) )) {
106 //if (par_ds && (cs.get_property_value(k) == par_ds.get_property_value(k) )) {
110 ns.push(k + ':'+ cs.get_property_value(k) +';');
113 bd.document_element.remove_child(def);
114 //print(ns.join(''));
116 return ns.length ? ns.join('') : '';
118 //el.remove_attribute('class');
119 //el.set_attribute('style', ns.length ? ns.join('') : '');
126 var traverseDOMTree = function(cb, currentElement, depth) {
127 if (currentElement) {
129 //if (currentElement.class_name.match(/roo-dynamic/)) {
132 //print(currentElement.node_name);
134 var nodeName = currentElement.node_name;
135 var tagName = currentElement.tag_name;
137 if (nodeName == '#text') {
138 cb(currentElement.node_value);
142 if(nodeName == 'BR'){
147 if (nodeName == 'SCRIPT') {
151 //if (nodeName == 'STYLE') { return; }
153 // Prints the node tagName, such as <A>, <IMG>, etc
154 var outNodeName = nodeName;
155 if (nodeName == 'IFRAME') {
156 //outNodeName = 'DIV';
165 for(i = 0; i < currentElement.attributes.length;i++) {
166 var aname = currentElement.attributes.item(i).name;
167 //if (aname =='class' || aname == 'style') {
170 if (aname =='src' || tagName == 'IFRAME') {
174 attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
177 //if (nodeName == 'IFRAME') {
178 // attr.push('src="' + currentElement.attributes.item(i).value + embeded_file '"' );
181 //var style = elClassToStyle(currentElement);
182 //if (style.length) {
183 // attr.push('style="' + style + '"' );
187 //if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
188 // cb("<DIV"+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
190 cb("<"+outNodeName + ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
198 if (nodeName == 'IFRAME') {
201 //traverseDOMTree(cb, currentElement.content_document.head, depth+1000);
202 traverseDOMTree(cb, currentElement.content_document.get_document_element(), depth+1000);
212 var currentElementChild = currentElement.child_nodes.item(i);
214 while (currentElementChild) {
215 // Formatting code (indent the tree so it looks nice on the screen)
217 if (currentElementChild.node_name == '#text') {
218 // if (currentElementChild.node_value.length) {
219 cb(currentElementChild.node_value);
223 currentElementChild=currentElement.child_nodes.item(i);
228 //for (j = 0; j < depth; j++) {
229 // ¦ is just a vertical line
234 // Recursively traverse the tree structure of the child node
235 traverseDOMTree(cb, currentElementChild, depth+1);
237 currentElementChild=currentElement.child_nodes.item(i);
240 // The remaining code is mostly for formatting the tree
242 //for (j = 0; j < depth - 1; j++) {
247 // if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
250 cb("</"+outNodeName+">");
258 var update_url = function (web_view, web_frame)
260 var toolbar = tab.get_toolbar();
262 toolbar.set_url(web_frame.get_uri());
263 toolbar.set_can_go_back(web_view.can_go_back());
264 toolbar.set_can_go_forward(web_view.can_go_forward());
274 var create_new_tab = function (web_view, web_frame, new_web_view)
276 new_web_view = new BrowserView();
277 new_web_view.signal.web_view_ready.connect(show_new_tab);
281 var show_new_tab = function (new_web_view)
283 TabbedBrowser.browser.new_tab("", new_web_view);
288 var hover_link = function (web_view, link, url)
290 tab.get_statusbar().set_status(url);
296 this.add_inject = function(force)
299 if (force || (typeof(injected[this.uri]) == 'undefined' )) {
300 injected[this.uri] = 0;
302 if (injected[this.uri] > 2) {
305 injected[this.uri]++;
306 var fn = __script_path__ + "/inject.js";
307 if (File.exists(fn)) {
308 // print("Adding inject");
309 var newjs = File.read(__script_path__ + "/inject.js");
310 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
318 var after_login = false;
321 var load_finished_called = false;
324 var do_print = function(web_frame) {
328 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
329 " var r = document.getElementsByTagName('link');" +
330 "for (var i=0;i < r.length;i++) { " +
331 "var a = r[i]; if (a.getAttribute('media') == 'screen') { a.removeAttribute('media'); } " +
337 if (current_url.match(/\.coconuts\.co\//) ) {
338 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
339 File.read( __script_path__ + "/domains/coconuts.co.js")
343 print("Delay:" + BrowserSettings.delay);
345 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay *1, function() {
347 if (web_frame && BrowserSettings.export_filename_html ) {
348 var html = traversedom(web_frame);
349 File.write(BrowserSettings.export_filename_html, html);
353 if (!BrowserSettings.export_filename) {
356 var mf = _t.get_main_frame();
358 var ar = Gtk.PaperSize.get_paper_sizes();
359 var psetup = new Gtk.PageSetup();
360 for(var i = 0; i < ar.length; i++) {
361 if (ar[i].get_name() =='iso_a2') {
362 psetup.set_paper_size(ar[i]);
366 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
367 p.set_default_page_setup(psetup);
368 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
369 print("made image - exiting");
377 var weibo_added = false;
379 var is_weibo = false;
382 var load_finished = function (webkit, web_frame, wb)
393 print("load finished");
396 if (after_login === true) {
402 if (after_login === false) {
405 print("adding timeout?");
407 GLib.timeout_add(GLib.PRIORITY_LOW, 2000, function() {
408 print("Redirecting after login?" + after_login);
410 _t.browse(after_login);
417 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
418 File.read( __script_path__ + "/weibo.js")
420 print("run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");");
422 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
423 "run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");"
429 if (after_login !== false) {
432 // wait a bit then load the real page..
433 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
434 print("Redirecting after login?");
435 _t.browse(after_login);
441 if (load_finished_called) {
445 load_finished_called = true;
449 // if(document.location.host=='weibo.com') {
450 // // clear login dialog from weibo.
451 // //check and hidden the login dialog and overlay .....
452 // var els = document.querySelectorAll('div[node-type]');
454 // for (i = 0; i < els.length; i++) {
455 // if (els[i].hasAttribute('node-type')) {
456 // if(els[i].getAttribute('node-type') == 'outer'){
461 // for (i = 0; i < bbc.length; i++) {// hide all the outer.....
462 // bbc[i].style.display = 'none';
466 // if(document.location.pathname == '/login.php'){
468 // // fills in the user name and password
469 // var els = document.querySelectorAll('input[node-type]');
471 // for (i = 0; i < els.length; i++) {
472 // if (els[i].hasAttribute('node-type')) {
473 // if(els[i].getAttribute('node-type') == 'username'){
474 // els[i].value = BrowserSettings.username;
476 // if(els[i].getAttribute('node-type') == 'password'){
477 // els[i].value = BrowserSettings.passwd;
483 // // simulating click event to login ....
484 // var elss = document.querySelectorAll('a[node-type]');
486 // for (i = 0; i < elss.length; i++) {
487 // if (elss[i].hasAttribute('node-type')) {
488 // if(elss[i].getAttribute('node-type') == 'submitBtn'){
500 // clear login dialog from weibo.
501 // TabbedBrowser.browser.current_tab().get_web_view().execute_script(
502 // " if (document.location.host=='weibo.com') { " +
504 // " var a = document.getElementsByClassName('W_layer')[0]; " +
505 // " a.parentNode.removeChild(a.previousSibling); " +
506 // " a.parentNode.removeChild(a); " +
507 // " } catch(e) { } } " );
511 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay, function() {
513 var mf = _t.get_main_frame();
515 var ar = Gtk.PaperSize.get_paper_sizes();
516 var psetup = new Gtk.PageSetup();
517 for(var i = 0; i < ar.length; i++) {
518 if (ar[i].get_name() =='iso_a2') {
519 psetup.set_paper_size(ar[i]);
523 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
524 p.set_default_page_setup(psetup);
525 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
526 print("made image - exiting");
538 var load_committed = function (web_view, web_frame)
540 print("load commited");
542 update_url(web_view, web_frame);
544 // call load finished after 20seconds??
545 // so even if it never complets we try and print the thing.
546 GLib.timeout_add(GLib.PRIORITY_LOW, 40000, function() {
547 print("load commited - 3000 ms?");
553 var clicked_link = function (web_view, web_frame, request,
554 action, decision, window)
556 if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
557 action.get_button() == 2)
559 browser.new_tab(request.get_uri(), null);
569 this.browse = function (url)
571 if(url.search("://") < 0)
572 url = "http://" + url;
576 print("BROWSE: " + url);
577 if (url.match(/\/weibo\.com\//) && after_login === false) {
580 url = "http://weibo.com/login.php";
582 else if (url.match(/\/weibo\.com\//) && after_login !== false) {
585 print("BROWSE -really: " + url);
589 this.set_tab = function (new_tab)
594 this.get_tab = function ()
601 //this.set_scroll_adjustments(null, null);
603 //this.signal.title_changed.connect(update_title);
604 //this.signal.load_committed.connect(load_committed);
605 this.signal.load_finished.connect(load_finished);
608 // For some reason, this segfaults seed in the instance init closure handler
609 // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
610 //this.signal.navigation_policy_decision_requested.connect(clicked_link);
612 //this.signal.hovering_over_link.connect(hover_link);
614 this.signal.create_web_view.connect(create_new_tab);
618 // print("ADDing console message sig handler");
623 this.toFilename = function(url)
625 url = url.replace(/^http[s]*:\/\//, '');
626 var p = url.split('/');
627 p.unshift(storedir+'/output');
628 for (var i =1 ;i < p.length; i++) {
629 p[i] = encodeURIComponent(p[i]);
632 p[p.length-1] = decodeURIComponent(p[p.length-1]);
634 var dir = File.dirname(ret);
639 this.checkdomain = function(comp)
641 var b = parseUri(this.uri);
642 var d = parseUri(comp);
643 return (d.host == b.host && d.protocol == b.protocol);
648 this.dupeCheck = function(url)
651 // order - return highest up the queue first..
652 if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
653 return downloaddir +'/' + encodeURIComponent(url);
655 if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
656 return parsedir +'/' + encodeURIComponent(url);
658 if (File.exists(donedir +'/' + encodeURIComponent(url))) {
659 return donedir +'/' + encodeURIComponent(url);
665 this.moveToParse = function(url)
667 var old = this.dupeCheck(url);
668 var target =parsedir +'/' + encodeURIComponent(url);
672 File.write(target, old ? File.read(old) : '');
679 this.moveToDownload= function(url)
681 var old = this.dupeCheck(url);
682 var target =downloaddir +'/' + encodeURIComponent(url);
686 File.write(target, old ? File.read(old) : '');
692 this.moveToDone= function(url)
694 var old = this.dupeCheck(url);
695 var target = donedir +'/' + encodeURIComponent(url);
699 File.write(target, old ? File.read(old) : '');
709 function parseUri (str) {
710 var o = parseUri.options,
711 m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
715 while (i--) uri[o.key[i]] = m[i] || "";
718 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
719 if ($1) uri[o.q.name][$1] = $2;
727 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
730 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
733 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
734 loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/