User:SoledadKabocha/talklink.js

( function ( mw, $ ) {	var ns = mw.config.get( 'wgNamespaceNumber' ),		title = mw.config.get( 'wgTitle' );

if ( ns < 0 ) { return; }

mw.loader.using( [ 'mediawiki.Title', 'mediawiki.api' ], function {		$( function  { var $catalk = $( '#ca-talk' ); if ( !$catalk.length || $catalk.hasClass( 'new' ) ) { return; }

new mw.Api.get( {				action: 'query',				titles: new mw.Title( title, ns | 1 ).getPrefixedText,				prop: 'revisions',				rvprop: 'content',				indexpageids: 1			} ).done( function ( r ) {				var p, s, o;

if ( !r || !r.query || !r.query.pages || !r.query.pageids || !r.query.pageids[0] ) { return; }				p = r.query.pages[r.query.pageids[0]]; if ( !p || !p.revisions || !p.revisions[0] || !p.revisions[0]['*'] ) { return; }

s = p.revisions[0]['*'].replace( //g, '' ); var redirectRegex = /^#REDIRECT:? *\[\[(.+)\]\]/i; if ( redirectRegex.test( s ) ) { var $realTalkLink = $catalk.find( 'a' ); $catalk.addClass( 'redirect' ); if ( window.talklinkDisableRedirection ) { var origHref = $realTalkLink.attr( 'href' ); $realTalkLink.attr( 'href', origHref + ( origHref.indexOf('?') == -1 ? '?' : '&' ) + 'redirect=no' ); }					var redirectTarget = redirectRegex.exec( s ); redirectTarget = redirectTarget[1]; if ( window.talklinkShowRedirectTarget ) { var origTitle = $realTalkLink.attr( 'title' ); $realTalkLink.attr( 'title', origTitle + '\n⤷' + redirectTarget ); }					if ( redirectTarget.replace( /_/g, ' ' ) == mw.config.get('wgPageName').replace( /_/g, ' ' ) ) { $catalk.addClass( 'self-redirect' ); }					return; }

do { o = s;					s = s.replace( //g, '' ); s = s.replace( //g, '' ); } while ( o !== s );

if ( /^\s*$/.test( s ) ) { $catalk.addClass( 'empty' ); return; }			} );		} );	} ); }( mediaWiki, jQuery ) );