MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
 
(16 intermediate revisions by the same user not shown)
Line 2: Line 2:
mw.loader.load('//use.fontawesome.com/053a76b93c.js');
mw.loader.load('//use.fontawesome.com/053a76b93c.js');


// Load EXIF.js to read photo metadata
setTimeout(function () {
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');
  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"]');


console.log("🚀 Castle Photo GPS auto-fill script active");
  if ($gpsInput.length === 0 || $orderInput.length === 0 || $imgEl.length === 0) return;


function convertDMSToDecimal(dms, ref) {
   var filename = $imgEl.attr('alt'); // e.g., yakami25.jpg
   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) {
  // Try to extract GPS values (optional)
   var text = textarea.value;
   var gpsValue = null;
 
  var $latEl = $('.mediaexif-gpslatitude');
   if (text.indexOf("|GPSLocation=") !== -1) {
  var $lonEl = $('.mediaexif-gpslongitude');
     textarea.value = text.replace(/(\|GPSLocation=)[^\n]*/, "$1" + gpsString);
   if ($latEl.length && $lonEl.length) {
  } else if (text.indexOf("{{Castle Photo") !== -1) {
     var latDec = parseFloat($latEl.text().trim());
     textarea.value = text.replace(
    var lonDec = parseFloat($lonEl.text().trim());
      /(\{\{Castle Photo[\s\S]*?)(\|[^=]*=)/,
     if (!isNaN(latDec) && !isNaN(lonDec)) {
       "$1|GPSLocation=" + gpsString + "\n$2"
       gpsValue = latDec.toFixed(6) + ", " + lonDec.toFixed(6);
    );
     }
  } else {
     console.log("⚠️ Could not find {{Castle Photo}} template to insert GPS.");
   }
   }


   console.log("✅ Injected GPSLocation:", gpsString);
   var $button = $('<button>')
}
    .text("📋 Insert EXIF GPS & Order")
 
    .attr("type", "button")
function attachGPSListener(input, textarea) {
     .css({ marginLeft: "0.5em", fontSize: "90%" })
  if (input.dataset.gpsListenerAttached) return;
     .click(function () {
  input.dataset.gpsListenerAttached = "true";
       if (gpsValue) {
 
        $gpsInput.val(gpsValue).focus().trigger('change');
  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)) {
      var match = filename.match(/\d+/);
         console.log("❌ No GPS EXIF data found.");
       if (match) {
         return;
         var orderValue = parseInt(match[0], 10) * 10;
         $orderInput.val(orderValue).focus().trigger('change');
       }
       }


      var latDec = convertDMSToDecimal(lat, latRef);
//      alert("✅ Fields updated.");
      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 });
  $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);