Jump to content

User:Fred Gandt/wikilinkPlus.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.
/* Hold down an ALT key when clicking any link to bring up ALTernative related destinations */

( function( d ) {
	function init() {
		var talk = function( ttl, rslt ) {
				rslt = ( /^(?:Talk|(\w+)_talk)\:((?:[^\/]+)(?:\/[^]+)?)$/ ).exec( ttl );
				if ( !!rslt ) {
					if ( rslt[ 1 ] ) {
						return [ rslt[ 1 ] + ":" + rslt[ 2 ], "Page" ];
					}
					return [ rslt[ 2 ], "Article" ];
				}
				rslt = ( /^(?:(\w+)\:)?((?:[^\/]+)(?:\/[^]+)?)$/ ).exec( ttl );
				return rslt[ 1 ] ? [ rslt[ 1 ] + "_talk:" + rslt[ 2 ], "Talk" ] : [ "Talk:" + rslt[ 2 ], "Talk" ];
			},
			user = function( ttl, usr ) {
				usr = ( /^User(?:_talk)?\:(?:([^\/]+)(?:\/[^]+)?)/ ).exec( ttl );
				return !!usr ? '<a href="/wiki/Special:Contributions/' + usr[ 1 ] + '">Contribs</a>' : "";
			},
			extraLinks = function( ttl, tlk ) {
				tlk = talk( ttl );
				return '<a href="/wiki/' + tlk[ 0 ] + '">' + tlk[ 1 ] + '</a><a href="/w/index.php?title=' + ttl +
				'&action=edit">Edit</a><a href="/w/index.php?title=' + ttl + '&action=history">History</a>' + user( ttl );
			};
		mw.loader.load( "/w/index.php?title=User:Fred_Gandt/wikilinkPlus.css&action=raw&ctype=text/css", "text/css" );
		d.getElementById( "mw-content-text" ).addEventListener( "click", function( evt, t, tt, ewl ) {
			if ( evt.altKey ) {
				t = evt.target;
				tt = t.title;
				if ( t.tagName.toLowerCase() === "a" && !t.getAttribute( "class" ) && !t.parentElement.classList.contains( "mw-editsection" ) && tt && tt.substring( 0, 8 ) != "Special:" ) {
					evt.preventDefault();
					ewl = d.createElement( "div" );
					ewl.setAttribute( "class", "fg-wikilink-plus-newi" );
					ewl.innerHTML = extraLinks( mw.util.wikiUrlencode( tt ) );
					t.appendChild( ewl );
				}
			}
		}, false );
	}
	$( d ).ready( init );
} ( document ) );