MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
 
(20 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
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');


console.log("🚀 GPS Auto-fill script starting...");
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"]');


function convertDMSToDecimal(dms, ref) {
  if ($gpsInput.length === 0 || $orderInput.length === 0 || $imgEl.length === 0) return;
  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 handleFileSelect(file, textarea) {
   var filename = $imgEl.attr('alt');  // e.g., yakami25.jpg
   EXIF.getData(file, function () {
    console.log("📦 EXIF loaded from", file.name);


    var lat = EXIF.getTag(this, "GPSLatitude");
  // Try to extract GPS values (optional)
    var latRef = EXIF.getTag(this, "GPSLatitudeRef");
  var gpsValue = null;
    var lon = EXIF.getTag(this, "GPSLongitude");
  var $latEl = $('.mediaexif-gpslatitude');
    var lonRef = EXIF.getTag(this, "GPSLongitudeRef");
  var $lonEl = $('.mediaexif-gpslongitude');
 
  if ($latEl.length && $lonEl.length) {
    if (!(lat && latRef && lon && lonRef)) {
     var latDec = parseFloat($latEl.text().trim());
      console.log("❌ No GPS EXIF data found.");
     var lonDec = parseFloat($lonEl.text().trim());
      return;
     if (!isNaN(latDec) && !isNaN(lonDec)) {
    }
       gpsValue = latDec.toFixed(6) + ", " + lonDec.toFixed(6);
 
     var latDec = convertDMSToDecimal(lat, latRef);
    var lonDec = convertDMSToDecimal(lon, lonRef);
    var gpsString = latDec + ", " + lonDec;
    console.log("✅ Extracted GPS:", 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("📝 GPSLOCATION injected into description field");
  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');
      }


// Use MutationObserver to watch for form elements
      var match = filename.match(/\d+/);
var observer = new MutationObserver(function () {
      if (match) {
  var fileInputs = document.querySelectorAll('input[type="file"].fileupload');
        var orderValue = parseInt(match[0], 10) * 10;
  var textarea = document.querySelector('textarea[name="wfUploadDescription"]');
        $orderInput.val(orderValue).focus().trigger('change');
      }


  if (fileInputs.length > 0 && textarea) {
//     alert("✅ Fields updated.");
    console.log("✅ File input(s) and description field found");
 
    fileInputs.forEach(function (input) {
      // Avoid double binding
      if (input.dataset.gpsListenerAttached) return;
      input.dataset.gpsListenerAttached = "true";
 
      input.addEventListener("change", function (e) {
        if (e.target.files.length > 0) {
          console.log("📂 File selected:", e.target.files[0].name);
          handleFileSelect(e.target.files[0], textarea);
        }
      });
     });
     });


    observer.disconnect();
  $gpsInput.after($button);
  }
}, 500);
});
 
// Start observing the document for dynamic form loading
observer.observe(document.body, { childList: true, subtree: true });

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);