Jump to content

MediaWiki:Common.js: Difference between revisions

From Adaris
No edit summary
Tag: Reverted
No edit summary
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
// -------------------------
// Infobox cleanup
// -------------------------
$(document).ready(function() {
$(document).ready(function() {
     $('.infoboxTable th').each(function() {
     $('.infoboxTable th').each(function() {
         if ($(this).text().trim() === "Image") {
         if ($(this).text().trim() === "Image") {
             $(this).text(""); // Removes the text but keeps the <th> element
             $(this).text("");
         }
         }
     });
     });
});
});


// -------------------------
// Remove unwanted Introduction headers
// -------------------------
$(document).ready(function() {
$(document).ready(function() {
     $('h2').each(function() {
     $('h2').each(function() {
         if ($(this).text().trim() === "Introduction") {
         if ($(this).text().trim() === "Introduction") {
             $(this).remove(); // Removes the Introduction header
             $(this).remove();
         }
         }
     });
     });
});
});


// -------------------------
// Remove first empty paragraph
// -------------------------
$(document).ready(function() {
$(document).ready(function() {
    // Remove first empty paragraph (<p><br></p>) if it exists
     $('p').first().each(function() {
     $('p').first().each(function() {
         if ($(this).html().trim() === "<br>") {
         if ($(this).html().trim() === "<br>") {
Line 24: Line 32:
});
});


mw.loader.using(['jquery'], function() {
 
     // Listen for new nodes being added to the document
mw.loader.load('https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@main/dist/en/v7.0.0/legacy/ol.js');
     $(document).on('DOMNodeInserted', function(e) {
mw.loader.load('/maps/elemara/elemara-map.js');
        // Check if the inserted node is a Leaflet popup content element.
 
        if ($(e.target).hasClass('leaflet-popup-content')) {
mw.loader.using([], function () {
            processPopup($(e.target));
  $(function () {
         }
     var container = document.getElementById("pixel-area-calculator");
 
    if (!container) {
      return;
    }
 
    container.innerHTML =
      '<div class="pixel-area-tool">' +
        '<h3>Pixel to Square Miles Calculator</h3>' +
 
        '<label>Width in pixels:' +
          '<input id="pixel-width" type="number" min="0" step="any" placeholder="Width">' +
        '</label>' +
 
        '<label>Height in pixels:' +
          '<input id="pixel-height" type="number" min="0" step="any" placeholder="Height">' +
        '</label>' +
 
        '<button id="pixel-area-button" type="button">Calculate</button>' +
 
        '<div id="pixel-area-result"></div>' +
      '</div>';
 
     document.getElementById("pixel-area-button").addEventListener("click", function () {
      var widthPixels = parseFloat(document.getElementById("pixel-width").value);
      var heightPixels = parseFloat(document.getElementById("pixel-height").value);
      var result = document.getElementById("pixel-area-result");
 
      if (isNaN(widthPixels) || isNaN(heightPixels)) {
        result.textContent = "Please enter both pixel values.";
        return;
      }
 
      var widthMiles = (widthPixels / 1000) * 50;
      var heightMiles = (heightPixels / 1000) * 50;
      var squareMiles = widthMiles * heightMiles;
 
      result.innerHTML =
        "<strong>" + widthMiles.toLocaleString(undefined, { maximumFractionDigits: 0 }) + " miles</strong>" +
        " × " +
        "<strong>" + heightMiles.toLocaleString(undefined, { maximumFractionDigits: 0 }) + " miles</strong>" +
        " = " +
         "<strong>" + squareMiles.toLocaleString(undefined, { maximumFractionDigits: 0 }) + " mi²</strong>";
     });
     });
   
  });
    function processPopup($popup) {
});
        var content = $popup.html();
 
         // Replace all instances of Wiki-link syntax [[Page Name]] with an HTML link.
(function () {
        var newContent = content.replace(/\[\[([^\]]+)\]\]/g, function(match, p1) {
    function stripHeaderLines(text) {
             // Convert spaces to underscores (as is common in wiki URLs)
         return text
             var pageName = p1.replace(/ /g, '_');
            // Remove standalone short lines that look like section headers
             return '<a href="/' + encodeURIComponent(pageName) + '" target="_blank">' + p1 + '</a>';
            .replace(/(?:^|\n)\s*[A-Z][A-Za-z0-9'’\- ,()]{0,50}\s*(?=\n)/g, '\n')
        });
             // Clean up excess blank lines
         $popup.html(newContent);
             .replace(/\n{2,}/g, '\n')
       
             .trim();
        // Additionally, if the title is in a <strong> tag, convert it to a clickable link.
    }
         $popup.find('strong').each(function() {
 
            var titleText = $(this).text();
    function cleanAdarisPopups() {
            // If it’s not already a link, wrap it in an anchor.
         document.querySelectorAll(
             if ($(this).find('a').length === 0) {
            '.mwe-popups-extract, .mwe-popups-extract p, .mwe-popups .extract, .mwe-popups .mwe-popups-extract'
                var link = $('<a>')
         ).forEach(function (el) {
                    .attr('href', '/wiki/' + encodeURIComponent(titleText.replace(/ /g, '_')))
             if (!el || el.dataset.adarisCleaned === '1') {
                    .attr('target', '_blank')
                 return;
                    .text(titleText);
                 $(this).html(link);
             }
             }
            el.textContent = stripHeaderLines(el.textContent);
            el.dataset.adarisCleaned = '1';
         });
         });
     }
     }
    const observer = new MutationObserver(function () {
        cleanAdarisPopups();
        setTimeout(cleanAdarisPopups, 100);
        setTimeout(cleanAdarisPopups, 300);
    });
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        characterData: true
    });
    cleanAdarisPopups();
})();
mw.loader.using('mediawiki.util', function () {
  $(function () {
    var input = document.getElementById('pixelToMileInput');
    var output = document.getElementById('pixelToMileOutput');
    if (!input || !output) {
      return;
    }
    input.addEventListener('input', function () {
      var pixels = parseFloat(input.value);
      if (isNaN(pixels)) {
        output.textContent = '0 mi';
        return;
      }
      var miles = (pixels / 1000) * 50;
      output.textContent = Math.round(miles) + ' mi';
    });
  });
});
$(function () {
  var container = document.getElementById('pixel-to-mile-calculator');
  if (!container) {
    return;
  }
  container.className = 'adaris-tool-box';
  container.innerHTML =
    '<label for="pixelToMileInput"><strong>Pixels:</strong></label><br>' +
    '<input id="pixelToMileInput" type="number" placeholder="Enter pixels">' +
    '<p><strong>Distance:</strong> <span id="pixelToMileOutput">0 mi</span></p>';
  var input = document.getElementById('pixelToMileInput');
  var output = document.getElementById('pixelToMileOutput');
  input.addEventListener('input', function () {
    var pixels = parseFloat(input.value);
    if (isNaN(pixels)) {
      output.textContent = '0 mi';
      return;
    }
    var miles = (pixels / 1000) * 50;
    output.textContent = Math.round(miles) + ' mi';
  });
});
});

Latest revision as of 18:40, 7 May 2026

// -------------------------
// Infobox cleanup
// -------------------------
$(document).ready(function() {
    $('.infoboxTable th').each(function() {
        if ($(this).text().trim() === "Image") {
            $(this).text("");
        }
    });
});

// -------------------------
// Remove unwanted Introduction headers
// -------------------------
$(document).ready(function() {
    $('h2').each(function() {
        if ($(this).text().trim() === "Introduction") {
            $(this).remove();
        }
    });
});

// -------------------------
// Remove first empty paragraph
// -------------------------
$(document).ready(function() {
    $('p').first().each(function() {
        if ($(this).html().trim() === "<br>") {
            $(this).remove();
        }
    });
});


mw.loader.load('https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@main/dist/en/v7.0.0/legacy/ol.js');
mw.loader.load('/maps/elemara/elemara-map.js');

mw.loader.using([], function () {
  $(function () {
    var container = document.getElementById("pixel-area-calculator");

    if (!container) {
      return;
    }

    container.innerHTML =
      '<div class="pixel-area-tool">' +
        '<h3>Pixel to Square Miles Calculator</h3>' +

        '<label>Width in pixels:' +
          '<input id="pixel-width" type="number" min="0" step="any" placeholder="Width">' +
        '</label>' +

        '<label>Height in pixels:' +
          '<input id="pixel-height" type="number" min="0" step="any" placeholder="Height">' +
        '</label>' +

        '<button id="pixel-area-button" type="button">Calculate</button>' +

        '<div id="pixel-area-result"></div>' +
      '</div>';

    document.getElementById("pixel-area-button").addEventListener("click", function () {
      var widthPixels = parseFloat(document.getElementById("pixel-width").value);
      var heightPixels = parseFloat(document.getElementById("pixel-height").value);
      var result = document.getElementById("pixel-area-result");

      if (isNaN(widthPixels) || isNaN(heightPixels)) {
        result.textContent = "Please enter both pixel values.";
        return;
      }

      var widthMiles = (widthPixels / 1000) * 50;
      var heightMiles = (heightPixels / 1000) * 50;
      var squareMiles = widthMiles * heightMiles;

      result.innerHTML =
        "<strong>" + widthMiles.toLocaleString(undefined, { maximumFractionDigits: 0 }) + " miles</strong>" +
        " × " +
        "<strong>" + heightMiles.toLocaleString(undefined, { maximumFractionDigits: 0 }) + " miles</strong>" +
        " = " +
        "<strong>" + squareMiles.toLocaleString(undefined, { maximumFractionDigits: 0 }) + " mi²</strong>";
    });
  });
});

(function () {
    function stripHeaderLines(text) {
        return text
            // Remove standalone short lines that look like section headers
            .replace(/(?:^|\n)\s*[A-Z][A-Za-z0-9'’\- ,()]{0,50}\s*(?=\n)/g, '\n')
            // Clean up excess blank lines
            .replace(/\n{2,}/g, '\n')
            .trim();
    }

    function cleanAdarisPopups() {
        document.querySelectorAll(
            '.mwe-popups-extract, .mwe-popups-extract p, .mwe-popups .extract, .mwe-popups .mwe-popups-extract'
        ).forEach(function (el) {
            if (!el || el.dataset.adarisCleaned === '1') {
                return;
            }

            el.textContent = stripHeaderLines(el.textContent);
            el.dataset.adarisCleaned = '1';
        });
    }

    const observer = new MutationObserver(function () {
        cleanAdarisPopups();
        setTimeout(cleanAdarisPopups, 100);
        setTimeout(cleanAdarisPopups, 300);
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
        characterData: true
    });

    cleanAdarisPopups();
})();

mw.loader.using('mediawiki.util', function () {
  $(function () {
    var input = document.getElementById('pixelToMileInput');
    var output = document.getElementById('pixelToMileOutput');

    if (!input || !output) {
      return;
    }

    input.addEventListener('input', function () {
      var pixels = parseFloat(input.value);

      if (isNaN(pixels)) {
        output.textContent = '0 mi';
        return;
      }

      var miles = (pixels / 1000) * 50;

      output.textContent = Math.round(miles) + ' mi';
    });
  });
});

$(function () {
  var container = document.getElementById('pixel-to-mile-calculator');

  if (!container) {
    return;
  }

  container.className = 'adaris-tool-box';

  container.innerHTML =
    '<label for="pixelToMileInput"><strong>Pixels:</strong></label><br>' +
    '<input id="pixelToMileInput" type="number" placeholder="Enter pixels">' +
    '<p><strong>Distance:</strong> <span id="pixelToMileOutput">0 mi</span></p>';

  var input = document.getElementById('pixelToMileInput');
  var output = document.getElementById('pixelToMileOutput');

  input.addEventListener('input', function () {
    var pixels = parseFloat(input.value);

    if (isNaN(pixels)) {
      output.textContent = '0 mi';
      return;
    }

    var miles = (pixels / 1000) * 50;

    output.textContent = Math.round(miles) + ' mi';
  });
});