MediaWiki:Gadget-GlobalScript.js

From the Super Mario Wiki, the Mario encyclopedia
Jump to navigationJump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all skins on both desktop and mobile */

/* Add autocollapse support to mw-collapsible */
function mwCollapsibleSetup($collapsibleContent) {
	var $element, autoCollapseThreshold = 2;
	$.each($collapsibleContent, function(index, element) {
		$element = $(element);
		if ($collapsibleContent.length >= autoCollapseThreshold && $element.hasClass('autocollapse'))
			$element.data('mw-collapsible').collapse();
	});
}
mw.hook('wikipage.collapsibleContent').add(mwCollapsibleSetup);

/* Fix search suggestions on mobile devices */
$(function() {
	$('#searchInput').on('input', function(e) {
		$(this).trigger(jQuery.Event('keydown', {
			keyCode: e.keyCode,
			which: e.which
		}));
		$(this).trigger(jQuery.Event('keypress', {
			keyCode: e.keyCode,
			which: e.which
		}));
	});
});

/* Clean-up Unicode */
function unicodeFixer() {
	var textAreas = ['#wpTextbox1', '#wikitext-editor'];
	for (var i = 0; i < textAreas.length; i++) {
		if ($(textAreas[i]).length)
			$(textAreas[i]).val($(textAreas[i]).val().replace(/[\u200B-\u200F\uFEFF]/g, '').replace(/[‘’]{3}/g, "'''").replace(/[‘’]{2}/g, "''"));
	}
}
$(function() {
	$('#searchform').on('submit', function() {
		$('#searchInput').val($('#searchInput').val().replace(/[‘’]/g, "'").replace(/[“”]/g, '"'));
	});
	if (['edit', 'submit'].includes(mw.config.get('wgAction')) && ![8, 274, 828, 2300].includes(mw.config.get('wgNamespaceNumber'))) {
		$('.editButtons input').click(unicodeFixer);
		if ($('body.skin-minerva').length)
			setTimeout(function() { $('.header-action button').click(unicodeFixer) }, 3000);
	}
});

/* Add disclaimer and URL to copied article text for newbies */
$(function() {
	if (!mw.config.get('wgUserGroups').includes('autoconfirmed')) {
		var site = 'the ' + mw.config.get('wgSiteName');
		var notice1 = 'The above text is from ' + site + ' and is available under a Creative Commons license.';
		var notice2 = 'Attribution must be provided through a list of authors or a link back to the original article.';
		var message = '\n\n' + notice1 + ' ' + notice2 + ' Source: ' + mw.config.get('wgServer');
		if (mw.config.get('wgNamespaceNumber') === 0) {
			$('#mw-content-text').on('copy', function(e) {
				var text = window.getSelection().toString();
				if (text.length > 250) {
					var articlePath = mw.config.get('wgArticlePath').replace('$1', encodeURIComponent(mw.config.get('wgPageName')));
					text = text.trim().concat(message + articlePath);
					e.originalEvent.clipboardData.setData('text/plain', text);
					e.preventDefault();
				}
			});
		}
		$('textarea').on('paste', function(e) {
			var clipboardData = e.clipboardData || window.clipboardData || e.originalEvent.clipboardData;
			var clipboard = clipboardData.getData('Text');
			regex = new RegExp(message + '[^ \n]+$');
			if (regex.test(clipboard)) {
				var input = $(this);
				var match = clipboard.match(regex)[0];
				var cursor = input[0].selectionStart + clipboard.length - match.length;
				setTimeout(function() {
					input.val(input.val().replace(match, ''));
					input[0].setSelectionRange(cursor, cursor);
				});
			}
		});
	}
});

/* Balance main page Shroom/MWE boxes and filler links */
$(function() {
	function mainPageMaintain() {
		if ($(window).width() >= 720) {
			var leftHeight = $('#featured').outerHeight(true) + $('#poll').outerHeight(true);
			var rightHeight = $('#news').outerHeight(true) + $('#dyk').outerHeight(true);
			var i, len; var divIds = ['shroom', 'mwe'];
			for (i = 0, len = divIds.length; i < len; ++i) {
				if (leftHeight < rightHeight) {
					$('#' + divIds[i] + '-left').attr('style', '');
					$('#' + divIds[i] + '-right').attr('style', 'display:none !important');
					leftHeight = leftHeight + $('#' + divIds[i] + '-left').outerHeight(true);
				} else {
					$('#' + divIds[i] + '-right').attr('style', '');
					$('#' + divIds[i] + '-left').attr('style', 'display:none !important');
					rightHeight = rightHeight + $('#' + divIds[i] + '-right').outerHeight(true);
				}
			}
			var heightDif; divIds = ['maintain', 'rc', 'random', 'faq', 'editing'];
			for (i = 0, len = divIds.length; i < len; ++i) {
				heightDif = Math.abs(leftHeight - rightHeight);
				if (heightDif > 25) {
					if ( leftHeight < rightHeight ) {
						$('#' + divIds[i] + '-left').attr('style', '');
						$('#' + divIds[i] + '-right').attr('style', 'display:none !important');
						leftHeight = leftHeight + $('#' + divIds[i] + '-left').outerHeight(true);
					} else {
						$('#' + divIds[i] + '-right').attr('style', '');
						$('#' + divIds[i] + '-left').attr('style', 'display:none !important');
						rightHeight = rightHeight + $('#' + divIds[i] + '-right').outerHeight(true);
					}
				} else {
					$('#' + divIds[i] + '-left').attr('style', 'display:none !important');
					$('#' + divIds[i] + '-right').attr('style', 'display:none !important');
				}
			}
		} else {
			$('#mwe-right').attr('style', '');
			$('#mwe-left').attr('style', 'display:none !important');
		}
	}
	if (mw.config.get('wgPageName') === 'Main_Page' || mw.config.get('wgPageName').startsWith("MarioWiki:BJAODN/April_Fool's")) {
		$(mainPageMaintain);
		$(window).resize(function() {
			var timeout = 100;
			if (document.getElementsByClassName('pollspinner')[0])
				timeout = 500;
			clearTimeout(window.resizeFinished);
			window.resizeFinished = setTimeout(function() {
				$(mainPageMaintain);
			}, timeout);
		});
	}
});