MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
Line 33: Line 33:
           var text = textarea.value;
           var text = textarea.value;


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

Revision as of 14:21, 3 May 2025

/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.load('//use.fontawesome.com/053a76b93c.js');
// Load exif-js for reading EXIF metadata
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');

$(document).ready(function () {
  $(document).on('change', 'input[type="file"]', function (e) {
    var file = e.target.files[0];
    if (!file || file.type.indexOf('image/') !== 0) 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");

      function convertDMSToDecimal(dms, ref) {
        var deg = dms[0];
        var min = dms[1];
        var 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;

        var textarea = document.querySelector('textarea[name="description"]');
        if (textarea) {
          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;
          }
        }
      } else {
        console.log("No GPS EXIF data found.");
      }
    });
  });
});