MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
Tag: Manual revert
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 image 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');


$(document).ready(function () {
if (
  mw.config.get("wgCanonicalSpecialPageName") === "SimpleBatchUpload" &&
  mw.config.get("wgTitle").indexOf("Castle_PhotoBatch") !== -1
) {
  $(document).ready(function () {
     $(document).on('change', 'input[type="file"]', function (e) {
     $(document).on('change', 'input[type="file"]', function (e) {
        const file = e.target.files[0];
      var file = e.target.files[0];
        if (!file || !file.type.startsWith('image/')) return;
      if (!file || file.type.indexOf('image/') !== 0) return;


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


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


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


                // Find the description/template box (usually part of SimpleBatchUpload UI)
          var textarea = document.querySelector('textarea[name="description"]');
                const textarea = document.querySelector('textarea[name="description"]');
          if (textarea) {
                if (textarea) {
            var text = textarea.value;
                    let text = textarea.value;


                    // If field exists, replace it
            if (text.indexOf("|GPSLOCATION=") !== -1) {
                    if (text.includes("|GPSLOCATION=")) {
              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.includes("{{Castle PhotoBatch")) {
              textarea.value = text.replace(
                        // Insert GPSLOCATION line in proper place
                /(\{\{Castle PhotoBatch[\s\S]*?)(\|[^=]*=)/,
                        textarea.value = text.replace(
                "$1|GPSLOCATION=" + gpsString + "\n$2"
                            /(\{\{Castle PhotoBatch[\s\S]*?)(\|[^=]*=)/,
              );
                            `$1|GPSLOCATION=${gpsString}\n$2`
                        );
                    } else {
                        // If template isn't yet filled, prefill
                        textarea.value =
`{{Castle PhotoBatch
|CASTLENAME=
|SOURCE=
|PHOTODATE=
|GPSLOCATION=${gpsString}
|OTHER=
}}` + "\n\n" + text;
                    }
                }
             } else {
             } else {
                 console.log("No GPS EXIF data found.");
              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.");
        }
      });
     });
     });
});
  });
}

Revision as of 14:17, 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');

if (
  mw.config.get("wgCanonicalSpecialPageName") === "SimpleBatchUpload" &&
  mw.config.get("wgTitle").indexOf("Castle_PhotoBatch") !== -1
) {
  $(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.");
        }
      });
    });
  });
}