MediaWiki:SmoothiesDebtCalculator.js

From Roat Pkz
Revision as of 10:56, 18 July 2026 by Hefner (talk | contribs)
Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
(function () {

function parseAmount(value) {

    value = value
        .toLowerCase()
        .replace(/,/g,"")
        .trim();

    if (value.endsWith("b")) {
        return Number(value.slice(0,-1)) * 1000000000;
    }

    if (value.endsWith("m")) {
        return Number(value.slice(0,-1)) * 1000000;
    }

    if (value.endsWith("k")) {
        return Number(value.slice(0,-1)) * 1000;
    }

    return Number(value) || 0;
}


function calculateDebt() {

    let pkp =
        parseAmount(document.getElementById("pkp-amount").value);


    let dp =
        parseAmount(document.getElementById("dp-price").value) *
        parseAmount(document.getElementById("dp-amount").value);


    let credits =
        parseAmount(document.getElementById("credits-price").value) *
        parseAmount(document.getElementById("credits-amount").value);


    let total = pkp + dp + credits;


    document.getElementById("total").innerHTML =
        Math.floor(total).toLocaleString() + " PKP";
}



function createCalculator() {

let app = document.getElementById("debt-calculator-app");

if (!app) return;


app.innerHTML = `

<div class="debt-items">


<div class="debt-item">

<img src="/images/PKP.png">

<h3>PKP</h3>

<label>Amount</label>
<input id="pkp-amount" value="0">

</div>



<div class="debt-item">

<img src="/images/DP.png">

<h3>DP</h3>

<label>Trade Price (PKP)</label>
<input id="dp-price" value="0">

<label>Amount</label>
<input id="dp-amount" value="0">

</div>



<div class="debt-item">

<img src="/images/Credits.png">

<h3>Credits</h3>

<label>Trade Price (PKP)</label>
<input id="credits-price" value="0">

<label>Amount</label>
<input id="credits-amount" value="0">

</div>


</div>

`;



document.querySelectorAll("#debt-calculator-app input")
.forEach(function(input){

input.addEventListener("input", calculateDebt);

});


}



mw.hook("wikipage.content").add(function(){

createCalculator();

});


})();