User:Gary/small lead languages.js

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.
/*
	SMALL LEAD LANGUAGES
	Description: Makes text in lead paragraph that provides translations to be smaller.
	
	TODO
	- Doesn't work with {{nihongo}}.
*/

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

function smallLeadLanguages()
{
	if (mw.config.get('wgCanonicalNamespace') != '' && mw.config.get('wgPageName') != 'User:Gary_King/Sandbox') return false;
	
	var openingBracket = false;
	var closingBracket = false;
	var bracketNode = $('<span></span>');
	var beforeBracket, insideBrackets, afterBracket;

	// Find first paragraph by finding the first one with text().
	var firstParagraph;
	$('#mw-content-text > p').each(function()
	{
		var node = $(this);
		if ($('#coordinates', node).length) return true;
		if (node.text())
		{
			firstParagraph = node;
			return false;
		}
	});
	if (!firstParagraph) return false;

	firstParagraph.contents().each(function()
	{
		var node = $(this);
		if (openingBracket) bracketNode.append(node);
		if (node[0].nodeType != 3) return true;
		
		// If current node's text is over 50 characters from beginning, then cancel this.
		if (!openingBracket && firstParagraph.text().indexOf(node[0].nodeValue) > 50) return false;
		
		// Found both opening and closing brackets in the same node.
		if (!openingBracket && node[0].nodeValue.indexOf('(') != -1 && node[0].nodeValue.indexOf(')') != -1)
		{
			openingBracket = true;
			closingBracket = true;
			
			beforeBracket = node[0].nodeValue.substring(0, node[0].nodeValue.indexOf('('));
			insideBrackets = node[0].nodeValue.substring(node[0].nodeValue.indexOf('('), node[0].nodeValue.indexOf(')') + 1);
			afterBracket = node[0].nodeValue.substring(node[0].nodeValue.indexOf(')') + 1);
			
			node.before(beforeBracket);
			bracketNode.append(insideBrackets);
			node.after(afterBracket);
			node.replaceWith(bracketNode);
			
			return false;
		}
		// Found opening bracket
		else if (!openingBracket && node[0].nodeValue.indexOf('(') != -1)
		{
			openingBracket = true;
			beforeBracket = node[0].nodeValue.substring(0, node[0].nodeValue.indexOf('('));
			afterBracket = node[0].nodeValue.substring(node[0].nodeValue.indexOf('('));
			
			node.before(beforeBracket);
			node.before(bracketNode);
			bracketNode.append(afterBracket);
			node.remove();
			
			return true;
		}
		// Found closing bracket
		else if (!closingBracket && node[0].nodeValue.indexOf(')') != -1)
		{
			closingBracket = true;
			beforeBracket = node[0].nodeValue.substring(0, node[0].nodeValue.indexOf(')') + 1);
			afterBracket = node[0].nodeValue.substring(node[0].nodeValue.indexOf(')') + 1);
			
			bracketNode.contents().last().remove();
			bracketNode.append(beforeBracket);
			bracketNode.after(afterBracket);
			
			return false;
		}
	});
	
	if (bracketNode.text().length > 50)
	{
		bracketNode.css('font-size', '.85em');
		bracketNode.attr('title', 'Text is smaller for better contrast.');
	}
	// Replace the temporary SPAN with its contents.
	else
	{
		bracketNode.contents().each(function()
		{
			$(this).parent().before($(this).clone());
		});
		
		bracketNode.remove();
	}
}

$(smallLeadLanguages);