Shoutbox Linkify

Links certain text (e.g. /profile/pet/42) in the shoutbox.

Versión del día 12/9/2015. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         Shoutbox Linkify
// @namespace    fortytwo
// @homepageURL	 https://greasyfork.org/en/users/14247-fortytwo
// @supportURL	 http://games-fortytwo.tumblr.com/tagged/shoutbox%20linkify
// @version      1.1
// @description  Links certain text (e.g. /profile/pet/42) in the shoutbox.
// @author       fortytwo
// @match        http://www.clanheart.com/*
// @grant        GM_getValue
// @grant		 GM_setValue
// @noframes
// @compatible	 chrome
// @compatible	 firefox
// ==/UserScript==
/***
	NOTICE: YOU ARE AGREEING THAT ANY USE OF THE FOLLOWING SCRIPT IS AT
	YOUR OWN RISK. I DO NOT MAKE ANY GUARANTEES THE SCRIPT WILL WORK, NOR 
	WILL I HOLD MYSELF ACCOUNTABLE FOR DAMAGE TO YOUR DEVICE.

	WHILE THE SCRIPT IS UNLIKELY TO CAUSE ANY HARM, AS WITH ALL TECHNICAL
	COMPONENTS, BUGS AND GLITCHES CAN HAPPEN.

	IF THE SCRIPT ISN'T WORKING FOR YOU, FEEL FREE TO SEND ME A MESSAGE: http://games-fortytwo.tumblr.com/
***/
(function(){
	var replaceables =[
		//profile/pet/42
		{ match: /\/?profile\/pet\/([0-9]+)\/?/gim, content: "<a href='http://clanheart.com/profile/pet/$1'>pet #$1</a> "},
		//forums/topic/42?page=5
		{ match: /\/?forums\/topic\/([0-9]+)\/?\?page=([0-9]+)/gim, content: "<a href='http://clanheart.com/forums/topic/$1?page=$2'>topic #$1?page=$2</a> "},
		//forums/topic/42
		{ match: /\/?forums\/topic\/([0-9]+)\/?/gim, content: "<a href='http://clanheart.com/forums/topic/$1'>topic #$1</a> "},
		//trading/make_offer/42
		{ match: /\/?trading\/make_offer\/([0-9]+)\/?/gim, content: "<a href='http://clanheart.com/trading/make_offer/$1'>offer on pet #$1</a> "},
		//settings/changeClan, settings/whatever
		{ match: /\/?settings\/([0-9a-z]+)\/?/gim, content: "<a href='http://clanheart.com/settings/$1'>settings: #$1</a> "}
	];
	
	//Testing
	console.log(replaceables);
	console.log(linkify("settings/changeclan 42 lfkmfk.nf \n\nlorem /profile/pet/42 fkfnjk /profile/pet/42 42 lorem ipsum \n` 42"));
	

	function linkify(text){
		for(var i = 0; i < replaceables.length; ++i){
			text = text.replace(replaceables[i].match, replaceables[i].content);
		}
		return text;
	};

	function magic(){
		var posts = document.getElementById('shoutbox-panel').getElementsByClassName('col-md-10');
		for(var i = 0; i < posts.length; ++i){
			//Fetch data. We want to get the string itself, manipulate it
			//and then add time and user link back in
			var post = posts[i];
			var e_userlink = post.getElementsByClassName('sb-link')[0];
			var e_time = post.getElementsByClassName("shoutbox-date")[0];

			var a = {
				href: e_userlink.href,
				html: e_userlink.innerHTML
			};

			var time = e_time.innerHTML;

			post.removeChild(e_userlink);
			post.removeChild(e_time);
	
			var text = linkify(post.innerHTML);
			post.innerHTML = [
				"<a href='"+a.href+"' class='sb-link'>"+a.html+"</a>",
				text,
				"<div class='shoutbox-date'>"+time+"</div>"
			].join("");
		}
	};

	//Make sure to monitor changes to the shoutbox
	var sbObserver = new MutationObserver(function(mutations){
		for(var i = 0; i < mutations.length; ++i){
			var mutation = mutations[i];

			if(mutation.addedNodes.length > 0){
				magic();
			}
		}
	}).observe(document.getElementById('shoutbox-panel'), { childList: true });
	
	//Initial state
	magic();
})();