MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 228: | Line 228: | ||
/** | /** | ||
* Quick Search — Navigate directly to wiki page | * Quick Search — Navigate directly to wiki page | ||
* Intercepts the Page Forms #forminput inside #crt-quick-search so that | * Intercepts the Page Forms #forminput inside #crt-quick-search so that | ||
* clicking "Go" (or pressing Enter, or selecting an autocomplete suggestion) | * clicking "Go" (or pressing Enter, or selecting an autocomplete suggestion) | ||
* navigates to the wiki article instead of opening the form editor. | * navigates to the wiki article instead of opening the form editor. | ||
* | |||
* Note: Page Forms creates the actual <input> element dynamically inside a | |||
* .pfFormInputWrapper div, so we must wait for it to appear before attaching | |||
* listeners. | |||
*/ | */ | ||
(function () { | (function () { | ||
'use strict'; | 'use strict'; | ||
if (mw.config.get('wgPageName') !== 'Main_Page') return; | |||
function goToPage(input) { | |||
var value = input.value.trim(); | |||
if (value) { | |||
window.location.href = mw.util.getUrl(value); | |||
} | |||
} | } | ||
function | function setup(container, form) { | ||
// The actual text input created by Page Forms inside .pfFormInputWrapper | |||
var input = container.querySelector('.pfFormInputWrapper input[type="text"], .pfFormInputWrapper input:not([type="hidden"])'); | |||
if (!input) return false; | |||
// Intercept form submission (Enter key / Go button) | |||
form.addEventListener('submit', function (e) { | |||
e.preventDefault(); | |||
e.stopPropagation(); | |||
goToPage(input); | |||
}); | |||
// Navigate immediately when an autocomplete suggestion is selected | |||
$(input).on('pfautocomplete', function () { | |||
setTimeout(function () { goToPage(input); }, 50); | |||
}); | |||
return true; | |||
} | |||
function init() { | |||
var container = document.getElementById('crt-quick-search'); | var container = document.getElementById('crt-quick-search'); | ||
if (!container) return; | if (!container) return; | ||
var form = container.querySelector('form'); | var form = container.querySelector('form'); | ||
if (!form) return; | |||
if (!form) | |||
// Try immediately — if PF has already initialized, we're done | |||
if (setup(container, form)) return; | |||
if ( | |||
// Otherwise watch for PF to inject the <input> dynamically | |||
var observer = new MutationObserver(function (mutations, obs) { | |||
if (setup(container, form)) { | |||
obs.disconnect(); | |||
} | } | ||
}); | }); | ||
observer.observe(container, { childList: true, subtree: true }); | |||
// Safety timeout: stop observing after 10s | |||
setTimeout(function () { observer.disconnect(); }, 10000); | |||
} | } | ||
if (document.readyState === 'loading') { | if (document.readyState === 'loading') { | ||
document.addEventListener('DOMContentLoaded', init); | |||
document.addEventListener('DOMContentLoaded', | |||
} else { | } else { | ||
init(); | |||
} | } | ||
})(); | })(); | ||