MediaWiki:Common.js

From Jcastle.info
Revision as of 15:27, 3 May 2025 by Eric (talk | contribs)

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.
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.load('//use.fontawesome.com/053a76b93c.js');

$(function () {
  // Run only on Semantic Forms pages
  if (mw.config.get('wgAction') === 'formedit') {
    console.log("📄 GPS helper active on form edit page");

    setTimeout(function () {
      var latText = mw.util.$content.find('.mediaexif-gpslatitude').text().trim();
      var lonText = mw.util.$content.find('.mediaexif-gpslongitude').text().trim();

      if (!latText || !lonText) {
        console.log("❌ No GPSLatitude or GPSLongitude found.");
        return;
      }

      // Convert DMS to decimal
      function dmsToDecimal(dmsStr) {
        var parts = dmsStr.match(/([\d.]+)[^\d]+([\d.]+)[^\d]+([\d.]+)?[^\d]+([NSEW])/);
        if (!parts) return null;
        var deg = parseFloat(parts[1]);
        var min = parseFloat(parts[2]);
        var sec = parseFloat(parts[3] || "0");
        var ref = parts[4];
        var dec = deg + min / 60 + sec / 3600;
        if (ref === "S" || ref === "W") dec *= -1;
        return dec.toFixed(6);
      }

      var latDec = dmsToDecimal(latText);
      var lonDec = dmsToDecimal(lonText);
      if (!latDec || !lonDec) {
        console.log("❌ GPS conversion failed.");
        return;
      }

      var gpsValue = latDec + ", " + lonDec;

      // Add button next to GPSLocation input field
      var $input = $('input[name$="[GPSLocation]"], textarea[name$="[GPSLocation]"]');

      if ($input.length === 0) {
        console.log("⚠️ Could not find GPSLocation input field.");
        return;
      }

      var $button = $('<button>')
        .text("📋 Use EXIF GPS: " + gpsValue)
        .css({ marginLeft: "1em", fontSize: "90%" })
        .click(function (e) {
          e.preventDefault();
          $input.val(gpsValue).focus();
          alert("✅ GPS inserted into field.");
        });

      $input.after($button);

    }, 500); // Wait for DOM and EXIF to fully render
  }
});