MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
 
(15 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');


$(function () {
setTimeout(function () {
   // Only run on file description edit pages
   var $gpsInput = $('input[name="Castle Photo[GPSLocation]"]');
   if (!mw.config.get('wgCanonicalSpecialPageName') &&
   var $orderInput = $('input[name="Castle Photo[order]"]');
      mw.config.get('wgAction') === 'edit' &&
  var $imgEl = $('img[alt$=".jpg"], img[alt$=".jpeg"], img[alt$=".JPG"], img[alt$=".JPEG"]');
      mw.config.get('wgNamespaceNumber') === 6) { // NS_FILE = 6


    console.log("🧭 GPS helper active on file edit page");
  if ($gpsInput.length === 0 || $orderInput.length === 0 || $imgEl.length === 0) return;


    // Wait for page to finish loading
  var filename = $imgEl.attr('alt'); // e.g., yakami25.jpg
    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) {
  // Try to extract GPS values (optional)
        console.log("❌ No GPSLatitude or GPSLongitude found on page.");
  var gpsValue = null;
        return;
  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);
    }
  }


      // Normalize GPS (assumes format like 35 deg 12' 33.42" N)
  var $button = $('<button>')
      function dmsToDecimal(dmsStr) {
    .text("📋 Insert EXIF GPS & Order")
        var parts = dmsStr.match(/([\d.]+)[^\d]+([\d.]+)[^\d]+([\d.]+)?[^\d]+([NSEW])/);
    .attr("type", "button")
        if (!parts) return null;
    .css({ marginLeft: "0.5em", fontSize: "90%" })
         var deg = parseFloat(parts[1]);
    .click(function () {
        var min = parseFloat(parts[2]);
      if (gpsValue) {
        var sec = parseFloat(parts[3] || "0");
         $gpsInput.val(gpsValue).focus().trigger('change');
        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 match = filename.match(/\d+/);
       var lonDec = dmsToDecimal(lonText);
       if (match) {
      if (!latDec || !lonDec) {
        var orderValue = parseInt(match[0], 10) * 10;
        console.log("❌ Failed to convert GPS EXIF to decimal.");
        $orderInput.val(orderValue).focus().trigger('change');
        return;
       }
       }


      var gpsLine = "|GPSLocation=" + latDec + ", " + lonDec;
//     alert("✅ Fields updated.");
 
    });
      // 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) {
  $gpsInput.after($button);
            // Insert after {{Castle Photo line
}, 500);
            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
  }
});

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