Jump to content

MediaWiki:Gadget-search-new-tab.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* JSHint-valid */
/* globals $:false */
$(function() {
	// Special:Search for all skins.
	$('#powersearch, #search').on('keydown', function(e) {
		$(this).prop('target', e.ctrlKey || e.metaKey ? '_blank' : '');
	});

	// CodexTypeaheadSearch header search [only on Vector (2022)].
	$('#p-search').on('keydown', '.cdx-typeahead-search #searchform', function(e) {
		if ((e.ctrlKey || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
			var URI = $(this).find('.cdx-menu-item--selected a.cdx-menu-item__content').prop('href');
			if (URI != undefined) {
				window.open(URI, '_blank');
				return false;
			}
		}
	});

	// Header/Side search on other skins [no auto suggest nav on MinervaNeue].
	// Search box [auto suggest on Vector legacy (2010), MonoBook, Timeless].
	$('div:not(.cdx-typeahead-search) #searchform #searchInput, .searchbox .mw-searchInput').on('keydown', function(e) {
		if ((e.ctrlKey || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
			if ($(this).data('suggestionsContext') != undefined) {
				var selectedIndex = $(this).data('suggestionsContext').config.suggestions.indexOf($(this).val());
				if (selectedIndex != -1) {
					var URI = $('.suggestions-results a:nth-child(' + (selectedIndex + 1) + ')').prop('href');
					if (URI != undefined) {
						window.open(URI, '_blank');
						$(this).trigger('blur');
					}
				}
			}
		}
	});
});