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;
44 var traversedom = function(doc, fn ) {
45 //print("TRAVERSE DOM?");
49 var cb = function(s) {
59 //Roo.select('body > div',true).each(function(el) {
60 traverseDOMTree(cb, doc.head, 1);
61 traverseDOMTree(cb, doc.body, 1);
63 File.write(cb.fn, ret +'</HTML>');
69 var traverseDOMTree = function(cb, currentElement, depth) {
72 //if (currentElement.class_name.match(/roo-dynamic/)) {
75 //print(currentElement.node_name);
77 var nodeName = currentElement.node_name;
78 var tagName = currentElement.tag_name;
80 if (nodeName == '#text') {
81 cb(currentElement.node_value);
90 if (nodeName == 'SCRIPT') {
94 //if (nodeName == 'STYLE') { return; }
96 // Prints the node tagName, such as <A>, <IMG>, etc
97 var outNodeName = nodeName;
98 if (nodeName == 'IFRAME') {
99 //outNodeName = 'DIV';
108 for(i = 0; i < currentElement.attributes.length;i++) {
109 var aname = currentElement.attributes.item(i).name;
110 //if (aname =='class' || aname == 'style') {
113 if (aname =='src' && tagName == 'IFRAME') {
117 attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
119 if (tagName == 'IFRAME') {
120 attr.push('src="' + File.basename(cb.fn) + '.' + cb.sub + '.html"' );
123 //if (nodeName == 'IFRAME') {
124 // attr.push('src="' + currentElement.attributes.item(i).value + embeded_file '"' );
127 //var style = elClassToStyle(currentElement);
128 //if (style.length) {
129 // attr.push('style="' + style + '"' );
133 //if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
134 // cb("<DIV"+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
136 cb("<"+outNodeName + ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
144 if (nodeName == 'IFRAME') {
147 traversedom(currentElement.content_document, cb.fn, cb.sub )
156 var currentElementChild = currentElement.child_nodes.item(i);
158 while (currentElementChild) {
159 // Formatting code (indent the tree so it looks nice on the screen)
161 if (currentElementChild.node_name == '#text') {
162 // if (currentElementChild.node_value.length) {
163 cb(currentElementChild.node_value);
167 currentElementChild=currentElement.child_nodes.item(i);
172 //for (j = 0; j < depth; j++) {
173 // ¦ is just a vertical line
178 // Recursively traverse the tree structure of the child node
179 traverseDOMTree(cb, currentElementChild, depth+1);
181 currentElementChild=currentElement.child_nodes.item(i);
184 // The remaining code is mostly for formatting the tree
186 //for (j = 0; j < depth - 1; j++) {
191 // if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
194 cb("</"+outNodeName+">");
202 var update_url = function (web_view, web_frame)
204 var toolbar = tab.get_toolbar();
206 toolbar.set_url(web_frame.get_uri());
207 toolbar.set_can_go_back(web_view.can_go_back());
208 toolbar.set_can_go_forward(web_view.can_go_forward());
218 var create_new_tab = function (web_view, web_frame, new_web_view)
220 new_web_view = new BrowserView();
221 new_web_view.signal.web_view_ready.connect(show_new_tab);
225 var show_new_tab = function (new_web_view)
227 TabbedBrowser.browser.new_tab("", new_web_view);
232 var hover_link = function (web_view, link, url)
234 tab.get_statusbar().set_status(url);
239 this.add_inject = function(force)
242 if (force || (typeof(injected[this.uri]) == 'undefined' )) {
243 injected[this.uri] = 0;
245 if (injected[this.uri] > 2) {
248 injected[this.uri]++;
249 var fn = __script_path__ + "/inject.js";
250 if (File.exists(fn)) {
251 // print("Adding inject");
252 var newjs = File.read(__script_path__ + "/inject.js");
253 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
261 var after_login = false;
264 var load_finished_called = false;
267 var do_print = function(web_frame) {
271 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
272 " var r = document.getElementsByTagName('link');" +
273 "for (var i=0;i < r.length;i++) { " +
274 "var a = r[i]; if (a.getAttribute('media') == 'screen') { a.removeAttribute('media'); } " +
280 if (current_url.match(/\.coconuts\.co\//) ) {
281 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
282 File.read( __script_path__ + "/domains/coconuts.co.js")
286 print("Delay:" + BrowserSettings.delay);
288 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay *1, function() {
290 if (web_frame && BrowserSettings.export_filename_html ) {
291 var html = traversedom(web_frame.get_dom_document(), BrowserSettings.export_filename_html);
292 //File.write(BrowserSettings.export_filename_html, html);
296 if (!BrowserSettings.export_filename) {
297 if (!BrowserSettings.export_filename_html) {
303 var mf = _t.get_main_frame();
305 var ar = Gtk.PaperSize.get_paper_sizes();
306 var psetup = new Gtk.PageSetup();
307 for(var i = 0; i < ar.length; i++) {
308 if (ar[i].get_name() =='iso_a2') {
309 psetup.set_paper_size(ar[i]);
313 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
314 p.set_default_page_setup(psetup);
315 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
316 print("made image - exiting");
324 var weibo_added = false;
326 var is_weibo = false;
329 var load_finished = function (webkit, web_frame, wb)
340 print("load finished");
343 if (after_login === true) {
349 if (after_login === false) {
352 print("adding timeout?");
354 GLib.timeout_add(GLib.PRIORITY_LOW, 2000, function() {
355 print("Redirecting after login?" + after_login);
357 _t.browse(after_login);
364 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
365 File.read( __script_path__ + "/weibo.js")
367 print("run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");");
369 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
370 "run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");"
376 if (after_login !== false) {
379 // wait a bit then load the real page..
380 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
381 print("Redirecting after login?");
382 _t.browse(after_login);
388 if (load_finished_called) {
392 load_finished_called = true;
396 // if(document.location.host=='weibo.com') {
397 // // clear login dialog from weibo.
398 // //check and hidden the login dialog and overlay .....
399 // var els = document.querySelectorAll('div[node-type]');
401 // for (i = 0; i < els.length; i++) {
402 // if (els[i].hasAttribute('node-type')) {
403 // if(els[i].getAttribute('node-type') == 'outer'){
408 // for (i = 0; i < bbc.length; i++) {// hide all the outer.....
409 // bbc[i].style.display = 'none';
413 // if(document.location.pathname == '/login.php'){
415 // // fills in the user name and password
416 // var els = document.querySelectorAll('input[node-type]');
418 // for (i = 0; i < els.length; i++) {
419 // if (els[i].hasAttribute('node-type')) {
420 // if(els[i].getAttribute('node-type') == 'username'){
421 // els[i].value = BrowserSettings.username;
423 // if(els[i].getAttribute('node-type') == 'password'){
424 // els[i].value = BrowserSettings.passwd;
430 // // simulating click event to login ....
431 // var elss = document.querySelectorAll('a[node-type]');
433 // for (i = 0; i < elss.length; i++) {
434 // if (elss[i].hasAttribute('node-type')) {
435 // if(elss[i].getAttribute('node-type') == 'submitBtn'){
447 // clear login dialog from weibo.
448 // TabbedBrowser.browser.current_tab().get_web_view().execute_script(
449 // " if (document.location.host=='weibo.com') { " +
451 // " var a = document.getElementsByClassName('W_layer')[0]; " +
452 // " a.parentNode.removeChild(a.previousSibling); " +
453 // " a.parentNode.removeChild(a); " +
454 // " } catch(e) { } } " );
458 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay, function() {
460 var mf = _t.get_main_frame();
462 var ar = Gtk.PaperSize.get_paper_sizes();
463 var psetup = new Gtk.PageSetup();
464 for(var i = 0; i < ar.length; i++) {
465 if (ar[i].get_name() =='iso_a2') {
466 psetup.set_paper_size(ar[i]);
470 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
471 p.set_default_page_setup(psetup);
472 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
473 print("made image - exiting");
485 var load_committed = function (web_view, web_frame)
487 print("load commited");
489 update_url(web_view, web_frame);
491 // call load finished after 20seconds??
492 // so even if it never complets we try and print the thing.
493 GLib.timeout_add(GLib.PRIORITY_LOW, 40000, function() {
494 print("load commited - 3000 ms?");
500 var clicked_link = function (web_view, web_frame, request,
501 action, decision, window)
503 if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
504 action.get_button() == 2)
506 browser.new_tab(request.get_uri(), null);
516 this.browse = function (url)
518 if(url.search("://") < 0)
519 url = "http://" + url;
523 print("BROWSE: " + url);
524 if (url.match(/\/weibo\.com\//) && after_login === false) {
527 url = "http://weibo.com/login.php";
529 else if (url.match(/\/weibo\.com\//) && after_login !== false) {
532 print("BROWSE -really: " + url);
536 this.set_tab = function (new_tab)
541 this.get_tab = function ()
548 //this.set_scroll_adjustments(null, null);
550 //this.signal.title_changed.connect(update_title);
551 //this.signal.load_committed.connect(load_committed);
552 this.signal.load_finished.connect(load_finished);
555 // For some reason, this segfaults seed in the instance init closure handler
556 // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
557 //this.signal.navigation_policy_decision_requested.connect(clicked_link);
559 //this.signal.hovering_over_link.connect(hover_link);
561 this.signal.create_web_view.connect(create_new_tab);
565 // print("ADDing console message sig handler");
570 this.toFilename = function(url)
572 url = url.replace(/^http[s]*:\/\//, '');
573 var p = url.split('/');
574 p.unshift(storedir+'/output');
575 for (var i =1 ;i < p.length; i++) {
576 p[i] = encodeURIComponent(p[i]);
579 p[p.length-1] = decodeURIComponent(p[p.length-1]);
581 var dir = File.dirname(ret);
586 this.checkdomain = function(comp)
588 var b = parseUri(this.uri);
589 var d = parseUri(comp);
590 return (d.host == b.host && d.protocol == b.protocol);
595 this.dupeCheck = function(url)
598 // order - return highest up the queue first..
599 if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
600 return downloaddir +'/' + encodeURIComponent(url);
602 if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
603 return parsedir +'/' + encodeURIComponent(url);
605 if (File.exists(donedir +'/' + encodeURIComponent(url))) {
606 return donedir +'/' + encodeURIComponent(url);
612 this.moveToParse = function(url)
614 var old = this.dupeCheck(url);
615 var target =parsedir +'/' + encodeURIComponent(url);
619 File.write(target, old ? File.read(old) : '');
626 this.moveToDownload= function(url)
628 var old = this.dupeCheck(url);
629 var target =downloaddir +'/' + encodeURIComponent(url);
633 File.write(target, old ? File.read(old) : '');
639 this.moveToDone= function(url)
641 var old = this.dupeCheck(url);
642 var target = donedir +'/' + encodeURIComponent(url);
646 File.write(target, old ? File.read(old) : '');
656 function parseUri (str) {
657 var o = parseUri.options,
658 m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
662 while (i--) uri[o.key[i]] = m[i] || "";
665 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
666 if ($1) uri[o.q.name][$1] = $2;
674 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
677 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
680 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
681 loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/