MediaWiki:Common.js: Difference between revisions
Appearance
Created page with "→Any JavaScript here will be loaded for all users on every page load.: document.addEventListener("DOMContentLoaded", () => { const table = document.querySelector(".wikitable.sortable"); if (!table) return; const tbody = table.querySelector("tbody"); if (!tbody) return; const rows = Array.from(tbody.querySelectorAll("tr")).slice(1); // skip header // Fisher–Yates shuffle for (let i = rows.length - 1; i > 0; i--) { const j = Math.floor(Math.rando..." |
No edit summary |
||
| Line 17: | Line 17: | ||
// Re-append in shuffled order | // Re-append in shuffled order | ||
rows.forEach(row => tbody.appendChild(row)); | rows.forEach(row => tbody.appendChild(row)); | ||
}); | |||
document.addEventListener("DOMContentLoaded", () => { | |||
const table = document.querySelector(".wikitable.sortable"); | |||
if (!table) return; | |||
const tbody = table.querySelector("tbody"); | |||
const rows = Array.from(tbody.querySelectorAll("tr")).slice(1); | |||
// Create button | |||
const button = document.createElement("button"); | |||
button.textContent = "🎲 Pick Random Game"; | |||
button.style.marginBottom = "10px"; | |||
button.style.padding = "8px 12px"; | |||
button.style.cursor = "pointer"; | |||
table.parentNode.insertBefore(button, table); | |||
button.addEventListener("click", () => { | |||
// Clear previous highlights | |||
rows.forEach(r => r.style.background = ""); | |||
const randomRow = rows[Math.floor(Math.random() * rows.length)]; | |||
// Highlight it | |||
randomRow.style.background = "#d4edda"; | |||
// Scroll into view | |||
randomRow.scrollIntoView({ behavior: "smooth", block: "center" }); | |||
}); | |||
}); | }); | ||
Revision as of 17:42, 30 March 2026
/* Any JavaScript here will be loaded for all users on every page load. */
document.addEventListener("DOMContentLoaded", () => {
const table = document.querySelector(".wikitable.sortable");
if (!table) return;
const tbody = table.querySelector("tbody");
if (!tbody) return;
const rows = Array.from(tbody.querySelectorAll("tr")).slice(1); // skip header
// Fisher–Yates shuffle
for (let i = rows.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[rows[i], rows[j]] = [rows[j], rows[i]];
}
// Re-append in shuffled order
rows.forEach(row => tbody.appendChild(row));
});
document.addEventListener("DOMContentLoaded", () => {
const table = document.querySelector(".wikitable.sortable");
if (!table) return;
const tbody = table.querySelector("tbody");
const rows = Array.from(tbody.querySelectorAll("tr")).slice(1);
// Create button
const button = document.createElement("button");
button.textContent = "🎲 Pick Random Game";
button.style.marginBottom = "10px";
button.style.padding = "8px 12px";
button.style.cursor = "pointer";
table.parentNode.insertBefore(button, table);
button.addEventListener("click", () => {
// Clear previous highlights
rows.forEach(r => r.style.background = "");
const randomRow = rows[Math.floor(Math.random() * rows.length)];
// Highlight it
randomRow.style.background = "#d4edda";
// Scroll into view
randomRow.scrollIntoView({ behavior: "smooth", block: "center" });
});
});