MediaWiki:Common.js

From Jcastle.info
Revision as of 14:37, 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');
// Load exif-js
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');

// Wait until both the file input and textarea are on the page
(function waitForFormElements() {
  var fileInput = document.querySelector('input[type="file"]');
  var textarea = document.querySelector('textarea[name="wfUploadDescription"]');

  if (fileInput && textarea) {
    console.log("✅ File input and textarea detected. Setting up listener.");

    fileInput.addEventListener('change', function (e) {
      var file = e.target.files[0];
      if (!file || file.type.indexOf('image/') !== 0) {
        console.log("⚠️ Not an image file or nothing selected.");
        return;
      }

      console.log("📷 File selected:", file.name);

      EXIF.getData(file, function () {
        console.log("🔍 EXIF loaded");

        var lat = EXIF.getTag(this, "GPSLatitude");
        var latRef = EXIF.getTag(this, "GPSLatitudeRef");
        var lon = EXIF.getTag(this, "GPSLongitude");
        var lonRef = EXIF.getTag(this, "GPSLongitudeRef");

        console.log("📡 Raw GPS tags:", lat, latRef, lon, lonRef);

        function convertDMSToDecimal(dms, ref) {
          var deg = dms[0], min = dms[1], sec = dms[2];
          var dec = deg + (min / 60) + (sec / 3600);
          if (ref === "S" || ref === "W") dec *= -1;
          return dec.toFixed(6);
        }

        if (lat && lon && latRef && lonRef) {
          var latDec = convertDMSToDecimal(lat, latRef);
          var lonDec = convertDMSToDecimal(lon, lonRef);
          var gpsString = latDec + ", " + lonDec;

          console.log("✅ GPS Coordinates:", gpsString);

          var text = textarea.value;

          if (text.indexOf("|GPSLOCATION=") !== -1) {
            textarea.value = text.replace(/(\|GPSLOCATION=)[^\n]*/, "$1" + gpsString);
          } else if (text.indexOf("{{Castle PhotoBatch") !== -1) {
            textarea.value = text.replace(
              /(\{\{Castle PhotoBatch[\s\S]*?)(\|[^=]*=)/,
              "$1|GPSLOCATION=" + gpsString + "\n$2"
            );
          } else {
            textarea.value =
              "{{Castle PhotoBatch\n|CASTLENAME=\n|SOURCE=\n|PHOTODATE=\n|GPSLOCATION=" +
              gpsString + "\n|OTHER=\n}}\n\n" + text;
          }

          console.log("✅ Inserted GPSLOCATION into template");
        } else {
          console.log("❌ No GPS EXIF data found.");
        }
      });
    });
  } else {
    // Retry after a short delay if inputs not yet ready
    setTimeout(waitForFormElements, 500);
  }
})();