MediaWiki:Common.js

From Jcastle.info
Revision as of 15:08, 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 to read photo metadata
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');

console.log("🚀 Castle Photo GPS auto-fill script active");

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);
}

function insertGPS(textarea, gpsString) {
  var text = textarea.value;

  if (text.indexOf("|GPSLocation=") !== -1) {
    textarea.value = text.replace(/(\|GPSLocation=)[^\n]*/, "$1" + gpsString);
  } else if (text.indexOf("{{Castle Photo") !== -1) {
    textarea.value = text.replace(
      /(\{\{Castle Photo[\s\S]*?)(\|[^=]*=)/,
      "$1|GPSLocation=" + gpsString + "\n$2"
    );
  } else {
    console.log("⚠️ Could not find {{Castle Photo}} template to insert GPS.");
  }

  console.log("✅ Injected GPSLocation:", gpsString);
}

function attachGPSListener(input, textarea) {
  if (input.dataset.gpsListenerAttached) return;
  input.dataset.gpsListenerAttached = "true";

  input.addEventListener('change', function (e) {
    var file = e.target.files[0];
    if (!file || file.type.indexOf('image/') !== 0) {
      console.log("❌ Not a valid image file.");
      return;
    }

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

      if (!(lat && lon && latRef && lonRef)) {
        console.log("❌ No GPS EXIF data found.");
        return;
      }

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

      insertGPS(textarea, gpsString);
    });
  });
}

// Watch for file input and description field to appear
var observer = new MutationObserver(function () {
  var fileInputs = document.querySelectorAll('input[type="file"].fileupload');
  var textarea = document.querySelector('textarea[name="wfUploadDescription"]');

  if (fileInputs.length && textarea) {
    console.log("✅ Found file input(s) and textarea");

    fileInputs.forEach(function (input) {
      attachGPSListener(input, textarea);
    });

    observer.disconnect(); // done
  }
});

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