home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 February / WN_129_CD.iso / Windows / Extensions Firefox / FlashGot / flashgot-0.5.3.xpi / chrome / flashgot.jar / content / flashgot / flashgotOverlay.js < prev    next >
Encoding:
JavaScript  |  2004-11-14  |  11.1 KB  |  344 lines

  1. /***** BEGIN LICENSE BLOCK *****
  2.    - Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.    -
  4.    - The contents of this file are subject to the Mozilla Public License Version
  5.    - 1.1 (the "License"); you may not use this file except in compliance with
  6.    - the License. You may obtain a copy of the License at
  7.    - http://www.mozilla.org/MPL/
  8.    -
  9.    - Software distributed under the License is distributed on an "AS IS" basis,
  10.    - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.    - for the specific language governing rights and limitations under the
  12.    - License.
  13.    -
  14.    - The Original Code is "FlashGot".
  15.    -
  16.    - The Initial Developer of the Original Code is Giorgio Maone.
  17.    - Portions created by the Initial Developer are Copyright (C) 2004
  18.    - the Initial Developer. All Rights Reserved.
  19.    -
  20.    - Contributor(s): Giorgio Maone <g.maone @ informaction.com>
  21.    -
  22.    - Alternatively, the contents of this file may be used under the terms of
  23.    - either the GNU General Public License Version 2 or later (the "GPL"), or
  24.    - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  25.    - in which case the provisions of the GPL or the LGPL are applicable instead
  26.    - of those above. If you wish to allow use of your version of this file only
  27.    - under the terms of either the GPL or the LGPL, and not to allow others to
  28.    - use your version of this file under the terms of the MPL, indicate your
  29.    - decision by deleting the provisions above and replace them with the notice
  30.    - and other provisions required by the LGPL or the GPL. If you do not delete
  31.    - the provisions above, a recipient may use your version of this file under
  32.    - the terms of any one of the MPL, the GPL or the LGPL.
  33.    -
  34.    - ***** END LICENSE BLOCK *****/
  35.  
  36.  
  37.  
  38. function FlashGot() {
  39.   
  40.   try {
  41.    
  42.     this.log("Per-window init started");
  43.     // install listeners
  44.     const contextMenu=document.getElementById("contentAreaContextMenu");
  45.     if(contextMenu) {
  46.       contextMenu.addEventListener("popupshowing", function(ev) {
  47.         flashGot.menuShowing(ev)
  48.       }, false);
  49.     }
  50.     
  51.     window.addEventListener("mouseover", function(ev) {
  52.       flashGot.hoverElement=ev.target;
  53.     }, false);
  54.     
  55.   } catch(err) {
  56.     this.log("Unrecoverable init error: "+err.message);
  57.   }
  58.   
  59.   flashGot=this;
  60.   
  61.   this.log("Per-window init done");
  62. }
  63.  
  64. FlashGot.prototype = {
  65.   lastHoverElement: null
  66. ,
  67.   log: function(msg) {
  68.     g_flashGotService.log(msg);
  69.   }
  70. ,
  71.   switchOption: function(opt) {
  72.     g_flashGotService.setPref(opt,!g_flashGotService.getPref(opt));
  73.   }
  74. ,
  75.   prepareMenus: function() {
  76.     function menuSwitch(name, disabled) {
  77.       var menuItem=document.getElementById("flashgot-menuitem-"+name);
  78.       if(menuItem && ! ( menuItem.hidden = hidden ) ) {
  79.        menuItem.setAttribute("disabled",disabled?"true":"false");
  80.        menuItem.hidden=disabled && hideDisabled;
  81.       }
  82.     }
  83.     const  defaultDM=g_flashGotService.defaultDM;  
  84.     
  85.     const invalidLink=!this.popupLink;
  86.    
  87.     const invalidSel= this.srcWindow.getSelection().isCollapsed;
  88.     // [check me, was:] 
  89.     // const selText=this.srcWindow.getSelection().toString();
  90.     // const invalidSel= ! (selText.length>0 && /[^\s]+/.test(selText));
  91.     
  92.     const noLink=this.linksCount==0;
  93.     const hideDisabled=g_flashGotService.getPref("hideDisabledCmds");  
  94.     
  95.     var hidden=!g_flashGotService.DMS.found;
  96.     menuSwitch("it",  invalidLink || defaultDM.disabledLink);
  97.     menuSwitch("sel", invalidSel || defaultDM.disabledSel);
  98.     menuSwitch("all", noLink || defaultDM.disabledAll);
  99.     
  100.     hidden=false;
  101.     menuSwitch("buildGallery",invalidLink && invalidSel && noLink);
  102.     
  103.     const optsMenu=document.getElementById("flashgot-menu-options");
  104.     const opts=optsMenu.getElementsByTagName("menuitem");
  105.     
  106.     var menuItem,match;
  107.     var j=opts.length;
  108.     while(j-->0) {
  109.       menuItem=opts[j];
  110.       match=menuItem.id?menuItem.id.match(/opt-(.*)/):null;
  111.       if(match) {
  112.         menuItem.setAttribute("checked",
  113.           g_flashGotService.getPref(match[1])?"true":"false"
  114.           );
  115.       }
  116.     }
  117.     
  118.     const dms=g_flashGotService.DMS;
  119.     var dm,menuItemId;
  120.     var anchorNode=document.getElementById("flashgot-menusep-dms");
  121.     for(j=dms.length; j-->0;) {
  122.       dm=dms[j];
  123.       menuItemId="flashgot-menuopt-dm-"+dm.name;
  124.       menuItem=document.getElementById(menuItemId);
  125.       if(!menuItem) {
  126.         menuItem=document.createElement("menuitem");
  127.         menuItem.setAttribute("id",menuItemId);
  128.         menuItem.setAttribute("name","flashgot-menuopt-dm");
  129.         menuItem.setAttribute("type","radio");
  130.         menuItem.setAttribute("autocheck","true");
  131.         menuItem.setAttribute("oncommand","g_flashGotService.defaultDM=\""+dm.name+"\"");
  132.         menuItem.setAttribute("label",dm.name);
  133.         anchorNode.parentNode.insertBefore(menuItem,anchorNode);
  134.       }
  135.       menuItem.setAttribute("checked",(dm.supported && defaultDM==dm.name)?"true":"false");
  136.       menuItem.setAttribute("disabled",dm.supported?"false":"true");
  137.       anchorNode=menuItem;
  138.     }
  139.     
  140.   }
  141. ,
  142.   menuShowing: function(ev) {
  143.     this.prepareMenus();
  144.   }
  145. ,
  146.   get srcWindow() {
  147.     return document.commandDispatcher.focusedWindow;
  148.   }
  149. ,
  150.   get srcDocument() {
  151.     return this.srcWindow.document;
  152.   }
  153. ,
  154.   get popupLink() { 
  155.     return g_flashGotService.findLinkAsc(document.popupNode);
  156.   }
  157. ,
  158.   get linksCount() {
  159.     const doc=this.srcDocument;
  160.     var count=doc.links.length;
  161.     if(g_flashGotService.getPref("includeImages")) count+=doc.images.length;
  162.     return count;
  163.   }
  164. ,
  165.   getLinks: function(filter,includeImages) {
  166.     var allLinks=filterLinks(this.srcDocument.links,filter);
  167.     const urlResolver=Components.classes['@mozilla.org/network/io-service;1'].getService(
  168.       Components.interfaces.nsIIOService).newURI(this.srcDocument.location.href,null,null);
  169.  
  170.     if(includeImages) {
  171.       allLinks=allLinks.concat(filterLinks(                                     
  172.         this.srcDocument.images,
  173.         function(l) {
  174.           l.href=urlResolver.resolve(l.src)
  175.           if(!l.title) l.title=l.alt+" ("+l.width+","+l.height+")";
  176.           return filter(l);
  177.         }));
  178.     }
  179.     
  180.     allLinks=allLinks.concat(filterLinks(
  181.       this.srcDocument.getElementsByTagName("embed"),
  182.       function(l) {
  183.           l.href=urlResolver.resolve(l.src);
  184.           if(!l.title) l.title=l.alt?l.alt:l.type;
  185.           return filter(l);
  186.       }));
  187.     return allLinks;
  188.     
  189.     function filterLinks(allLinks,filter) {
  190.       const links=new Array();
  191.       for(var j=0,len=allLinks.length; j<len; j++) {
  192.         var l=allLinks[j];
  193.         if(filter(l)) {
  194.           links[links.length]=l;
  195.         }
  196.       }
  197.       return links;
  198.     }
  199.   }
  200. ,
  201.   get referrer() {
  202.     if(this._referrer) return this._referrer;
  203.     var doc=this.srcDocument;
  204.     var ref=doc._flashgotGB_referrer;
  205.     return ref?ref:doc.location.href;
  206.   }
  207. ,  
  208.   set referrer(r) {
  209.     return this._referrer=r; 
  210.   }
  211. ,
  212.   getSelectionLinks: function(includeImages) {
  213.     var sel=this.srcWindow.getSelection();
  214.     
  215.     this.selection=sel;
  216.     var links=this.getLinks(function(link) {
  217.       return g_flashGotService.checkLink(link) && flashGot.selection.containsNode(link,true); 
  218.     }, includeImages);
  219.     const httpFixer=/^h.{2}p/i;
  220.     const noprotFixer=/^([a-z]+[a-z\d]+\.[a-z\d\.]+\/)/i;
  221.     const m=this.selection.toString().match(/\b(h.{2}p:\/\/|ftp:\/\/|mms:\/\/|rtsp:\/\/|[a-z]+[a-z\d]+\.[a-z\d\.]+\/)[^\s]*/gi);
  222.     if(m) {
  223.       var textLinks=new Array();
  224.       for(var j=0, len=m.length; j<len; j++) {
  225.         var href=m[j].replace(httpFixer,"http").replace(noprotFixer,"http://$1");
  226.         for(var k=links.length; k-->0;) {
  227.           if(links[k].href.toLowerCase()==href.toLowerCase()) {
  228.             href=null; break;
  229.           }
  230.         }
  231.         if(href) {
  232.           textLinks[textLinks.length]={href: href, title: m[j], innerHTML: "", tagName: "flashgottextlink"};
  233.         }
  234.       }
  235.       links=links.concat(textLinks);
  236.     }
  237.     return links;
  238.   }
  239. ,
  240.   downloadPopupLink: function(dmName) {
  241.     const link=this.popupLink;
  242.     if(link != null) {
  243.       this.download([link], dmName);
  244.     }
  245.   }
  246. ,
  247.   downloadLink: function(dmName) {
  248.     const sel=this.srcWindow.getSelection();
  249.     this.log("hover: "+this.hoverElement);
  250.     var link=g_flashGotService.checkAnchor(sel.anchorNode)
  251.       ?sel.anchorNode
  252.       :(link=g_flashGotService.findLinkAsc(this.hoverElement))?link:null
  253.       ;
  254.     this.log("link: "+link);
  255.     if(link) {
  256.       this.download([link], g_flashGotService.OP_ONE, dmName);
  257.     }
  258.   }
  259. ,
  260.   downloadSel: function(dmName) {
  261.     this.download(this.getSelectionLinks(g_flashGotService.getPref("includeImages")), 
  262.       g_flashGotService.OP_SEL,dmName);
  263.   }
  264. ,
  265.   downloadAll: function(dmName) {
  266.     const links=this.getLinks(
  267.       g_flashGotService.checkLink,
  268.       g_flashGotService.getPref("includeImages"));
  269.     this.download(links, g_flashGotService.OP_ALL, dmName);
  270.   }
  271. ,
  272.   download: function(links, opType, dmName) {
  273.     links.document=this.srcDocument;
  274.     links.window=this.srcWindow;
  275.     links.referrer=this.referrer;
  276.     links.cookie=links.document.cookie;
  277.     return g_flashGotService.download(links, opType, dmName);
  278.   }
  279. ,
  280.   buildGallery: function() {
  281.     var previewURL=null, contentURL=null;
  282.     var doc=this.srcDocument;
  283.     if(doc._flashgotGB_previews || doc._flashgotGB_contents) {
  284.       previewURL=doc._flashgotGB_previews;
  285.       contentURL=doc._flashgotGB_contents;
  286.     } else {
  287.       var links=this.getSelectionLinks(true);
  288.       if(links.length==0) {
  289.          links=this.popupLink
  290.           ?[this.popupLink]
  291.           :this.srcDocument.links;
  292.       }
  293.       
  294.       var previewRX=/\d+.*\.(jpg|jpeg|png|gif|bmp)(\?|$)/i;
  295.       var contentRX=/\d+.*\.[a-z]{2,4}(\?|$)/i;
  296.       for(var j=0, len=links.length; j<len && ! (contentURL && previewURL); j++) {
  297.         var l=links[j];
  298.         var tag=l.tagName.toLowerCase();
  299.         var url=l.href;
  300.         if( (!previewURL) && (tag=="img" || previewRX.test(url)) ) {
  301.           previewURL=url;
  302.         } else {
  303.           if(tag!="img" && contentRX.test(url)) {
  304.             contentURL=url;
  305.             if(tag=="a") {
  306.               var imgs=l.getElementsByTagName("img");
  307.               for(var i=0; i<imgs.length; i++) {
  308.                 if(previewRX.test(imgs[i].src)) {
  309.                   previewURL=imgs[i].src;
  310.                   break;
  311.                 }
  312.               }
  313.             }
  314.           }
  315.         }
  316.       }
  317.       if(!previewURL) previewURL="";
  318.       if(previewURL && !contentURL) contentURL=previewURL;
  319.     }
  320.     window.openDialog("chrome://flashgot/content/flashgotGalleryBuilder.xul","_blank",
  321.       "chrome,dialog,centerscreen",
  322.       { 
  323.         previewURL: previewURL, 
  324.         contentURL: contentURL, 
  325.         referrerURL: this.referrer,
  326.         originalWindow: window,
  327.         srcDocument: this.srcDocument,
  328.         tmpDir: g_flashGotService.tmpDir
  329.       }
  330.     );
  331.   }
  332.  
  333. }
  334.  
  335.  
  336.  
  337. window.addEventListener("load",  
  338.   function(loadEv) {
  339.     new FlashGot();
  340.   }, false);
  341.  
  342.  
  343.  
  344.