Tip of the day
Collectibles you find give 1 extra achievement point per item!

MediaWiki:Gadget-HideTranslatedSearchResults.js: Difference between revisions

From Walkscape Walkthrough
Created page with "(function () { 'use strict'; // Only run on Special:Search if (mw.config.get('wgCanonicalSpecialPageName') !== 'Search') { return; } // Match translated subpages like: // Page/en // Page/fr // Page/it // Page/pt-br // Page/zh-hans // // Does NOT match normal subpages like: // Guide/Mining // Item/Source // // Language code rule here: // /xx // /xxx // /xx-yyy // /xxx-yyyy var translatedSuffixRegex = /\/[a-z]{2,3}(?:-[a-z0-9]{2,8})?$/i;..."
 
mNo edit summary
 
Line 2: Line 2:
'use strict';
'use strict';


// Only run on Special:Search
// Matches translated subpages like:
if (mw.config.get('wgCanonicalSpecialPageName') !== 'Search') {
// /en
return;
// /fr
}
// /it
 
// /pt-br
// Match translated subpages like:
// /zh-hans
// Page/en
// Page/fr
// Page/it
// Page/pt-br
// Page/zh-hans
//
//
// Does NOT match normal subpages like:
// Does not usually match normal content subpages like /Mining or /Source
// Guide/Mining
// Item/Source
//
// Language code rule here:
//  /xx
//  /xxx
//  /xx-yyy
//  /xxx-yyyy
var translatedSuffixRegex = /\/[a-z]{2,3}(?:-[a-z0-9]{2,8})?$/i;
var translatedSuffixRegex = /\/[a-z]{2,3}(?:-[a-z0-9]{2,8})?$/i;


function getResultTitle($result) {
function isTranslatedTitle(title) {
// Standard search result title link
title = (title || '').trim();
return translatedSuffixRegex.test(title);
}
 
function getSearchResultTitle($result) {
var $headingLink = $result.find('.mw-search-result-heading a').first();
var $headingLink = $result.find('.mw-search-result-heading a').first();


Line 36: Line 27:
}
}


function filterSearchResults() {
function filterSpecialSearchResults() {
var hiddenCount = 0;
var hiddenCount = 0;


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


if (!title) {
if (!title) {
Line 47: Line 38:
}
}


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


updateNotice(hiddenCount);
updateSearchNotice(hiddenCount);
}
}


function updateNotice(hiddenCount) {
function updateSearchNotice(hiddenCount) {
var noticeId = 'translated-subpages-filter-notice';
var noticeId = 'translated-subpages-filter-notice';
var $existing = $('#' + noticeId);
var $existing = $('#' + noticeId);
Line 88: Line 81:
}
}


function observeChanges() {
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') ||
var target = document.querySelector('.mw-search-results') ||
            document.querySelector('.searchresults');
            document.querySelector('.searchresults');
Line 97: Line 140:


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


Line 103: Line 146:
childList: true,
childList: true,
subtree: 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 () {
$(function () {
filterSearchResults();
// Filter full search results if on Special:Search
observeChanges();
if (mw.config.get('wgCanonicalSpecialPageName') === 'Search') {
filterSpecialSearchResults();
observeSearchResults();
}
 
// Filter autocomplete suggestions everywhere
bindSearchInputEvents();
scanAndFilterSuggestionPopups();
observeSuggestions();
});
});
}());
}());

Latest revision as of 10:19, 14 March 2026

(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();
	});
}());