Jump to content

MediaWiki:Common.js

From Fedoverse Wiki, the free encyclopedia
Revision as of 15:56, 28 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
})();

(function () {
  var box = document.getElementById('mc-status');
  if (!box) return;
  var host = box.dataset.host;
  var q = function (s) { return box.querySelector(s); };
  var logo = q('.mc-icon'), hint = q('.mc-hint');
  var customLogo = !!logo.firstElementChild;   // [[File:...]] placed in the div wins
  var lastSrc = '';

  function copy() {
    var done = function () {
      hint.textContent = 'copied!';
      setTimeout(function () { hint.textContent = 'click to copy'; }, 2000);
    };
    if (navigator.clipboard) navigator.clipboard.writeText(host).then(done);
  }
  q('.mc-main').addEventListener('click', copy);
  q('.mc-main').addEventListener('keydown', function (e) { if (e.key === 'Enter') copy(); });

  function setIcon(src) {
    if (customLogo || src === lastSrc) return;
    lastSrc = src;
    logo.textContent = '';
    var img = document.createElement('img');
    img.alt = '';
    img.onerror = function () { logo.textContent = '⛏️'; };
    img.src = src;
    logo.appendChild(img);
  }

  function load() {
    fetch('https://api.mcsrvstat.us/3/' + encodeURIComponent(host))
      .then(function (r) { return r.json(); })
      .then(function (d) {
        box.classList.toggle('is-online', !!d.online);
        box.classList.toggle('is-offline', !d.online);
        q('.mc-state').textContent = d.online ? 'Online' : 'Offline';
        q('.mc-players').textContent = d.players ? d.players.online + ' / ' + d.players.max : '– / –';
        q('.mc-players').title = d.players && d.players.list
          ? d.players.list.map(function (p) { return p.name; }).join(', ') : '';
        q('.mc-motd').textContent = d.online && d.motd ? d.motd.clean.join(' ').trim() : '';
        setIcon(d.icon || 'https://api.mcsrvstat.us/icon/' + host);
      })
      .catch(function () { q('.mc-state').textContent = 'Status unavailable'; });
  }
  load();
  setInterval(load, 60000);
})();