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++) { " +
276 "if ( a.getAttribute('media') == '') { continue; } " +
277 "if ( a.getAttribute('media') == 'screen') { "+
278 "a.removeAttribute('media'); continue;" +
285 if (current_url.match(/\.coconuts\.co\//) ) {
286 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
287 File.read( __script_path__ + "/domains/coconuts.co.js")
291 print("Delay:" + BrowserSettings.delay);
293 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay *1, function() {
297 if (web_frame && BrowserSettings.export_filename_html ) {
298 var html = traversedom(web_frame.get_dom_document(), BrowserSettings.export_filename_html);
299 //File.write(BrowserSettings.export_filename_html, html);
303 if (!BrowserSettings.export_filename) {
304 if (!BrowserSettings.export_filename_html) {
310 var mf = _t.get_main_frame();
312 var ar = Gtk.PaperSize.get_paper_sizes();
313 var psetup = new Gtk.PageSetup();
314 for(var i = 0; i < ar.length; i++) {
315 if (ar[i].get_name() =='iso_a2') {
316 psetup.set_paper_size(ar[i]);
320 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
321 p.set_default_page_setup(psetup);
322 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
323 print("made image - exiting");
331 var weibo_added = false;
333 var is_weibo = false;
336 var load_finished = function (webkit, web_frame, wb)
347 print("load finished");
350 if (after_login === true) {
356 if (after_login === false) {
359 print("adding timeout?");
361 GLib.timeout_add(GLib.PRIORITY_LOW, 2000, function() {
362 print("Redirecting after login?" + after_login);
364 _t.browse(after_login);
371 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
372 File.read( __script_path__ + "/weibo.js")
374 print("run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");");
376 TabbedBrowser.browser.current_tab().get_web_view().execute_script(
377 "run_weibo(" + JSON.stringify( BrowserSettings.username ) + ", " + JSON.stringify(BrowserSettings.passwd) + ");"
383 if (after_login !== false) {
386 // wait a bit then load the real page..
387 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
388 print("Redirecting after login?");
389 _t.browse(after_login);
395 if (load_finished_called) {
399 load_finished_called = true;
403 // if(document.location.host=='weibo.com') {
404 // // clear login dialog from weibo.
405 // //check and hidden the login dialog and overlay .....
406 // var els = document.querySelectorAll('div[node-type]');
408 // for (i = 0; i < els.length; i++) {
409 // if (els[i].hasAttribute('node-type')) {
410 // if(els[i].getAttribute('node-type') == 'outer'){
415 // for (i = 0; i < bbc.length; i++) {// hide all the outer.....
416 // bbc[i].style.display = 'none';
420 // if(document.location.pathname == '/login.php'){
422 // // fills in the user name and password
423 // var els = document.querySelectorAll('input[node-type]');
425 // for (i = 0; i < els.length; i++) {
426 // if (els[i].hasAttribute('node-type')) {
427 // if(els[i].getAttribute('node-type') == 'username'){
428 // els[i].value = BrowserSettings.username;
430 // if(els[i].getAttribute('node-type') == 'password'){
431 // els[i].value = BrowserSettings.passwd;
437 // // simulating click event to login ....
438 // var elss = document.querySelectorAll('a[node-type]');
440 // for (i = 0; i < elss.length; i++) {
441 // if (elss[i].hasAttribute('node-type')) {
442 // if(elss[i].getAttribute('node-type') == 'submitBtn'){
454 // clear login dialog from weibo.
455 // TabbedBrowser.browser.current_tab().get_web_view().execute_script(
456 // " if (document.location.host=='weibo.com') { " +
458 // " var a = document.getElementsByClassName('W_layer')[0]; " +
459 // " a.parentNode.removeChild(a.previousSibling); " +
460 // " a.parentNode.removeChild(a); " +
461 // " } catch(e) { } } " );
465 GLib.timeout_add(GLib.PRIORITY_LOW, BrowserSettings.delay, function() {
467 var mf = _t.get_main_frame();
469 var ar = Gtk.PaperSize.get_paper_sizes();
470 var psetup = new Gtk.PageSetup();
471 for(var i = 0; i < ar.length; i++) {
472 if (ar[i].get_name() =='iso_a2') {
473 psetup.set_paper_size(ar[i]);
477 var p = new Gtk.PrintOperation({ export_filename : BrowserSettings.export_filename });
478 p.set_default_page_setup(psetup);
479 mf.print_full(p, Gtk.PrintOperationAction.EXPORT);
480 print("made image - exiting");
492 var load_committed = function (web_view, web_frame)
494 print("load commited");
496 update_url(web_view, web_frame);
498 // call load finished after 20seconds??
499 // so even if it never complets we try and print the thing.
500 GLib.timeout_add(GLib.PRIORITY_LOW, 40000, function() {
501 print("load commited - 3000 ms?");
507 var clicked_link = function (web_view, web_frame, request,
508 action, decision, window)
510 if(action.get_reason() == WebKit.WebNavigationReason.LINK_CLICKED &&
511 action.get_button() == 2)
513 browser.new_tab(request.get_uri(), null);
523 this.browse = function (url)
525 if(url.search("://") < 0)
526 url = "http://" + url;
530 print("BROWSE: " + url);
531 if (url.match(/\/weibo\.com\//) && after_login === false) {
534 url = "http://weibo.com/login.php";
536 else if (url.match(/\/weibo\.com\//) && after_login !== false) {
539 print("BROWSE -really: " + url);
543 this.set_tab = function (new_tab)
548 this.get_tab = function ()
555 //this.set_scroll_adjustments(null, null);
557 //this.signal.title_changed.connect(update_title);
558 //this.signal.load_committed.connect(load_committed);
559 this.signal.load_finished.connect(load_finished);
562 // For some reason, this segfaults seed in the instance init closure handler
563 // Once that's fixed, uncommenting the next line will give middle-click-open-in-new tab
564 //this.signal.navigation_policy_decision_requested.connect(clicked_link);
566 //this.signal.hovering_over_link.connect(hover_link);
568 this.signal.create_web_view.connect(create_new_tab);
572 // print("ADDing console message sig handler");
577 this.toFilename = function(url)
579 url = url.replace(/^http[s]*:\/\//, '');
580 var p = url.split('/');
581 p.unshift(storedir+'/output');
582 for (var i =1 ;i < p.length; i++) {
583 p[i] = encodeURIComponent(p[i]);
586 p[p.length-1] = decodeURIComponent(p[p.length-1]);
588 var dir = File.dirname(ret);
593 this.checkdomain = function(comp)
595 var b = parseUri(this.uri);
596 var d = parseUri(comp);
597 return (d.host == b.host && d.protocol == b.protocol);
602 this.dupeCheck = function(url)
605 // order - return highest up the queue first..
606 if (File.exists(downloaddir +'/' + encodeURIComponent(url))) {
607 return downloaddir +'/' + encodeURIComponent(url);
609 if (File.exists(parsedir +'/' + encodeURIComponent(url))) {
610 return parsedir +'/' + encodeURIComponent(url);
612 if (File.exists(donedir +'/' + encodeURIComponent(url))) {
613 return donedir +'/' + encodeURIComponent(url);
619 this.moveToParse = function(url)
621 var old = this.dupeCheck(url);
622 var target =parsedir +'/' + encodeURIComponent(url);
626 File.write(target, old ? File.read(old) : '');
633 this.moveToDownload= function(url)
635 var old = this.dupeCheck(url);
636 var target =downloaddir +'/' + encodeURIComponent(url);
640 File.write(target, old ? File.read(old) : '');
646 this.moveToDone= function(url)
648 var old = this.dupeCheck(url);
649 var target = donedir +'/' + encodeURIComponent(url);
653 File.write(target, old ? File.read(old) : '');
663 function parseUri (str) {
664 var o = parseUri.options,
665 m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
669 while (i--) uri[o.key[i]] = m[i] || "";
672 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
673 if ($1) uri[o.q.name][$1] = $2;
681 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
684 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
687 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
688 loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/