User:Gary/highlight nearby links.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Gary/highlight nearby links. |
/*
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');
});
}