// ==UserScript==
// @name Frameset links
// @author TarquinWJ 
// @namespace http://www.howtocreate.co.uk/ 
// @version 2.1.1
// @description  If frames are disabled, this script extracts the
//			addresses of all pages in the frameset, and
//			displays a list of links to the individual
//			frames.
// @ujs:category browser: enhancements
// @ujs:published 2005-07-12 20:23
// @ujs:modified 2005-09-22 21:13
// @ujs:documentation http://userjs.org/scripts/browser/enhancements/frameset-links 
// @ujs:download http://userjs.org/scripts/download/browser/enhancements/frameset-links.js
// ==/UserScript==


/* 
 * Please see
 * http://www.howtocreate.co.uk/operaStuff/userJavaScript.html#terms
 * for License and Terms of Use
 */

document.addEventListener(
	'load',
	function ujsenabledFramesetLinks() {

		//a big ugly load of regexps that try to make sense of link addresses that contain no titles/alt
		var tryToGetName = function (oAddress) {
			oAddress = oAddress.replace(/^\s+|\s+$/,'');
			oAddress = oAddress.replace(/\?.*$/g,'').replace(/#.*$/g,'').replace(/\.[^\.\\\/]*$/g,'').replace(/[\\\/](index|main|default)$/ig,'');
			oAddress = oAddress.replace(/[\\\/]+$/g,'').replace(/^((https?|ftp|goto|wais|nntp):)?(\/\/)?/ig,'').replace(/^www\./ig,'');
			if( oAddress.match(/javascript:/i) ) { return 'JavaScript'; }
			var oTmp = oAddress.substr(oAddress.search(/[\\\/][^\\\/]*$/)+1);
			return oTmp ? oTmp.replace(/_/g,' ') : 'Untitled';
		};

		//make links in framesets
		if( document.getElementsByTagName('frameset').length ) {
			var header = document.createElement('h2');
			header.appendChild(document.createTextNode('Frameset links'));
			var listGroup = document.createElement('ul');
			var frameList = document.getElementsByTagName('frame');
			for( var i = 0, oHref; frameList[i]; i++ ) {
				var listItem = document.createElement('li');
				listGroup.appendChild(listItem);
				var listLink = document.createElement('a');
				var frameName = frameList[i].getAttribute('name');
				if( !frameName ) { frameName = 'Frame '+i; }
				listLink.appendChild(document.createTextNode(frameName));
				listItem.appendChild(listLink);
				listLink.setAttribute('href',frameList[i].getAttribute('src'));
				if( oHref = frameList[i].getAttribute('longdesc') ) {
					listLink = document.createElement('a');
					listLink.className = 'citelongdesc';
					listLink.setAttribute('href',oHref);
					listLink.setAttribute('title','Description of frame content');
					listLink.appendChild(document.createTextNode('[More]'));
					listItem.appendChild(document.createTextNode(' '));
					listItem.appendChild(listLink);
				}
			}
			document.body.appendChild(header);
			document.body.appendChild(listGroup);
		}

		var elementList = [
			['blockquote','cite','Source of quoted text'],
			['q','cite','Source of quoted text'],
			['del','cite','Information about deleted text'],
			['ins','cite','Information about inserted text'],
			['img','longdesc','Description of image'],
		//	['frame','longdesc','Description of frame content'], //this is handled elsewhere
			['iframe','longdesc','Description of frame content']
		];

		//additional links for normal elements
		for( var i = 0, j, oHref; j = elementList[i]; i++ ) {
			for( var k = 0, oElements = document.getElementsByTagName(j[0]), oL; oL = oElements[k]; k++ ) {
				if( oHref = oL.getAttribute(j[1]) ) {
					var oA = document.createElement('a');
					oA.className = 'citelongdesc';
					oA.setAttribute('href',oHref);
					oA.setAttribute('title',j[2]);
					oA.appendChild(document.createTextNode('[More]'));
					try { oL.parentNode.insertBefore(oA,oL.nextSibling); } catch(e) { /* in case links are not allowed */ }
				}
			}
		}

		//image maps - convert image to object and put links in bullet points as fallback
		for( var k = 0, oElements = document.getElementsByTagName('img'), oL; oL = oElements[k]; k++ ) {
			if( oMap = oL.getAttribute('usemap') ) {
				var oA = document.createElement('object');
				var oUl = document.createElement('ul');
				oA.appendChild(oUl);
				for( var i = 0, j; j = oL.attributes[i]; i++ ) {
					oA.setAttribute(j.nodeName.replace(/^src$/i,'data'),j.nodeValue);
				}
				oMap = oMap.replace(/^.*#/,'');
				for( var i = 0, j, oMps = document.getElementsByTagName('map'); j = oMps[i]; i++ ) {
					if( oMap == j.getAttribute('name') ) {
						for( var p = 0, q, oETY = ['area','a']; q = oETY[p]; p++ ) {
							for( var n = 0, m, oArs = j.getElementsByTagName(q); m = oArs[n]; n++ ) {
								if( !m.getAttribute('href') ) { continue; }
								var oLi = document.createElement('li');
								var oLn = document.createElement('a');
								oLi.appendChild(oLn);
								oLn.className = 'citelongdesc';
								oLn.setAttribute('href',m.getAttribute('href'));
								if( m.title ) { oLn.title = m.title; }
								oLn.appendChild(document.createTextNode(m.getAttribute('alt')?m.getAttribute('alt'):(m.title?m.title:tryToGetName(m.href))));
								oA.firstChild.appendChild(oLi);
							}
						}
						break;
					}
				}
				oL.parentNode.replaceChild(oA,oL);
				k--;
			}
		}

	},
	false
);