Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
(attempt to only apply on category pages)
m (fix regression)
Line 145: Line 145:
       // Replace the answer element with the calculated answer
       // Replace the answer element with the calculated answer
       var answerElement = document.getElementById('simple-calc__answer');
       var answerElement = document.getElementById('simple-calc__answer');
       answerElement.textContent = 'Answer: ' + answer;
       answerElement.textContent = '' + answer;
     });
     });



Revision as of 05:56, 25 October 2023

/* Any JavaScript here will be loaded for all users on every page load. */

//Show a badge for featured projects
$(document).ready( function () {
if ($.inArray("Featured projects", mw.config.get("wgCategories")) > -1) {
$('.mw-indicators').append($('<div class="mw-ui-icon-featured" title="This is a featured community project on Prism Party"></div>'));
}
});

//Get player counts of Minecraft server
$.getJSON("https://mcapi.us/server/status?ip=play.prismparty.net", function(json) {
   if (json.status !== "error") {
     $(".online").html(json.players.now);
   } else {
     $(".online").html(0);
   }
});

//Display Discord widget
$( '#discord-widget' ).html( '<iframe class="responsive-iframe" src="https://discord.com/widget?id=614592231428587522&theme=dark" allowtransparency="true" frameborder="0" sandbox="allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts"></iframe>' );

//live countdown
$(document).ready( function () {
if ($.inArray("Pages with a countdown", mw.config.get("wgCategories")) > -1) {
var spanDate = document.getElementById("get-time").innerText;
var countDownDate = new Date(spanDate).getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="live-countdown"
    document.getElementById("live-countdown").innerHTML = days + "d " + hours + "h "
    + minutes + "m " + seconds + "s ";
  // If the count down is finished, write some text
  if (distance < 0 ) {
    clearInterval(x);
    document.getElementById("live-countdown").innerHTML = "This event has ended!";
  }
}, 1000);
}});

//Copy element contents on click - can we replace this?
"use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var copy = document.querySelectorAll(".copy");
var _iterator = _createForOfIteratorHelper(copy),
  _step;
try {
  var _loop = function _loop() {
    var copied = _step.value;
    copied.onclick = function () {
      document.execCommand("copy");
    };
    copied.addEventListener("copy", function (event) {
      event.preventDefault();
      if (event.clipboardData) {
        event.clipboardData.setData("text/plain", copied.textContent);
        console.log(event.clipboardData.getData("text"));
      }
      ;
    });
  };
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
    _loop();
  }
} catch (err) {
  _iterator.e(err);
} finally {
  _iterator.f();
}
;

// Simple calculator

$(document).ready(function () {
  // Check if the current page belongs to the desired category
  if ($.inArray("Pages with dynamic calculator", mw.config.get("wgCategories")) > -1) {
    // Create the input elements and replace the corresponding span elements
    for (var index = 1; index <= 3; index++) {
      var spanElement = document.getElementById('simple-calc__rinput' + index);

      // Create the input element
      var inputElement = document.createElement('input');
      inputElement.type = 'text';
      inputElement.placeholder = 'Enter a value';
      inputElement.id = 'simple-calc__input' + index;

      // Replace the span element with the input element
      spanElement.parentNode.replaceChild(inputElement, spanElement);
    }

    // Get the span element to be replaced with the button
    var spanElement = document.getElementById('simple-calc__rcalculate');

    // Create the button element
    var calculateButton = document.createElement('button');
    calculateButton.id = 'calculate-btn';
    calculateButton.textContent = 'Calculate';

    // Add click event listener to the button
    calculateButton.addEventListener('click', function () {
      // Get the input values
      var inputValue1 = parseFloat(document.getElementById('simple-calc__input1').value);
      var inputValue2 = parseFloat(document.getElementById('simple-calc__input2').value);
      var inputValue3 = parseFloat(document.getElementById('simple-calc__input3').value);

      // Perform calculations
      var i = inputValue1;
      var l = inputValue2;
      var j = inputValue3;

      // Calculate k based on the seed value
      var seedValue = parseFloat(document.getElementById('simple-calc__seed').textContent);

      // Output the results
      console.log('i = ' + i);
      console.log('l = ' + l);
      console.log('j = ' + j);
      console.log('k = ' + seedValue);

      // Calculate the answer based on the simplified equation
      var answer = i + (i * (l - seedValue) * 0.01) - ((i + (i * (l - 2) * 0.01)) * (j - 1) * 0.09);

      // Round the answer to two decimal places using toFixed
      answer = answer.toFixed(2);

      // Output the answer
      console.log('Your answer is (' + answer + ')');

      // Replace the answer element with the calculated answer
      var answerElement = document.getElementById('simple-calc__answer');
      answerElement.textContent = '₲' + answer;
    });

    // Replace the span element with the button
    spanElement.parentNode.replaceChild(calculateButton, spanElement);
  }
});