MediaWiki:Common.js

Revision as of 06:52, 18 August 2026 by Admin (talk | contribs) (Link MII/IPW team codes in research notes.)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * Hide leftover imported rows the infobox no longer shows.
 * Put the research-notes disclaimer under that section heading.
 * Turn MII / IPW / 439-G style codes in notes into team links.
 */
$(function () {
	$('.infobox th, .soldier-infobox th, table.wikitable td').each(function () {
		var text = $(this).text().replace(/\s+/g, ' ').trim();
		if (
			text === 'Overseas' ||
			text === 'Over Seas Flag' ||
			text === 'Special'
		) {
			$(this).closest('tr').remove();
		}
	});

	var $heading = $('#Research_notes').closest('h2');
	var $disclaimer = $('.mw-parser-output > .research-notes-disclaimer').first();
	if ($heading.length && $disclaimer.length) {
		$disclaimer.insertAfter($heading);
	}

	function teamHref(name) {
		return '/index.php?title=' + encodeURIComponent('Team ' + name).replace(/%20/g, '_');
	}
	function teamLink(name, label) {
		return '<a href="' + teamHref(name) + '" title="Team ' + name + '">' + label + '</a>';
	}
	function linkifyTeams(raw) {
		var text = $('<div>').text(raw).html();
		text = text.replace(/\bMII\s+(\d{2,4})\s*[-–]?\s*([A-Za-z])\b/gi, function (_, num, letter) {
			var name = 'MII ' + num + '-' + letter.toUpperCase();
			return teamLink(name, 'MII ' + num + '-' + letter.toUpperCase());
		});
		text = text.replace(/\bMII\s+(\d{2,4})\b/gi, function (_, num) {
			return teamLink('MII ' + num, 'MII ' + num);
		});
		text = text.replace(/(?<!\bMII\s)\b([4-5]\d{2})\s*[-–]\s*([GIgi])\b/g, function (_, num, letter) {
			var name = 'MII ' + num + '-' + letter.toUpperCase();
			return teamLink(name, num + '-' + letter.toUpperCase());
		});
		text = text.replace(/\bIPW\s+(\d{1,4})\b/gi, function (_, num) {
			return teamLink('IPW ' + num, 'IPW ' + num);
		});
		return text;
	}

	var negative = /fail(?:ed|ure)?|flunk|wash(?:ed)?\s*out|kick(?:ed)?\s*out|dismiss|expel|unsatisfactor|poor\s+(?:performance|work)|awol|court[-\s]?martial|disciplin|discharged\s+for\s+cause|drop(?:ped)?\s+(?:out|from)|relieved|eliminat|unfit|misconduct|desert/i;
	$heading.nextAll('ul').first().find('li').each(function () {
		var $li = $(this);
		var kept = [];
		$.each($li.text().split(';'), function (_, clause) {
			clause = $.trim(clause);
			if (clause && !negative.test(clause)) {
				kept.push(clause);
			}
		});
		if (kept.length) {
			$li.html(linkifyTeams(kept.join('; ')));
		} else {
			$li.remove();
		}
	});
});