MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.load('//use.fontawesome.com/053a76b93c.js');
mw.loader.load('//use.fontawesome.com/053a76b93c.js');
// Load exif-js for reading EXIF metadata
// Load exif-js
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');
console.log("MediaWiki:Common.js script loaded");
$(document).ready(function () {
console.log(EXIF.getAllTags(this));
  $(document).on('change', 'input[type="file"]', function (e) {
    var file = e.target.files[0];
    if (!file || file.type.indexOf('image/') !== 0) return;


    console.log("File selected:", file.name);
// 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"]');


    EXIF.getData(file, function () {
  if (fileInput && textarea) {
      console.log("EXIF loaded");
    console.log("✅ File input and textarea detected. Setting up listener.");


      var lat = EXIF.getTag(this, "GPSLatitude");
    fileInput.addEventListener('change', function (e) {
       var latRef = EXIF.getTag(this, "GPSLatitudeRef");
       var file = e.target.files[0];
       var lon = EXIF.getTag(this, "GPSLongitude");
       if (!file || file.type.indexOf('image/') !== 0) {
      var lonRef = EXIF.getTag(this, "GPSLongitudeRef");
        console.log("⚠️ Not an image file or nothing selected.");
        return;
      }


       function convertDMSToDecimal(dms, ref) {
       console.log("📷 File selected:", file.name);
        var deg = dms[0];
 
        var min = dms[1];
      EXIF.getData(file, function () {
        var sec = dms[2];
        console.log("🔍 EXIF loaded");
        var dec = deg + (min / 60) + (sec / 3600);
 
        if (ref === "S" || ref === "W") dec *= -1;
        var lat = EXIF.getTag(this, "GPSLatitude");
        return dec.toFixed(6);
        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) {
        if (lat && lon && latRef && lonRef) {
        var latDec = convertDMSToDecimal(lat, latRef);
          var latDec = convertDMSToDecimal(lat, latRef);
        var lonDec = convertDMSToDecimal(lon, lonRef);
          var lonDec = convertDMSToDecimal(lon, lonRef);
        var gpsString = latDec + ", " + lonDec;
          var gpsString = latDec + ", " + lonDec;


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


        var textarea = document.querySelector('textarea[name="wfUploadDescription"]');
        if (textarea) {
           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;
           }
           }
          console.log("✅ Inserted GPSLOCATION into template");
        } else {
          console.log("❌ No GPS EXIF data found.");
         }
         }
       } else {
       });
        console.log("No GPS EXIF data found.");
      }
     });
     });
   });
   } else {
});
    // Retry after a short delay if inputs not yet ready
    setTimeout(waitForFormElements, 500);
  }
})();

Revision as of 14:37, 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
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);
  }
})();