MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 80: | Line 80: | ||
var host = box.dataset.host; | var host = box.dataset.host; | ||
var q = function (s) { return box.querySelector(s); }; | var q = function (s) { return box.querySelector(s); }; | ||
var logo = q('.mc-icon | var logo = q('.mc-icon'); | ||
var customLogo = !!logo.firstElementChild; | var customLogo = !!logo.firstElementChild; | ||
var lastSrc = ''; | var lastSrc = ''; | ||
function copy() { | function copy() { if (navigator.clipboard) navigator.clipboard.writeText(host); } | ||
q('.mc-main').addEventListener('click', copy); | q('.mc-main').addEventListener('click', copy); | ||
q('.mc-main').addEventListener('keydown', function (e) { if (e.key === 'Enter') copy(); }); | q('.mc-main').addEventListener('keydown', function (e) { if (e.key === 'Enter') copy(); }); | ||
Revision as of 16:00, 28 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
})();
(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');
var customLogo = !!logo.firstElementChild;
var lastSrc = '';
function copy() { if (navigator.clipboard) navigator.clipboard.writeText(host); }
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);
})();