User:Gary/small lead languages.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/small lead languages. |
/*
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);