MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 224: | Line 224: | ||
/** | |||
* Quick Search — Navigate directly to wiki page | |||
* Intercepts the Page Forms #forminput inside #crt-quick-search so that | |||
* clicking "Go" (or pressing Enter, or selecting an autocomplete suggestion) | |||
* navigates to the wiki article instead of opening the form editor. | |||
*/ | |||
(function () { | |||
'use strict'; | |||
// Only run on Main Page | |||
if (mw.config.get('wgPageName') !== 'Main_Page') return; | |||
function initQuickSearch() { | |||
var container = document.getElementById('crt-quick-search'); | |||
if (!container) return; | |||
var form = container.querySelector('form'); | |||
if (!form) return; | |||
var input = form.querySelector('input[type="text"]'); | |||
if (!input) return; | |||
/** | |||
* Navigate to the wiki page for whatever is currently in the input. | |||
*/ | |||
function goToPage() { | |||
var value = input.value.trim(); | |||
if (value) { | |||
window.location.href = mw.util.getUrl(value); | |||
} | |||
} | |||
// Intercept normal form submission (Enter key / Go button) | |||
form.addEventListener('submit', function (e) { | |||
e.preventDefault(); | |||
e.stopPropagation(); | |||
goToPage(); | |||
}); | |||
// Page Forms autocomplete fires a 'pfautocomplete' event on the | |||
// input when a suggestion is selected. Listen for it and navigate | |||
// immediately so the user doesn't have to click Go a second time. | |||
$(input).on('pfautocomplete', function () { | |||
// Small delay to let PF finish updating the input value | |||
setTimeout(goToPage, 50); | |||
}); | |||
} | |||
// Run after the DOM is ready (PF may inject its form dynamically) | |||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', initQuickSearch); | |||
} else { | |||
initQuickSearch(); | |||
} | |||
})(); | |||