// ==UserScript==
// @name Extra download links
// @author João Eiras 
// @namespace http://userjs.org/ 
// @version 1.0
// @description  Adds links to all images, embeds, objects, applets,
//			and iframes on the page, to make them easier to
//			download using Opera's links panel.
// @ujs:category general: enhancements
// @ujs:published 2006-01-23 23:06
// @ujs:modified 2006-01-24 10:45
// @ujs:documentation http://userjs.org/scripts/general/enhancements/extra-download-links 
// @ujs:download http://userjs.org/scripts/download/general/enhancements/extra-download-links.js
// ==/UserScript==

/* 
 * Copyright © 2006 by João Eiras
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */


window.addEventListener('load', function(){
	
	//indicate whether you want to see the links placeholder
	var showLinks = false;
	//indicate if you want to add image links
	var addImageLinks = true;
	//indicate if you want to add iframe links
	var addIframeLinks = true;
	//indicate if you want to add object, embed and applet links
	var addObjectLinks = true;
	
	
	//rest of code... don't mess this
	if( !document.body )
		return;
	var capitalizeString=function(str){return str.substr(0,1).toUpperCase()+str.substr(1).toLowerCase();}
	var resolveurl = function(url){var link = document.createElement("a");link.href=url;url=link.href;delete link;return url;}

	var addelemsto = function(a,b){
		for(var k=0;k<b.length;k++){
			var elem = b[k];
			if( !elem.tagName || !elem.nodeType || elem.nodeType != 1 )
				continue;
			var tagname = capitalizeString(elem.tagName);
			var linktext="", linkurl="";
			if( tagname == "Object"){
				linkurl = elem.getAttribute("data");
				if(!linkurl){
					linkurl = elem.getAttribute("src");//xhtml 2.0 :)
					if(!linkurl){
						for(var j=0,c=elem.childNodes;j<c.length;j++){
							var elem2,theatr;elem2 = c[j];
							if( !elem2.tagName || elem2.tagName.toLowerCase()!="param" )
								continue;
							theatr = elem2.getAttribute("name");
							if( theatr == "movie" || theatr == "data" ||
								theatr == "src" || theatr == "code" ){
								linkurl = elem2.getAttribute("value");
								if(linkurl)
									break;
							}
						}
					}
				}
			}else if( tagname == "Iframe"){
				try{linkurl = elem.contentWindow.location.href;}
				catch(ex){linkurl = elem.getAttribute("src");}
			}else if( tagname == "Embed"){
				linkurl = elem.getAttribute("src");
			}else if( tagname == "Applet"){
				//http://java.sun.com/docs/books/tutorial/applet/appletsonly/appletTag.html
				linkurl = (elem.getAttribute("codebase")+"/"+elem.getAttribute("code")).replace(/(^|\w)\/\/\/?/g,"$1/");
			}else if( tagname == "Img"){
				linkurl = elem.getAttribute("src");
			}else{
				continue;
			}
			if(!linkurl)
				continue;
			linkurl = resolveurl(linkurl);
			linktext = "[UserJS ExtraLinks "+tagname+"] "+linkurl.match(/[^\\\/]+(\?.*)?$/)[0];
			a[linkurl] = linktext;
		}
		return a;
	}
	 
	var elems = new Object();
	if(addImageLinks)
		elems = addelemsto(elems,document.getElementsByTagName('img'));
	if(addIframeLinks)
		elems = addelemsto(elems,document.getElementsByTagName('iframe'));
	if(addObjectLinks){
		elems = addelemsto(elems,document.getElementsByTagName('applet'));
		elems = addelemsto(elems,document.getElementsByTagName('embed'));
		elems = addelemsto(elems,document.getElementsByTagName('object'));
	}
	
	var thedivcss;
	var theacss;
	if(showLinks){
		thedivcss="border:medium dotted grey;overflow:visible;"+
			"background-color:white;text-align:left;display:inline-block;";
		theacss="color:black;font-weight:bold;background-color:none;margin:.2em;"+
			"cursor:hand;font-family:Trebuchet MS,Verdana,Arial;font-size:10pt;";
	}else{
		thedivcss=theacss="display:none";
	}
	var anc,thedivhtml = "";
	for(anc in elems)
		thedivhtml += "<a style=\""+theacss+"\" href=\""+anc+"\">"+elems[anc]+"</a><br>"
	if(!thedivhtml)
		//no links - goodbye
		return;
	var thediv = document.createElement("div");
	thediv.setAttribute("id","ujs_extra_links_place_holder");
	thediv.setAttribute("style",thedivcss);
	thediv.innerHTML = thedivhtml;
	if(showLinks)document.body.appendChild(document.createElement('br'));
	document.body.appendChild(thediv);
	
}, false );