MediaWiki:Common.js
From The Undead Archive
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. */
(function () {
function weightedChoose(container) {
const options = [...container.querySelectorAll(".gadget-choose-option")];
const weights = options.map(el => parseFloat(el.dataset.weight ?? 1));
const total = weights.reduce((sum, w) => sum + w, 0);
let random = Math.random() * total;
const chosen = options.find((_el, i) => (random -= weights[i]) < 0);
options.forEach(el => el.style.display = "none");
chosen.style.display = "";
}
document.querySelectorAll(".gadget-choose").forEach(weightedChoose);
})();
