Jump to content

MediaWiki:Common.js: Difference between revisions

From Fedoverse Wiki, the free encyclopedia
No edit summary
No edit summary
Line 33: Line 33:
});
});


/* Open the first search suggestion on Enter press */
/* Open top search autocomplete result on Enter in Vector 2022 */
mw.hook('wikipage.content').add(function () {
(function () {
     const searchInputs = document.querySelectorAll('input.cdx-text-input__input, input.searchform__input, input#searchInput');
     window.addEventListener('keydown', function (e) {
        if (e.key !== 'Enter') return;


    searchInputs.forEach(function (input) {
        const activeEl = document.activeElement;
         // Prevent binding duplicate event listeners
         // Check if user is typing in a search box
         if (input.dataset.quickEnterBound) return;
         if (!activeEl || !activeEl.matches('input[type="search"], .cdx-text-input__input')) return;
        input.dataset.quickEnterBound = 'true';


         input.addEventListener('keydown', function (e) {
         // 1. Locate the first visible suggestion item in Codex / Vector 2022
            if (e.key === 'Enter') {
        const firstMenuItem = document.querySelector(
                // Look for the first suggestion link rendered in Vector 2022 / Codex
            '.cdx-typeahead-search__menu .cdx-menu-item:not(.cdx-menu-item--disabled), ' +
                const firstResult = document.querySelector(
            '.cdx-menu__listbox .cdx-menu-item:not(.cdx-menu-item--disabled)'
                    '.cdx-menu-item--enabled a, .cdx-typeahead-search__menu a, .suggestions-results a, .oo-ui-menuOptionWidget:not(.oo-ui-widget-disabled) a'
        );
                );


                if (firstResult && firstResult.getAttribute('href')) {
        if (!firstMenuItem) return;
                    e.preventDefault();
 
                    e.stopImmediatePropagation();
        // 2. Extract destination URL: either from an anchor or from its data/ID/query attributes
                    window.location.href = firstResult.getAttribute('href');
        let targetUrl = null;
                }
        const anchor = firstMenuItem.querySelector('a') || (firstMenuItem.tagName === 'A' ? firstMenuItem : null);
 
        if (anchor && anchor.getAttribute('href')) {
            targetUrl = anchor.getAttribute('href');
        } else {
            // Codex frequently stores the target title in the element's text or label
            const labelEl = firstMenuItem.querySelector('.cdx-search-result-title, .cdx-menu-item__text, .cdx-menu-item__content');
            const title = (labelEl || firstMenuItem).textContent.trim();
            if (title) {
                targetUrl = mw.util.getUrl(title);
             }
             }
         }, true); // Use capture to intercept before the default form submission fires
         }
    });
 
});
        // 3. Prevent standard search submission and redirect immediately
        if (targetUrl) {
            e.preventDefault();
            e.stopPropagation();
            e.stopImmediatePropagation();
            window.location.href = targetUrl;
        }
    }, true); // 'true' captures the event before Vue or native form submit triggers
})();

Revision as of 14:16, 12 September 2026

/* Any JavaScript here will be loaded for all users on every page load. */

mw.hook('wikipage.content').add(function () {
    const logo = document.querySelector('a.mw-logo');
    if (logo) {
        logo.setAttribute('href', 'https://fedoverse.com');
    }
});

mw.hook('wikipage.content').add(function () {
    const button = document.querySelector('.your-button-class');
    if (!button) return;

    const premiumEase = 'cubic-bezier(0.23, 1, 0.32, 1)';

    button.addEventListener('mouseenter', () => {
        button.style.transition = `transform 0.15s ${premiumEase}`;
    });

    button.addEventListener('mousemove', (e) => {
        const rect = button.getBoundingClientRect();
        const x = (e.clientX - rect.left - rect.width / 2) / (rect.width / 2);
        const y = (e.clientY - rect.top - rect.height / 2) / (rect.height / 2);
        button.style.transform = `perspective(400px) scale(1.05) rotateX(${y * -15}deg) rotateY(${x * 15}deg)`;
    });

    button.addEventListener('mouseleave', () => {
        button.style.transition = `transform 0.65s ${premiumEase}`;
        // Force the browser to commit the 0.65s transition before updating the transform
        void button.offsetHeight;
        button.style.transform = 'perspective(400px) scale(1) rotateX(0deg) rotateY(0deg)';
    });
});

/* Open top search autocomplete result on Enter in Vector 2022 */
(function () {
    window.addEventListener('keydown', function (e) {
        if (e.key !== 'Enter') return;

        const activeEl = document.activeElement;
        // Check if user is typing in a search box
        if (!activeEl || !activeEl.matches('input[type="search"], .cdx-text-input__input')) return;

        // 1. Locate the first visible suggestion item in Codex / Vector 2022
        const firstMenuItem = document.querySelector(
            '.cdx-typeahead-search__menu .cdx-menu-item:not(.cdx-menu-item--disabled), ' +
            '.cdx-menu__listbox .cdx-menu-item:not(.cdx-menu-item--disabled)'
        );

        if (!firstMenuItem) return;

        // 2. Extract destination URL: either from an anchor or from its data/ID/query attributes
        let targetUrl = null;
        const anchor = firstMenuItem.querySelector('a') || (firstMenuItem.tagName === 'A' ? firstMenuItem : null);

        if (anchor && anchor.getAttribute('href')) {
            targetUrl = anchor.getAttribute('href');
        } else {
            // Codex frequently stores the target title in the element's text or label
            const labelEl = firstMenuItem.querySelector('.cdx-search-result-title, .cdx-menu-item__text, .cdx-menu-item__content');
            const title = (labelEl || firstMenuItem).textContent.trim();
            if (title) {
                targetUrl = mw.util.getUrl(title);
            }
        }

        // 3. Prevent standard search submission and redirect immediately
        if (targetUrl) {
            e.preventDefault();
            e.stopPropagation();
            e.stopImmediatePropagation();
            window.location.href = targetUrl;
        }
    }, true); // 'true' captures the event before Vue or native form submit triggers
})();