MediaWiki:Common.js: Difference between revisions
Appearance
Sonja says (talk | contribs) No edit summary |
Sonja says (talk | contribs) No edit summary |
||
| Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
(function () { | (function () { | ||
/* ========================================================= | |||
CONFIG | |||
========================================================= */ | |||
const DISCORD_URL = 'https://discord.gg/t2Rp2dRvze'; | const DISCORD_URL = 'https://discord.gg/t2Rp2dRvze'; | ||
| Line 11: | Line 11: | ||
// Clock timezone (same for everyone) | // Clock timezone (same for everyone) | ||
const CLOCK_TIMEZONE = 'Etc/UTC'; | const CLOCK_TIMEZONE = 'Etc/UTC'; | ||
// Game time anchor (real UTC -> game time) | // Game time anchor (real UTC -> game time) | ||
| Line 23: | Line 23: | ||
const pad2 = (n) => String(n).padStart(2, '0'); | const pad2 = (n) => String(n).padStart(2, '0'); | ||
function ensureDiscordLink( | /* ========================================================= | ||
if (document.getElementById( | Tabs (your existing code) | ||
========================================================= */ | |||
$(document).ready(function () { | |||
$('.wiki-tabs-container').each(function () { | |||
var $container = $(this); | |||
var $firstButton = $container.find('.wiki-tab-button').first(); | |||
var firstTabId = $firstButton.data('tab'); | |||
$firstButton.addClass('active'); | |||
$container.find('#' + firstTabId).addClass('active').show(); | |||
}); | |||
$('.wiki-tab-button').on('click', function () { | |||
var $button = $(this); | |||
var tabId = $button.data('tab'); | |||
var $container = $button.closest('.wiki-tabs-container'); | |||
$container.find('.wiki-tab-button').removeClass('active'); | |||
$container.find('.wiki-tab-pane').removeClass('active').hide(); | |||
$button.addClass('active'); | |||
$container.find('#' + tabId).addClass('active').show(); | |||
}); | |||
}); | |||
/* ========================================================= | |||
Helpers: find/create containers in BOTH headers | |||
========================================================= */ | |||
function ensureDiscordLink(container, id) { | |||
if (!container) return null; | |||
if (document.getElementById(id)) return document.getElementById(id); | |||
const link = document.createElement('a'); | const link = document.createElement('a'); | ||
link.id = | link.id = id; | ||
link.href = DISCORD_URL; | link.href = DISCORD_URL; | ||
link.target = '_blank'; | link.target = '_blank'; | ||
link.className = | link.className = | ||
'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet'; | 'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet sticky-discord-link'; | ||
link.textContent = 'Join Us on Discord!'; | link.textContent = 'Join Us on Discord!'; | ||
link.style.fontWeight = 'bold'; | link.style.fontWeight = 'bold'; | ||
container.prepend(link); | |||
return link; | |||
} | } | ||
// Sticky header: insert our widget bar inside sticky icons row | |||
function getStickyBar() { | |||
/ | |||
function | |||
const icons = document.querySelector('.vector-sticky-header-icons'); | const icons = document.querySelector('.vector-sticky-header-icons'); | ||
if (!icons) return null; | if (!icons) return null; | ||
let | let bar = document.getElementById('sticky-time-widgets'); | ||
if ( | if (bar) return bar; | ||
// | // Keep Discord where it was (far-left in sticky icons) | ||
ensureDiscordLink(icons); | const discord = ensureDiscordLink(icons, 'custom-sticky-link'); | ||
bar = document.createElement('span'); | |||
bar.id = 'sticky-time-widgets'; | |||
bar.className = 'time-widgets'; | |||
// Put widgets immediately AFTER Discord (so Discord stays at the left) | |||
if (discord && discord.parentNode === icons) { | if (discord && discord.parentNode === icons) { | ||
discord.insertAdjacentElement('afterend', | discord.insertAdjacentElement('afterend', bar); | ||
} else { | } else { | ||
icons.prepend( | icons.prepend(bar); | ||
} | } | ||
return | return bar; | ||
} | } | ||
function | // Main header: put our widgets between search and username area | ||
function getMainBar() { | |||
const header = document.querySelector('.vector-header'); | |||
if (!header) return null; | |||
const | // "End" is where user menu lives; we can safely insert at the start of it | ||
const headerEnd = header.querySelector('.vector-header-end'); | |||
if (!headerEnd) return null; | |||
let bar = document.getElementById('main-time-widgets'); | |||
if (bar) return bar; | |||
bar = document.createElement('span'); | |||
bar.id = 'main-time-widgets'; | |||
bar.className = 'time-widgets'; | |||
// Add Discord + widgets inside the same main bar | |||
headerEnd.prepend(bar); | |||
ensureDiscordLink(bar, 'main-discord-link'); | |||
return bar; | |||
} | } | ||
function | /* ========================================================= | ||
if ( | Build widgets inside a bar (no duplicates) | ||
========================================================= */ | |||
function ensureWidget(bar, role, labelText) { | |||
if (!bar) return; | |||
// If this bar already has this role, skip | |||
if (bar.querySelector(`[data-role="${role}"]`)) return; | |||
const wrap = document.createElement('span'); | const wrap = document.createElement('span'); | ||
wrap. | wrap.className = 'header-widget'; | ||
wrap. | wrap.dataset.widget = role; | ||
const label = document.createElement('span'); | if (labelText) { | ||
const label = document.createElement('span'); | |||
label.className = 'header-widget__label'; | |||
label.textContent = labelText; | |||
wrap.appendChild(label); | |||
} | |||
const value = document.createElement('span'); | const value = document.createElement('span'); | ||
value.className = 'header-widget__value'; | value.className = 'header-widget__value'; | ||
value.dataset.role = | value.dataset.role = role; | ||
wrap.appendChild(value); | wrap.appendChild(value); | ||
bar.appendChild(wrap); | |||
} | } | ||
/* ========================================================= | |||
Calculations / formatting | |||
========================================================= */ | |||
function formatRemaining(ms) { | function formatRemaining(ms) { | ||
if (ms <= 0) return '00:00:00'; | if (ms <= 0) return '00:00:00'; | ||
| Line 167: | Line 165: | ||
const hoursPassed = Math.floor((nowMs - GAME_ANCHOR_REAL_UTC) / 3600000); | const hoursPassed = Math.floor((nowMs - GAME_ANCHOR_REAL_UTC) / 3600000); | ||
// Day increments every hour | // Day increments every hour (1..24) | ||
const dayIndex = (GAME_ANCHOR_DAY - 1) + hoursPassed; | const dayIndex = (GAME_ANCHOR_DAY - 1) + hoursPassed; | ||
const dayNumber = ((dayIndex % 24) + 24) % 24 + 1; | const dayNumber = ((dayIndex % 24) + 24) % 24 + 1; | ||
// Month increments every 24 hours | // Month increments every 24 hours (Jan..Jul cycle) | ||
const daysPassed = Math.floor(dayIndex / 24); | const daysPassed = Math.floor(dayIndex / 24); | ||
const totalMonthIndex = GAME_ANCHOR_MONTH_INDEX + daysPassed; | const totalMonthIndex = GAME_ANCHOR_MONTH_INDEX + daysPassed; | ||
const monthIndex = ((totalMonthIndex % 7) + 7) % 7; | const monthIndex = ((totalMonthIndex % 7) + 7) % 7; | ||
| Line 196: | Line 192: | ||
const now = Date.now(); | const now = Date.now(); | ||
// Update ALL widgets in BOTH headers by role | |||
document.querySelectorAll('[data-role="clock"]').forEach((el) => { | |||
el.textContent = clockFmt.format(new Date(now)); | |||
}); | |||
document.querySelectorAll('[data-role="game"]').forEach((el) => { | |||
el.textContent = computeGameString(now); | |||
}); | |||
document.querySelectorAll('[data-role="countdown"]').forEach((el) => { | |||
el.textContent = formatRemaining(COUNTDOWN_TARGET_UTC - now); | |||
}); | |||
} | } | ||
/* ========================================================= | |||
const | Boot: create + update widgets for BOTH headers | ||
if ( | ========================================================= */ | ||
function buildBarsIfPossible() { | |||
const stickyBar = getStickyBar(); | |||
const mainBar = getMainBar(); | |||
// Sticky: no Discord inside bar (Discord is separate link to the left) | |||
if (stickyBar) { | |||
ensureWidget(stickyBar, 'clock', 'UTC:'); | |||
ensureWidget(stickyBar, 'game', null); | |||
ensureWidget(stickyBar, 'countdown', 'Age ends in:'); | |||
} | |||
// Main: Discord lives INSIDE bar (first), then widgets | |||
if (mainBar) { | |||
ensureWidget(mainBar, 'clock', 'UTC:'); | |||
ensureWidget(mainBar, 'game', null); | |||
ensureWidget(mainBar, 'countdown', 'Age ends in:'); | |||
} | |||
updateAll(); | |||
} | |||
// Fires reliably for header DOM | |||
mw.hook('skin.ready').add(function () { | |||
buildBarsIfPossible(); | |||
if (!window.__timeWidgetsStarted) { | |||
if (!window. | window.__timeWidgetsStarted = true; | ||
window. | |||
setInterval(updateAll, 1000); | setInterval(updateAll, 1000); | ||
} | } | ||
}); | |||
// Fires on page navigation/content swaps | |||
mw.hook('wikipage.content').add(function () { | |||
buildBarsIfPossible(); | |||
}); | }); | ||
})(); | })(); | ||
Revision as of 11:55, 19 February 2026
/* Any JavaScript here will be loaded for all users on every page load. */
(function () {
/* =========================================================
CONFIG
========================================================= */
const DISCORD_URL = 'https://discord.gg/t2Rp2dRvze';
// Countdown target: Sat, 18 Apr 2026 00:00 UTC
const COUNTDOWN_TARGET_UTC = Date.parse('2026-04-18T00:00:00Z');
// Clock timezone (same for everyone)
const CLOCK_TIMEZONE = 'Etc/UTC';
// Game time anchor (real UTC -> game time)
// At 2026-02-19 14:00:00 UTC, game time was Jan 21 YR2
const GAME_ANCHOR_REAL_UTC = Date.parse('2026-02-19T14:00:00Z');
const GAME_ANCHOR_MONTH_INDEX = 0; // Jan
const GAME_ANCHOR_DAY = 21; // 1..24
const GAME_ANCHOR_YEAR = 2; // YR2
const GAME_MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'];
const pad2 = (n) => String(n).padStart(2, '0');
/* =========================================================
Tabs (your existing code)
========================================================= */
$(document).ready(function () {
$('.wiki-tabs-container').each(function () {
var $container = $(this);
var $firstButton = $container.find('.wiki-tab-button').first();
var firstTabId = $firstButton.data('tab');
$firstButton.addClass('active');
$container.find('#' + firstTabId).addClass('active').show();
});
$('.wiki-tab-button').on('click', function () {
var $button = $(this);
var tabId = $button.data('tab');
var $container = $button.closest('.wiki-tabs-container');
$container.find('.wiki-tab-button').removeClass('active');
$container.find('.wiki-tab-pane').removeClass('active').hide();
$button.addClass('active');
$container.find('#' + tabId).addClass('active').show();
});
});
/* =========================================================
Helpers: find/create containers in BOTH headers
========================================================= */
function ensureDiscordLink(container, id) {
if (!container) return null;
if (document.getElementById(id)) return document.getElementById(id);
const link = document.createElement('a');
link.id = id;
link.href = DISCORD_URL;
link.target = '_blank';
link.className =
'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet sticky-discord-link';
link.textContent = 'Join Us on Discord!';
link.style.fontWeight = 'bold';
container.prepend(link);
return link;
}
// Sticky header: insert our widget bar inside sticky icons row
function getStickyBar() {
const icons = document.querySelector('.vector-sticky-header-icons');
if (!icons) return null;
let bar = document.getElementById('sticky-time-widgets');
if (bar) return bar;
// Keep Discord where it was (far-left in sticky icons)
const discord = ensureDiscordLink(icons, 'custom-sticky-link');
bar = document.createElement('span');
bar.id = 'sticky-time-widgets';
bar.className = 'time-widgets';
// Put widgets immediately AFTER Discord (so Discord stays at the left)
if (discord && discord.parentNode === icons) {
discord.insertAdjacentElement('afterend', bar);
} else {
icons.prepend(bar);
}
return bar;
}
// Main header: put our widgets between search and username area
function getMainBar() {
const header = document.querySelector('.vector-header');
if (!header) return null;
// "End" is where user menu lives; we can safely insert at the start of it
const headerEnd = header.querySelector('.vector-header-end');
if (!headerEnd) return null;
let bar = document.getElementById('main-time-widgets');
if (bar) return bar;
bar = document.createElement('span');
bar.id = 'main-time-widgets';
bar.className = 'time-widgets';
// Add Discord + widgets inside the same main bar
headerEnd.prepend(bar);
ensureDiscordLink(bar, 'main-discord-link');
return bar;
}
/* =========================================================
Build widgets inside a bar (no duplicates)
========================================================= */
function ensureWidget(bar, role, labelText) {
if (!bar) return;
// If this bar already has this role, skip
if (bar.querySelector(`[data-role="${role}"]`)) return;
const wrap = document.createElement('span');
wrap.className = 'header-widget';
wrap.dataset.widget = role;
if (labelText) {
const label = document.createElement('span');
label.className = 'header-widget__label';
label.textContent = labelText;
wrap.appendChild(label);
}
const value = document.createElement('span');
value.className = 'header-widget__value';
value.dataset.role = role;
wrap.appendChild(value);
bar.appendChild(wrap);
}
/* =========================================================
Calculations / formatting
========================================================= */
function formatRemaining(ms) {
if (ms <= 0) return '00:00:00';
const totalSeconds = Math.floor(ms / 1000);
const days = Math.floor(totalSeconds / 86400);
const hours = Math.floor((totalSeconds % 86400) / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
if (days > 0) return `${days}d ${pad2(hours)}:${pad2(minutes)}:${pad2(seconds)}`;
return `${pad2(hours)}:${pad2(minutes)}:${pad2(seconds)}`;
}
function computeGameString(nowMs) {
const hoursPassed = Math.floor((nowMs - GAME_ANCHOR_REAL_UTC) / 3600000);
// Day increments every hour (1..24)
const dayIndex = (GAME_ANCHOR_DAY - 1) + hoursPassed;
const dayNumber = ((dayIndex % 24) + 24) % 24 + 1;
// Month increments every 24 hours (Jan..Jul cycle)
const daysPassed = Math.floor(dayIndex / 24);
const totalMonthIndex = GAME_ANCHOR_MONTH_INDEX + daysPassed;
const monthIndex = ((totalMonthIndex % 7) + 7) % 7;
// Year increments every 7 real days
const yearsPassed = Math.floor(totalMonthIndex / 7);
const year = GAME_ANCHOR_YEAR + yearsPassed;
return `Current Game Date: ${GAME_MONTHS[monthIndex]} ${dayNumber} YR${year}`;
}
const clockFmt = new Intl.DateTimeFormat('en-GB', {
timeZone: CLOCK_TIMEZONE,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
function updateAll() {
const now = Date.now();
// Update ALL widgets in BOTH headers by role
document.querySelectorAll('[data-role="clock"]').forEach((el) => {
el.textContent = clockFmt.format(new Date(now));
});
document.querySelectorAll('[data-role="game"]').forEach((el) => {
el.textContent = computeGameString(now);
});
document.querySelectorAll('[data-role="countdown"]').forEach((el) => {
el.textContent = formatRemaining(COUNTDOWN_TARGET_UTC - now);
});
}
/* =========================================================
Boot: create + update widgets for BOTH headers
========================================================= */
function buildBarsIfPossible() {
const stickyBar = getStickyBar();
const mainBar = getMainBar();
// Sticky: no Discord inside bar (Discord is separate link to the left)
if (stickyBar) {
ensureWidget(stickyBar, 'clock', 'UTC:');
ensureWidget(stickyBar, 'game', null);
ensureWidget(stickyBar, 'countdown', 'Age ends in:');
}
// Main: Discord lives INSIDE bar (first), then widgets
if (mainBar) {
ensureWidget(mainBar, 'clock', 'UTC:');
ensureWidget(mainBar, 'game', null);
ensureWidget(mainBar, 'countdown', 'Age ends in:');
}
updateAll();
}
// Fires reliably for header DOM
mw.hook('skin.ready').add(function () {
buildBarsIfPossible();
if (!window.__timeWidgetsStarted) {
window.__timeWidgetsStarted = true;
setInterval(updateAll, 1000);
}
});
// Fires on page navigation/content swaps
mw.hook('wikipage.content').add(function () {
buildBarsIfPossible();
});
})();