// ==UserScript==
// @name           cvtUrlToLink.user.js
// @namespace      http://furyu.tea-nifty.com/
// @description    convert URLs to hyperlinks ver.0.01a
// @include        *
// @type           SleipnirScript
// ==/UserScript==

(function(){

var	mainScript=function(){

// === Options
var	linkTarget='_blank';
var	excludeApostrophe=true;
var	delKeywordLink=true;	//	for Hatena::Diary
var	flgConvert=true;
var	cvtMark='*** converted ***';
	
var	debug=false;

// === Preparation
var	oWindow=window;
if (typeof _window!='undefined'&&typeof _document!='undefined') {
	var	w=_window,d=_document;
}
else {
	if (typeof unsafeWindow!='undefined') {
		window=unsafeWindow;
		document=window.document;
	}
	var	w=window,d=w.document,sleipnir=w.sleipnir;
}

// === Funtions
var	log=function(text) {
	if (!debug) return;
	try {
		var	message='[urlToLink] '+text;
		if (typeof sleipnir!='undefined') {
			sleipnir.Output.Visible=true;
			sleipnir.Echo(message);
		}
		else if (typeof console!='undefined') {
			console.log(message);
		}
		else {
			GM_log(message);
		}
	}
	catch (e) {
		prompt('[urlToLink]',text);
	}
};

// === Main
var	tim1=new Date().getTime();
if (!linkTarget) linkTarget='_self';
var	work=d.createElement('div');
var	regUrl=(excludeApostrophe)?/(h?)(ttps?:\/\/[-_.!~*()a-zA-Z0-9;/?:@&=+$,%#]+)/mgi:/(h?)(ttps?:\/\/[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+)/mgi;
var	getLink=function(mt,m1,m2) {
	var	text=m1+m2,url=(m1)?text:('h'+m2);
	return '<a href="'+url+'" target="'+linkTarget+'" title="'+cvtMark+'">'+text+'<\/a>';
};
if (cvtMark) {
	var	cvtObj=d.createElement('span');
	cvtObj.innerHTML=cvtMark;
	cvtObj.style.fontSize='80%';
}
else {
	var	cvtObj=null;
}
var	urlToLink=function(obj){
	if (delKeywordLink&&obj.nodeType==1&&w.location.href.match(/^https?:\/\/(d|\w+\.g)\.hatena\.ne\.jp\//)) {
		var	tmpLinks=obj.getElementsByTagName('a'),links=[];
		for (var ci=0,len=tmpLinks.length; ci<len; ci++) {
			if (tmpLinks[ci].className=='keyword') links[links.length]=tmpLinks[ci];
		}
		for (var ci=0,len=links.length; ci<len; ci++) {
			var	link=links[ci];
			var	parent=link.parentNode,prevObj=link.previousSibling,nextObj=link.nextSibling,chkObj;
			while (link.firstChild) parent.insertBefore(link.firstChild,link);
			parent.removeChild(link);
			if (prevObj&&prevObj.nodeType==3) {
				while ((chkObj=prevObj.nextSibling)&&chkObj.nodeType==3) {
					prevObj.nodeValue+=chkObj.nodeValue;
					parent.removeChild(chkObj);
					if (chkObj===nextObj) {
						nextObj=null;
						break;
					}
				}
			}
			if (nextObj&&nextObj.nodeType==3) {
				while ((chkObj=nextObj.previousSibling)&&chkObj.nodeType==3) {
					nextObj.nodeValue=chkObj.nodeValue+nextObj.nodeValue;
					parent.removeChild(chkObj);
					if (chkObj===prevObj) break;
				}
			}
		}
	}
	var	callFunc=function(obj,parent){
		while(obj){
			var	nextNode=obj.nextSibling;
			switch (obj.nodeType) {
				case	1	:
					var	tagName=obj.tagName.toUpperCase();
					if (tagName=='A'||tagName=='TEXTAREA') break;
					callFunc(obj.firstChild,obj);
					break;
				case	3	:
					if (!parent) break;
					if (!obj.nodeValue.match(regUrl)) break;
					work.innerHTML=obj.nodeValue.replace(/</mg,'&lt;').replace(/>/mg,'&gt;').replace(regUrl,getLink);
					while (work.firstChild) parent.insertBefore(work.firstChild,obj);
					parent.removeChild(obj);
					break;
			}
			obj=nextNode;
		}
	};
	if (flgConvert) callFunc(obj);
};

var	AutoPagerize=w.AutoPagerize||oWindow.AutoPagerize;
if (AutoPagerize) {
	var	addFilter=(AutoPagerize.addElementFilter)?AutoPagerize.addElementFilter:AutoPagerize.addFilter;
	if (addFilter) {
		addFilter(function(nodes,url,siteinfo){
			for (var ci=0,len=nodes.length; ci<len; ci++) {
				urlToLink(nodes[ci]);
			}
		});
	}
};
urlToLink(d.body);

var	tim2=new Date().getTime();
log('processing time:'+(tim2-tim1)+'ms');

};	//	end of mainScript()

if (typeof sleipnir!='undefined'&&sleipnir&&typeof _window!='undefined') {
	_window.sleipnir=sleipnir;
	try {
		_window.execScript('('+mainScript.toString()+')();','JavaScript');
	}
	catch(e){
		mainScript();
	};
	_window.sleipnir=null;
}
else {
	mainScript();
}

})();
