MediaWiki:Common.js: Difference between revisions

From Rhythm Game Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Manual revert
 
(41 intermediate revisions by the same user not shown)
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. */


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


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


    const inputGoldPerfect = document.createElement("input");
/* Links without href/links to "javascript:void(0)" are not allowed by MediaWiki, this is a workaround */
     inputGoldPerfect.type = "number";
/* Use Template:EmptyLink to activate this piece of code. */
     inputGoldPerfect.size = 4;
findAndExecute(".rgwiki-main-emptylink", (span) => {
    root.appendChild(inputGoldPerfect);
  const a = span.querySelector("a");
  for (const className of span.classList) {
     if (className !== "rgwiki-main-emptylink") {
      a.classList.add(className);
     }
  }
  a.href = "javascript:void(0)";
  const parent = span.parentNode;
  parent.replaceChild(a, span);
});


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


     const inputGreat = document.createElement("input");
/* OS tabs */
     inputGreat.type = "number";
findAndExecute(".tabber.rgwiki-main-os-tabber", (tabber) => {
     inputGreat.size = 4;
     const osString = (window.navigator.oscpu || window.navigator.platform || window.navigator.userAgent).toLowerCase();
     root.appendChild(inputGreat);
     let userOs = null;
    if (osString.includes("linux")) userOs = "linux";
     if (osString.includes("mac")) userOs = "mac";
     if (osString.includes("win")) userOs = "win";


     const inputGood = document.createElement("input");
     const oldAnchor = window.location.hash;
     inputGood.type = "number";
     tabber.querySelectorAll(".tabber__header > .tabber__tabs > a.tabber__tab").forEach(tab => {
    inputGood.size = 4;
        const tabName = tab.textContent.toLowerCase();
    root.appendChild(inputGood);
        if (userOs === "linux" && tabName.includes("linux")) tab.click();
 
        if (userOs === "mac" && tabName.includes("macos")) tab.click();
    const inputMiss = document.createElement("input");
         if (userOs === "win" && tabName.includes("windows")) tab.click();
    inputMiss.type = "number";
    inputMiss.size = 4;
    root.appendChild(inputMiss);
 
    const calculateBtn = document.createElement("button");
    calculateBtn.textContent = "Calculate";
    root.appendChild(calculateBtn);
 
    const output = document.createElement("p");
    root.appendChild(output);
 
    calculateBtn.addEventListener("click", () => {
        const goldPerfect = Number(inputGoldPerfect.value);
        const anyPerfect = Number(inputAnyPerfect.value);
        const great = Number(inputGreat.value);
        const good = Number(inputGood.value);
         const miss = Number(inputMiss.value);
        const hitNotes = anyPerfect + great + good;
        const totalNotes = anyPerfect + great + good + miss;
        const nonGoldPerfect = anyPerfect - goldPerfect;
        const score = Math.floor((anyPerfect + (great * 0.8) + (good * 0.5)) / totalNotes * 1000000) + (goldPerfect * 0.0001);
        output.textContent = `Score: ${score} - Hit notes: ${hitNotes}/${totalNotes} - Non-gold perfects: ${nonGoldPerfect}`;
     });
     });
    setTimeout(() => {
        window.location.hash = oldAnchor;
    }, 100);
});
});

Latest revision as of 07:52, 11 March 2025

/* 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;
});

/* Links without href/links to "javascript:void(0)" are not allowed by MediaWiki, this is a workaround */
/* Use Template:EmptyLink to activate this piece of code. */
findAndExecute(".rgwiki-main-emptylink", (span) => {
  const a = span.querySelector("a");
  for (const className of span.classList) {
    if (className !== "rgwiki-main-emptylink") {
      a.classList.add(className);
    }
  }
  a.href = "javascript:void(0)";
  const parent = span.parentNode;
  parent.replaceChild(a, span);
});

/* 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}`;
});

/* OS tabs */
findAndExecute(".tabber.rgwiki-main-os-tabber", (tabber) => {
    const osString = (window.navigator.oscpu || window.navigator.platform || window.navigator.userAgent).toLowerCase();
    let userOs = null;
    if (osString.includes("linux")) userOs = "linux";
    if (osString.includes("mac")) userOs = "mac";
    if (osString.includes("win")) userOs = "win";

    const oldAnchor = window.location.hash;
    tabber.querySelectorAll(".tabber__header > .tabber__tabs > a.tabber__tab").forEach(tab => {
        const tabName = tab.textContent.toLowerCase();
        if (userOs === "linux" && tabName.includes("linux")) tab.click();
        if (userOs === "mac" && tabName.includes("macos")) tab.click();
        if (userOs === "win" && tabName.includes("windows")) tab.click();
    });
    setTimeout(() => {
        window.location.hash = oldAnchor;
    }, 100);
});