Jump to content

User:Gary/highlight nearby links.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.
/*
	HIGHLIGHT NEARBY LINKS
	Description: If two links are next to each other so it's hard to tell if they are one link or two separate ones (such as [[New York]] [[City]]),
		both links will be underlined so that it is clear that they are separate.
*/

if (typeof(unsafeWindow) != 'undefined')
{
	mw = unsafeWindow.mw;
}

$(highlightNearbyLinks);

function highlightNearbyLinks()
{
	if (!(mw.config.get('wgAction') == 'view' || (mw.config.get('wgPageName') == 'Wikipedia:Sandbox' && mw.config.get('wgAction') == 'submit'))) return false;
	
	mw.util.addCSS('a.highlight-nearby { text-decoration: underline; }');
	mw.util.addCSS('a.highlight-nearby:hover { text-decoration: none; }');
	
	var bodyId = mw.config.get('wgPageName') == 'Wikipedia:Sandbox' && mw.config.get('wgAction') == 'submit' ? 'wikiPreview' : 'bodyContent';
	var body = $('#' + bodyId);
	if (!body.length) return false;
	var links = $('a', body);
	
	// loop through links in page body
	links.each(function()
	{
		var link = $(this);
		var next = link.next();
		
		// NOT: next node is a link, preceded by spaces
		// NOT: next node is a link and is also the next sibling
		if (!(next.length && next[0].nodeName == 'A' && ((link[0].nextSibling.nodeType == 3 && link[0].nextSibling.nodeValue.replace(/ /g, '') == '') || link[0].nextSibling == next[0]))) return true;
		
		// NOT: current and next links are not images
		if (!(!link.hasClass('image') && !next.hasClass('image'))) return true;
		
		// this is a refname
		if ((link.children().length && link.children().first()[0].nodeName == 'SUP') || (next.children().length && next.children().first()[0].nodeName == 'SUP')) return true;
		
		link.addClass('highlight-nearby');
		next.addClass('highlight-nearby');
	});
}