MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
Tag: Reverted
No edit summary
 
(31 intermediate revisions by the same user not shown)
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
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');


$(document).ready(function () {
setTimeout(function () {
    $(document).on('change', 'input[type="file"]', function (e) {
  var $gpsInput = $('input[name="Castle Photo[GPSLocation]"]');
        const file = e.target.files[0];
  var $orderInput = $('input[name="Castle Photo[order]"]');
        if (!file || !file.type.startsWith('image/')) return;
  var $imgEl = $('img[alt$=".jpg"], img[alt$=".jpeg"], img[alt$=".JPG"], img[alt$=".JPEG"]');


        EXIF.getData(file, function () {
  if ($gpsInput.length === 0 || $orderInput.length === 0 || $imgEl.length === 0) return;
            const lat = EXIF.getTag(this, "GPSLatitude");
            const latRef = EXIF.getTag(this, "GPSLatitudeRef");
            const lon = EXIF.getTag(this, "GPSLongitude");
            const lonRef = EXIF.getTag(this, "GPSLongitudeRef");


            // Convert DMS to decimal
  var filename = $imgEl.attr('alt'); // e.g., yakami25.jpg
            function convertDMSToDecimal(dms, ref) {
                const [deg, min, sec] = dms;
                let dec = deg + (min / 60) + (sec / 3600);
                if (ref === "S" || ref === "W") dec *= -1;
                return dec.toFixed(6);
            }


            if (lat && lon && latRef && lonRef) {
  // Try to extract GPS values (optional)
                const latDec = convertDMSToDecimal(lat, latRef);
  var gpsValue = null;
                const lonDec = convertDMSToDecimal(lon, lonRef);
  var $latEl = $('.mediaexif-gpslatitude');
                const gpsString = `${latDec}, ${lonDec}`;
  var $lonEl = $('.mediaexif-gpslongitude');
  if ($latEl.length && $lonEl.length) {
    var latDec = parseFloat($latEl.text().trim());
    var lonDec = parseFloat($lonEl.text().trim());
    if (!isNaN(latDec) && !isNaN(lonDec)) {
      gpsValue = latDec.toFixed(6) + ", " + lonDec.toFixed(6);
    }
  }


                // Find the description/template box (usually part of SimpleBatchUpload UI)
  var $button = $('<button>')
                const textarea = document.querySelector('textarea[name="description"]');
    .text("📋 Insert EXIF GPS & Order")
                if (textarea) {
    .attr("type", "button")
                    let text = textarea.value;
    .css({ marginLeft: "0.5em", fontSize: "90%" })
    .click(function () {
      if (gpsValue) {
        $gpsInput.val(gpsValue).focus().trigger('change');
      }


                    // If field exists, replace it
      var match = filename.match(/\d+/);
                    if (text.includes("|GPSLOCATION=")) {
      if (match) {
                        textarea.value = text.replace(/(\|GPSLOCATION=)[^\n]*/, `$1${gpsString}`);
        var orderValue = parseInt(match[0], 10) * 10;
                    } else if (text.includes("{{Castle PhotoBatch")) {
        $orderInput.val(orderValue).focus().trigger('change');
                        // Insert GPSLOCATION line in proper place
      }
                        textarea.value = text.replace(
 
                            /(\{\{Castle PhotoBatch[\s\S]*?)(\|[^=]*=)/,
//     alert("✅ Fields updated.");
                            `$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 {
                console.log("No GPS EXIF data found.");
            }
        });
     });
     });
});
 
  $gpsInput.after($button);
}, 500);

Latest revision as of 16:09, 18 May 2025

/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.load('//use.fontawesome.com/053a76b93c.js');

setTimeout(function () {
  var $gpsInput = $('input[name="Castle Photo[GPSLocation]"]');
  var $orderInput = $('input[name="Castle Photo[order]"]');
  var $imgEl = $('img[alt$=".jpg"], img[alt$=".jpeg"], img[alt$=".JPG"], img[alt$=".JPEG"]');

  if ($gpsInput.length === 0 || $orderInput.length === 0 || $imgEl.length === 0) return;

  var filename = $imgEl.attr('alt');  // e.g., yakami25.jpg

  // Try to extract GPS values (optional)
  var gpsValue = null;
  var $latEl = $('.mediaexif-gpslatitude');
  var $lonEl = $('.mediaexif-gpslongitude');
  if ($latEl.length && $lonEl.length) {
    var latDec = parseFloat($latEl.text().trim());
    var lonDec = parseFloat($lonEl.text().trim());
    if (!isNaN(latDec) && !isNaN(lonDec)) {
      gpsValue = latDec.toFixed(6) + ", " + lonDec.toFixed(6);
    }
  }

  var $button = $('<button>')
    .text("📋 Insert EXIF GPS & Order")
    .attr("type", "button")
    .css({ marginLeft: "0.5em", fontSize: "90%" })
    .click(function () {
      if (gpsValue) {
        $gpsInput.val(gpsValue).focus().trigger('change');
      }

      var match = filename.match(/\d+/);
      if (match) {
        var orderValue = parseInt(match[0], 10) * 10;
        $orderInput.val(orderValue).focus().trigger('change');
      }

//      alert("✅ Fields updated.");
    });

  $gpsInput.after($button);
}, 500);