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, sub ) {
45 //print("TRAVERSE DOM?");
50 var cb = function(s) {
60 //Roo.select('body > div',true).each(function(el) {
61 traverseDOMTree(cb, doc.head, 1);
62 traverseDOMTree(cb, doc.body, 1);
64 File.write(cb.fn.replace(/\.html$/, '')+ (sub ? ('.' + sub ) : '') + '.html' , ret +'</HTML>');
70 var traverseDOMTree = function(cb, currentElement, depth) {
73 //if (currentElement.class_name.match(/roo-dynamic/)) {
76 //print(currentElement.node_name);
78 var nodeName = currentElement.node_name;
79 var tagName = currentElement.tag_name;
81 if (nodeName == '#text') {
82 cb(currentElement.node_value);
91 if (nodeName == 'SCRIPT') {
95 //if (nodeName == 'STYLE') { return; }
97 // Prints the node tagName, such as <A>, <IMG>, etc
98 var outNodeName = nodeName;
99 if (nodeName == 'IFRAME') {
100 //outNodeName = 'DIV';
109 for(i = 0; i < currentElement.attributes.length;i++) {
110 var aname = currentElement.attributes.item(i).name;
111 //if (aname =='class' || aname == 'style') {
114 if (aname =='src' && tagName == 'IFRAME') {
118 attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
120 if (tagName == 'IFRAME') {
121 attr.push('src="' + File.basename(cb.fn).replace(/\.html$/, '')+ '.' + cb.sub + '.html"' );
124 //if (nodeName == 'IFRAME') {
125 // attr.push('src="' + currentElement.attributes.item(i).value + embeded_file '"' );
128 //var style = elClassToStyle(currentElement);
129 //if (style.length) {
130 // attr.push('style="' + style + '"' );
134 //if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
135 // cb("<DIV"+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
137 cb("<"+outNodeName + ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
145 if (nodeName == 'IFRAME') {
148 traversedom(currentElement.content_document, cb.fn, cb.sub )
157 var currentElementChild = currentElement.child_nodes.item(i);
159 while (currentElementChild) {
160 // Formatting code (indent the tree so it looks nice on the screen)
162 if (currentElementChild.node_name == '#text') {
163 // if (currentElementChild.node_value.length) {
164 cb(currentElementChild.node_value);
168 currentElementChild=currentElement.child_nodes.item(i);
173 //for (j = 0; j < depth; j++) {
174 // ¦ is just a vertical line
179 // Recursively traverse the tree structure of the child node
180 traverseDOMTree(cb, currentElementChild, depth+1);
182 currentElementChild=currentElement.child_nodes.item(i);
185 // The remaining code is mostly for formatting the tree
187 //for (j = 0; j < depth - 1; j++) {
192 // if (depth > 1000 && (tagName == 'BODY' || tagName == 'HEAD' )) {
195 cb("</"+outNodeName+">");
203 var update_url = function (web_view, web_frame)
205 var toolbar = tab.get_toolbar();
207 toolbar.set_url(web_frame.get_uri());
208 toolbar.set_can_go_back(web_view.can_go_back());
209 toolbar.set_can_go_forward(web_view.can_go_forward());
219 var create_new_tab = function (web_view, web_frame, new_web_view)
221 new_web_view = new BrowserView();
222 new_web_view.signal.web_view_ready.connect(show_new_tab);
226 var show_new_tab = function (new_web_view)
228 TabbedBrowser.browser.new_tab("", new_web_view);
233 var hover_link = function (web_view, link, url)
235 tab.get_statusbar().set_status(url);
240 this.add_inject = function(force)
243 if (force || (typeof(injected[this.uri]) == 'undefined' )) {
244 injected[this.uri] = 0;
246 if (injected[this.uri] > 2) {
249 injected[this.uri]++;
250 var fn = __script_path__ + "/inject.js";
251 if (File.exists(fn)) {
252 // print("Adding inject");
253 var newjs = File.read(__script_path__ + "/inject.js");
254 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
262 var after_login = false;
265 var load_finished_called = false;
268 var do_print = function(web_frame) {
272 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
273 " var r = document.getElementsByTagName('link');" +
274 "for (var i=0;i < r.length;i++) { " +
275 "var a = r[i]; if (a.getAttribute('media') == 'screen') { a.removeAttribute('media'); } " +
281 if (current_url.match(/\.coconuts\.co\//) ) {
282 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
283 File.read( __script_path__ + "/domains/coconuts.co.js")
287 print("Delay:" + BrowserSettings.delay);
289 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay *1, function() {
293 if (web_frame && BrowserSettings.export_filename_html ) {
294 var html = traversedom(web_frame.get_dom_document(), BrowserSettings.export_filename_html);
295 //File.write(BrowserSettings.export_filename_html, html);
299 if (!BrowserSettings.export_filename) {
300 if (!BrowserSettings.export_filename_html) {
306 var mf = _t.get_main_frame();
308 var ar = Gtk.PaperSize.get_paper_sizes();
309 var psetup = new Gtk.PageSetup();
310 for(var i = 0; i < ar.length; i++) {
311 if (ar[i].get_name() =='iso_a2') {
312 psetup.set_paper_size(ar[i]);
316 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
317 p.set_default_page_setup(psetup);
318 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
319 print("made image - exiting");
327 var weibo_added = false;
329 var is_weibo = false;
332 var load_finished = function (webkit, web_frame, wb)
343 print("load finished");
346 if (after_login === true) {
352 if (after_login === false) {
355 print("adding timeout?");
357 GLib.timeout_add(GLib.PRIORITY_LOW, 2000, function() {
358 print("Redirecting after login?" + after_login);
360 _t.browse(after_login);
367 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
368 File.read( __script_path__ + "/weibo.js")
370 print("run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");");
372 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
373 "run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");"
379 if (after_login !== false) {
382 // wait a bit then load the real page..
383 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
384 print("Redirecting after login?");
385 _t.browse(after_login);
391 if (load_finished_called) {
395 load_finished_called = true;
399 // if(document.location.host=='weibo.com') {
400 // // clear login dialog from weibo.
401 // //check and hidden the login dialog and overlay .....
402 // var els = document.querySelectorAll('div[node-type]');
404 // for (i = 0; i < els.length; i++) {
405 // if (els[i].hasAttribute('node-type')) {
406 // if(els[i].getAttribute('node-type') == 'outer'){
411 // for (i = 0; i < bbc.length; i++) {// hide all the outer.....
412 // bbc[i].style.display = 'none';
416 // if(document.location.pathname == '/login.php'){
418 // // fills in the user name and password
419 // var els = document.querySelectorAll('input[node-type]');
421 // for (i = 0; i < els.length; i++) {
422 // if (els[i].hasAttribute('node-type')) {
423 // if(els[i].getAttribute('node-type') == 'username'){
424 // els[i].value = BrowserSettings.username;
426 // if(els[i].getAttribute('node-type') == 'password'){
427 // els[i].value = BrowserSettings.passwd;
433 // // simulating click event to login ....
434 // var elss = document.querySelectorAll('a[node-type]');
436 // for (i = 0; i < elss.length; i++) {
437 // if (elss[i].hasAttribute('node-type')) {
438 // if(elss[i].getAttribute('node-type') == 'submitBtn'){
450 // clear login dialog from weibo.
451 // TabbedBrowser.browser.current_tab().get_web_view().execute_script(
452 // " if (document.location.host=='weibo.com') { " +
454 // " var a = document.getElementsByClassName('W_layer')[0]; " +
455 // " a.parentNode.removeChild(a.previousSibling); " +
456 // " a.parentNode.removeChild(a); " +
457 // " } catch(e) { } } " );
461 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay, function() {
463 var mf = _t.get_main_frame();
465 var ar = Gtk.PaperSize.get_paper_sizes();
466 var psetup = new Gtk.PageSetup();
467 for(var i = 0; i < ar.length; i++) {
468 if (ar[i].get_name() =='iso_a2') {
469 psetup.set_paper_size(ar[i]);
473 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
474 p.set_default_page_setup(psetup);
475 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
476 print("made image - exiting");
488 var load_committed = function (web_view, web_frame)
490 print("load commited");
492 update_url(web_view, web_frame);
494 // call load finished after 20seconds??
495 // so even if it never complets we try and print the thing.
496 GLib.timeout_add(GLib.PRIORITY_LOW, 40000, function() {
497 print("load commited - 3000 ms?");
503 var clicked_link = function (web_view, web_frame, request,
504 action, decision, window)
506 if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
507 action.get_button() == 2)
509 browser.new_tab(request.get_uri(), null);
519 this.browse = function (url)
521 if(url.search("://") < 0)
522 url = "http://" + url;
526 print("BROWSE: " + url);
527 if (url.match(/\/weibo\.com\//) && after_login === false) {
530 url = "http://weibo.com/login.php";
532 else if (url.match(/\/weibo\.com\//) && after_login !== false) {
535 print("BROWSE -really: " + url);
539 this.set_tab = function (new_tab)
544 this.get_tab = function ()
551 //this.set_scroll_adjustments(null, null);
553 //this.signal.title_changed.connect(update_title);
554 //this.signal.load_committed.connect(load_committed);
555 this.signal.load_finished.connect(load_finished);
558 // For some reason, this segfaults seed in the instance init closure handler
559 // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
560 //this.signal.navigation_policy_decision_requested.connect(clicked_link);
562 //this.signal.hovering_over_link.connect(hover_link);
564 this.signal.create_web_view.connect(create_new_tab);
568 // print("ADDing console message sig handler");
573 this.toFilename = function(url)
575 url = url.replace(/^http[s]*:\/\//, '');
576 var p = url.split('/');
577 p.unshift(storedir+'/output');
578 for (var i =1 ;i < p.length; i++) {
579 p[i] = encodeURIComponent(p[i]);
582 p[p.length-1] = decodeURIComponent(p[p.length-1]);
584 var dir = File.dirname(ret);
589 this.checkdomain = function(comp)
591 var b = parseUri(this.uri);
592 var d = parseUri(comp);
593 return (d.host == b.host && d.protocol == b.protocol);
598 this.dupeCheck = function(url)
601 // order - return highest up the queue first..
602 if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
603 return downloaddir +'/' + encodeURIComponent(url);
605 if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
606 return parsedir +'/' + encodeURIComponent(url);
608 if (File.exists(donedir +'/' + encodeURIComponent(url))) {
609 return donedir +'/' + encodeURIComponent(url);
615 this.moveToParse = function(url)
617 var old = this.dupeCheck(url);
618 var target =parsedir +'/' + encodeURIComponent(url);
622 File.write(target, old ? File.read(old) : '');
629 this.moveToDownload= function(url)
631 var old = this.dupeCheck(url);
632 var target =downloaddir +'/' + encodeURIComponent(url);
636 File.write(target, old ? File.read(old) : '');
642 this.moveToDone= function(url)
644 var old = this.dupeCheck(url);
645 var target = donedir +'/' + encodeURIComponent(url);
649 File.write(target, old ? File.read(old) : '');
659 function parseUri (str) {
660 var o = parseUri.options,
661 m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
665 while (i--) uri[o.key[i]] = m[i] || "";
668 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
669 if ($1) uri[o.q.name][$1] = $2;
677 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
680 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
683 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
684 loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/