MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
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
$(function () {
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js');
  // Only run on file description edit pages
  if (!mw.config.get('wgCanonicalSpecialPageName') &&
      mw.config.get('wgAction') === 'edit' &&
      mw.config.get('wgNamespaceNumber') === 6) { // NS_FILE = 6


console.log("🚀 Castle Photo GPS auto-fill script active");
    console.log("🧭 GPS helper active on file edit page");


function convertDMSToDecimal(dms, ref) {
    // Wait for page to finish loading
  var deg = dms[0], min = dms[1], sec = dms[2];
    setTimeout(function () {
  var dec = deg + (min / 60) + (sec / 3600);
      var latText = mw.util.$content.find('.mediaexif-gpslatitude').text().trim();
  if (ref === "S" || ref === "W") dec *= -1;
      var lonText = mw.util.$content.find('.mediaexif-gpslongitude').text().trim();
  return dec.toFixed(6);
}


function insertGPS(textarea, gpsString) {
      if (!latText || !lonText) {
  var text = textarea.value;
        console.log("❌ No GPSLatitude or GPSLongitude found on page.");
        return;
      }


  if (text.indexOf("|GPSLocation=") !== -1) {
      // Normalize GPS (assumes format like 35 deg 12' 33.42" N)
    textarea.value = text.replace(/(\|GPSLocation=)[^\n]*/, "$1" + gpsString);
      function dmsToDecimal(dmsStr) {
  } else if (text.indexOf("{{Castle Photo") !== -1) {
        var parts = dmsStr.match(/([\d.]+)[^\d]+([\d.]+)[^\d]+([\d.]+)?[^\d]+([NSEW])/);
    textarea.value = text.replace(
        if (!parts) return null;
      /(\{\{Castle Photo[\s\S]*?)(\|[^=]*=)/,
        var deg = parseFloat(parts[1]);
      "$1|GPSLocation=" + gpsString + "\n$2"
        var min = parseFloat(parts[2]);
    );
        var sec = parseFloat(parts[3] || "0");
  } else {
        var ref = parts[4];
    console.log("⚠️ Could not find {{Castle Photo}} template to insert GPS.");
        var dec = deg + min / 60 + sec / 3600;
  }
        if (ref === "S" || ref === "W") dec *= -1;
        return dec.toFixed(6);
      }


  console.log("✅ Injected GPSLocation:", gpsString);
      var latDec = dmsToDecimal(latText);
}
      var lonDec = dmsToDecimal(lonText);
      if (!latDec || !lonDec) {
        console.log("❌ Failed to convert GPS EXIF to decimal.");
        return;
      }


function attachGPSListener(input, textarea) {
      var gpsLine = "|GPSLocation=" + latDec + ", " + lonDec;
  if (input.dataset.gpsListenerAttached) return;
  input.dataset.gpsListenerAttached = "true";


  input.addEventListener('change', function (e) {
      // Add button to copy/insert
    var file = e.target.files[0];
      var $button = $('<button>')
    if (!file || file.type.indexOf('image/') !== 0) {
        .text("📋 Copy GPS to field")
      console.log("❌ Not a valid image file.");
        .css({ marginLeft: "1em", fontSize: "90%" })
      return;
        .click(function () {
    }
          var $ta = $('textarea[name="wpTextbox1"]');
          var lines = $ta.val().split("\n");
          var updated = false;


    EXIF.getData(file, function () {
          for (var i = 0; i < lines.length; i++) {
      var lat = EXIF.getTag(this, "GPSLatitude");
            if (lines[i].match(/^\|GPSLocation=/)) {
      var latRef = EXIF.getTag(this, "GPSLatitudeRef");
              lines[i] = gpsLine;
      var lon = EXIF.getTag(this, "GPSLongitude");
              updated = true;
      var lonRef = EXIF.getTag(this, "GPSLongitudeRef");
              break;
            }
          }


      if (!(lat && lon && latRef && lonRef)) {
          if (!updated) {
        console.log("❌ No GPS EXIF data found.");
            // Insert after {{Castle Photo line
        return;
            for (var i = 0; i < lines.length; i++) {
      }
              if (lines[i].match(/^\{\{Castle Photo/)) {
                lines.splice(i + 1, 0, gpsLine);
                updated = true;
                break;
              }
            }
          }


      var latDec = convertDMSToDecimal(lat, latRef);
          if (updated) {
      var lonDec = convertDMSToDecimal(lon, lonRef);
            $ta.val(lines.join("\n"));
      var gpsString = latDec + ", " + lonDec;
            console.log("✅ GPS inserted:", gpsLine);


      insertGPS(textarea, gpsString);
            // Scroll to the line
    });
            var gpsIndex = $ta.val().indexOf(gpsLine);
  });
            if (gpsIndex !== -1) {
}
              $ta[0].focus();
              $ta[0].setSelectionRange(gpsIndex, gpsIndex + gpsLine.length);
            }


// Watch for file input and description field to appear
            alert("✅ GPS inserted into GPSLocation field.");
var observer = new MutationObserver(function () {
          } else {
  var fileInputs = document.querySelectorAll('input[type="file"].fileupload');
            alert("⚠️ Could not find place to insert GPSLocation.");
  var textarea = document.querySelector('textarea[name="wfUploadDescription"]');
          }
        });


  if (fileInputs.length && textarea) {
      // Append to EXIF display area
    console.log("✅ Found file input(s) and textarea");
      var $infoBox = mw.util.$content.find('.mediaexif');
      if ($infoBox.length) {
        $infoBox.first().append($('<div>').append($button));
      } else {
        console.log("⚠️ Could not find mediaexif display container.");
      }


     fileInputs.forEach(function (input) {
     }, 500); // Slight delay to ensure content is present
      attachGPSListener(input, textarea);
    });
 
    observer.disconnect(); // done
   }
   }
});
});
observer.observe(document.body, { childList: true, subtree: true });

Revision as of 15:23, 3 May 2025

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

$(function () {
  // Only run on file description edit pages
  if (!mw.config.get('wgCanonicalSpecialPageName') &&
      mw.config.get('wgAction') === 'edit' &&
      mw.config.get('wgNamespaceNumber') === 6) { // NS_FILE = 6

    console.log("🧭 GPS helper active on file edit page");

    // Wait for page to finish loading
    setTimeout(function () {
      var latText = mw.util.$content.find('.mediaexif-gpslatitude').text().trim();
      var lonText = mw.util.$content.find('.mediaexif-gpslongitude').text().trim();

      if (!latText || !lonText) {
        console.log("❌ No GPSLatitude or GPSLongitude found on page.");
        return;
      }

      // Normalize GPS (assumes format like 35 deg 12' 33.42" N)
      function dmsToDecimal(dmsStr) {
        var parts = dmsStr.match(/([\d.]+)[^\d]+([\d.]+)[^\d]+([\d.]+)?[^\d]+([NSEW])/);
        if (!parts) return null;
        var deg = parseFloat(parts[1]);
        var min = parseFloat(parts[2]);
        var sec = parseFloat(parts[3] || "0");
        var ref = parts[4];
        var dec = deg + min / 60 + sec / 3600;
        if (ref === "S" || ref === "W") dec *= -1;
        return dec.toFixed(6);
      }

      var latDec = dmsToDecimal(latText);
      var lonDec = dmsToDecimal(lonText);
      if (!latDec || !lonDec) {
        console.log("❌ Failed to convert GPS EXIF to decimal.");
        return;
      }

      var gpsLine = "|GPSLocation=" + latDec + ", " + lonDec;

      // Add button to copy/insert
      var $button = $('<button>')
        .text("📋 Copy GPS to field")
        .css({ marginLeft: "1em", fontSize: "90%" })
        .click(function () {
          var $ta = $('textarea[name="wpTextbox1"]');
          var lines = $ta.val().split("\n");
          var updated = false;

          for (var i = 0; i < lines.length; i++) {
            if (lines[i].match(/^\|GPSLocation=/)) {
              lines[i] = gpsLine;
              updated = true;
              break;
            }
          }

          if (!updated) {
            // Insert after {{Castle Photo line
            for (var i = 0; i < lines.length; i++) {
              if (lines[i].match(/^\{\{Castle Photo/)) {
                lines.splice(i + 1, 0, gpsLine);
                updated = true;
                break;
              }
            }
          }

          if (updated) {
            $ta.val(lines.join("\n"));
            console.log("✅ GPS inserted:", gpsLine);

            // Scroll to the line
            var gpsIndex = $ta.val().indexOf(gpsLine);
            if (gpsIndex !== -1) {
              $ta[0].focus();
              $ta[0].setSelectionRange(gpsIndex, gpsIndex + gpsLine.length);
            }

            alert("✅ GPS inserted into GPSLocation field.");
          } else {
            alert("⚠️ Could not find place to insert GPSLocation.");
          }
        });

      // Append to EXIF display area
      var $infoBox = mw.util.$content.find('.mediaexif');
      if ($infoBox.length) {
        $infoBox.first().append($('<div>').append($button));
      } else {
        console.log("⚠️ Could not find mediaexif display container.");
      }

    }, 500); // Slight delay to ensure content is present
  }
});