MediaWiki:Gadget-main-trivia-box.js

Revision as of 18:42, 9 January 2025 by TadeLn (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.
findAndExecute(".rgwiki-main-trivia-box", (div) => {
    function roll() {
        div.querySelectorAll("li.rgwiki-main-trivia-active").forEach(x => { x.classList.remove("rgwiki-main-trivia-active"); x.classList.add("rgwiki-main-trivia-previously-active"); });

        const elements = div.querySelectorAll("li:not(.rgwiki-main-trivia-previously-active");
        if (elements.length !== 0) {
            const rand = Math.floor(Math.random() * elements.length);
            elements[rand].classList.add("rgwiki-main-trivia-active");
        }

        div.querySelectorAll("li.rgwiki-main-trivia-previously-active").forEach(x => { x.classList.remove("rgwiki-main-trivia-previously-active"); });
    }
    const buttons = div.querySelector(".rgwiki-main-trivia-buttons");

    const a = document.createElement("a");
    a.href = "javascript:void(0)";
    a.addEventListener("click", roll);
    a.textContent = "[reroll]";
    buttons.appendChild(a);
    roll();
});