MediaWiki:Common.js

Revision as of 18:59, 9 January 2025 by TadeLn (talk | contribs) (Move Gadvia score calculator to a gadget)

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. */

/* Finds elements by a selector and executes a function for each element */
function findAndExecute(selector, callback) {
    const elements = document.querySelectorAll(selector);
    elements.forEach((element) => {
        try {
            callback(element);
        } catch (e) {
            console.error(`Error while initializing "${selector}"`, e);
        }
    });
}

/* tabindex="-1" is not allowed by MediaWiki, this is a workaround */
findAndExecute(".rgwiki-main-negative-tabindex", (element) => {
    element.tabIndex = -1;
});

/* Localized date */
findAndExecute(".rgwiki-main-date-localized", (span) => {
    const inputDateString = span.dataset.datetime !== undefined ? span.dataset.datetime : span.textContent;
    const date = new Date(inputDateString);
    const outputDateString = date.toString();
    span.title = `This date in your timezone: ${outputDateString}`;
});


/* Rizline clickable bio chips */
findAndExecute(".rgwiki-rizline-bio-chip-focusable", (chip) => {
    chip.addEventListener("click", () => {
        const isLeft = chip.classList.contains("rgwiki-rizline-bio-chip-left");
        const isRight = chip.classList.contains("rgwiki-rizline-bio-chip-right");
        if (isLeft) {
            document.querySelectorAll(".rgwiki-rizline-bio-chip-focusable.rgwiki-rizline-bio-chip-left.rgwiki-rizline-bio-chip-clicked").forEach((x) => {
                x.classList.remove("rgwiki-rizline-bio-chip-clicked");
            });
        }
        if (isRight) {
            document.querySelectorAll(".rgwiki-rizline-bio-chip-focusable.rgwiki-rizline-bio-chip-right.rgwiki-rizline-bio-chip-clicked").forEach((x) => {
                x.classList.remove("rgwiki-rizline-bio-chip-clicked");
            });
        }
        chip.classList.add("rgwiki-rizline-bio-chip-clicked");
    });
});