Tip of the day
Want to find something particular about the game and don't mind spoilers? Check the Wiki!

MediaWiki:Gadget-HideTranslatedSearchResults.js

From Walkscape Walkthrough
Revision as of 10:19, 14 March 2026 by Bonez565 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
(function () {
	'use strict';

	// Matches translated subpages like:
	// /en
	// /fr
	// /it
	// /pt-br
	// /zh-hans
	//
	// Does not usually match normal content subpages like /Mining or /Source
	var translatedSuffixRegex = /\/[a-z]{2,3}(?:-[a-z0-9]{2,8})?$/i;

	function isTranslatedTitle(title) {
		title = (title || '').trim();
		return translatedSuffixRegex.test(title);
	}

	function getSearchResultTitle($result) {
		var $headingLink = $result.find('.mw-search-result-heading a').first();

		if ($headingLink.length) {
			return ($headingLink.attr('title') || $headingLink.text() || '').trim();
		}

		return '';
	}

	function filterSpecialSearchResults() {
		var hiddenCount = 0;

		$('.mw-search-result').each(function () {
			var $result = $(this);
			var title = getSearchResultTitle($result);

			if (!title) {
				return;
			}

			if (isTranslatedTitle(title)) {
				$result.hide();
				hiddenCount++;
			} else {
				$result.show();
			}
		});

		updateSearchNotice(hiddenCount);
	}

	function updateSearchNotice(hiddenCount) {
		var noticeId = 'translated-subpages-filter-notice';
		var $existing = $('#' + noticeId);

		if (!hiddenCount) {
			$existing.remove();
			return;
		}

		var message =
			hiddenCount +
			' translated subpage' +
			(hiddenCount === 1 ? '' : 's') +
			' hidden from search results.';

		if ($existing.length) {
			$existing.text(message);
			return;
		}

		$('<div>')
			.attr('id', noticeId)
			.css({
				margin: '0 0 1em 0',
				padding: '0.75em 1em',
				border: '1px solid #c8ccd1',
				background: '#f8f9fa'
			})
			.text(message)
			.insertBefore('.searchresults, .mw-search-results');
	}

	function filterSuggestionContainer(container) {
		var $container = $(container);
		var hiddenAny = false;

		// Common links/items used in MediaWiki search suggestion UIs
		$container.find('a, li, .suggestions-result, .oo-ui-optionWidget').each(function () {
			var $item = $(this);
			var title = '';

			// Try multiple ways to get the suggestion title
			title =
				$item.attr('title') ||
				$item.attr('data-title') ||
				$item.attr('data-value') ||
				$item.find('.suggestions-result-title').text() ||
				$item.find('.oo-ui-labelElement-label').text() ||
				$item.text() ||
				'';

			title = title.trim();

			if (!title) {
				return;
			}

			if (isTranslatedTitle(title)) {
				$item.hide();
				hiddenAny = true;
			} else {
				$item.show();
			}
		});

		// Hide empty wrappers if all visible children are gone
		if (hiddenAny) {
			$container.find('ul, .suggestions-results, .oo-ui-selectWidget').each(function () {
				var $wrapper = $(this);
				var hasVisibleChildren = $wrapper.children(':visible').length > 0;
				$wrapper.toggle(hasVisibleChildren);
			});
		}
	}

	function scanAndFilterSuggestionPopups() {
		$('.suggestions, .suggestions-results, .oo-ui-popupWidget-popup, .oo-ui-menuSelectWidget')
			.each(function () {
				filterSuggestionContainer(this);
			});
	}

	function observeSearchResults() {
		var target = document.querySelector('.mw-search-results') ||
		             document.querySelector('.searchresults');

		if (!target || !window.MutationObserver) {
			return;
		}

		var observer = new MutationObserver(function () {
			filterSpecialSearchResults();
		});

		observer.observe(target, {
			childList: true,
			subtree: true
		});
	}

	function observeSuggestions() {
		if (!window.MutationObserver) {
			return;
		}

		var observer = new MutationObserver(function (mutations) {
			mutations.forEach(function (mutation) {
				if (mutation.type === 'childList') {
					// Re-scan globally because suggestion widgets are often inserted dynamically
					scanAndFilterSuggestionPopups();
				}
			});
		});

		observer.observe(document.body, {
			childList: true,
			subtree: true
		});
	}

	function bindSearchInputEvents() {
		var $inputs = $('#searchInput, #simpleSearch input[name="search"], .mw-searchInput');

		$inputs.on('input keyup focus', function () {
			setTimeout(scanAndFilterSuggestionPopups, 50);
			setTimeout(scanAndFilterSuggestionPopups, 150);
		});
	}

	$(function () {
		// Filter full search results if on Special:Search
		if (mw.config.get('wgCanonicalSpecialPageName') === 'Search') {
			filterSpecialSearchResults();
			observeSearchResults();
		}

		// Filter autocomplete suggestions everywhere
		bindSearchInputEvents();
		scanAndFilterSuggestionPopups();
		observeSuggestions();
	});
}());