Jump to content

User:Technical 13/Scripts/Gadget-codeBacklinks.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
( function ( mw, $ ) {
	$( 'pre.source-' + mw.config.get( 'wgPageContentModel' ) ).find( 'span.co1, span.coMULTI' ).each( function() {
		var codeComment = $( this ).html().substr( $( this ).html().indexOf( '[[' ) );
		var wikiLink = '', wikiLinkPipe = '', replaceWikiLink = '', extraLinks =  '';// declare variables
		while ( codeComment.indexOf( '[[' ) !== -1 && codeComment.indexOf( ']]' ) !== -1 ){
			if ( codeComment.indexOf( '|' ) !== -1 &&
				codeComment.indexOf( '|' ) > codeComment.indexOf( '[[' ) &&
				codeComment.indexOf( '|' ) < codeComment.indexOf( ']]' ) ) {
				wikiLink = codeComment.slice( codeComment.indexOf( '[[' ) + 2, codeComment.indexOf( '|' ) );
				wikiLinkPipe = codeComment.slice( codeComment.indexOf( '|' ), codeComment.indexOf( ']]' ) );
			} else {
				wikiLink = codeComment.slice( codeComment.indexOf( '[[' ) + 2, codeComment.indexOf( ']]' ) );
			}
			replaceWikiLink = codeComment.slice( codeComment.indexOf( '[[' ), codeComment.indexOf( ']]' ) + 2 );
			codeComment = codeComment.substr( codeComment.indexOf( ']]' ) + 2 );
			if ( wikiLink.search( 'Special:' ) === -1 ) {
			extraLinks =  '&thinsp;<small style="font-style: normal;">(&thinsp;<a href="' +
				mw.util.getUrl( wikiLink, { action: 'edit#editform' } ) +
				'" title="Please use [show preview] before saving.">edit</a>&thinsp;|&thinsp;<a href="' +
				mw.util.getUrl( 'Special:WhatLinksHere/' + wikiLink ) +
				'" title="A list of all wiki pages that link here.">wlh</a>&thinsp;)</small>';
			}
			$( this ).html( $( this ).html().replace( replaceWikiLink, '<b>[[</b><a href="' +
				mw.util.getUrl( wikiLink ) + '" title="' + wikiLink + '">' + wikiLink + '</a><b>' +
				wikiLinkPipe + ']]</b>' + extraLinks ) );
			wikiLink = '', wikiLinkPipe = '', replaceWikiLink = '', extraLinks =  '';
		}
	} );
} ( mediaWiki, jQuery ) );