Jump to content

MediaWiki:Common.js

From Fedoverse Wiki, the free encyclopedia
Revision as of 14:16, 12 September 2026 by Fedorista (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* 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
})();