MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
Line 3: Line 3:


$(function () {
$(function () {
   // Only run on file description edit pages
   // Run only on Semantic Forms pages
   if (!mw.config.get('wgCanonicalSpecialPageName') &&
   if (mw.config.get('wgAction') === 'formedit') {
      mw.config.get('wgAction') === 'edit' &&
    console.log("📄 GPS helper active on form edit page");
      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 () {
     setTimeout(function () {
       var latText = mw.util.$content.find('.mediaexif-gpslatitude').text().trim();
       var latText = mw.util.$content.find('.mediaexif-gpslatitude').text().trim();
Line 16: Line 12:


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


       // Normalize GPS (assumes format like 35 deg 12' 33.42" N)
       // Convert DMS to decimal
       function dmsToDecimal(dmsStr) {
       function dmsToDecimal(dmsStr) {
         var parts = dmsStr.match(/([\d.]+)[^\d]+([\d.]+)[^\d]+([\d.]+)?[^\d]+([NSEW])/);
         var parts = dmsStr.match(/([\d.]+)[^\d]+([\d.]+)[^\d]+([\d.]+)?[^\d]+([NSEW])/);
Line 36: Line 32:
       var lonDec = dmsToDecimal(lonText);
       var lonDec = dmsToDecimal(lonText);
       if (!latDec || !lonDec) {
       if (!latDec || !lonDec) {
         console.log("❌ Failed to convert GPS EXIF to decimal.");
         console.log("❌ GPS conversion failed.");
         return;
         return;
       }
       }


       var gpsLine = "|GPSLocation=" + latDec + ", " + lonDec;
       var gpsValue = latDec + ", " + lonDec;
 
      // Add button next to GPSLocation input field
      var $input = $('input[name$="[GPSLocation]"], textarea[name$="[GPSLocation]"]');
 
      if ($input.length === 0) {
        console.log("⚠️ Could not find GPSLocation input field.");
        return;
      }


      // Add button to copy/insert
       var $button = $('<button>')
       var $button = $('<button>')
         .text("📋 Copy GPS to field")
         .text("📋 Use EXIF GPS: " + gpsValue)
         .css({ marginLeft: "1em", fontSize: "90%" })
         .css({ marginLeft: "1em", fontSize: "90%" })
         .click(function () {
         .click(function (e) {
           var $ta = $('textarea[name="wpTextbox1"]');
           e.preventDefault();
          var lines = $ta.val().split("\n");
           $input.val(gpsValue).focus();
          var updated = false;
          alert("✅ GPS inserted into field.");
 
          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
       $input.after($button);
      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
     }, 500); // Wait for DOM and EXIF to fully render
   }
   }
});
});

Revision as of 15:27, 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 () {
  // Run only on Semantic Forms pages
  if (mw.config.get('wgAction') === 'formedit') {
    console.log("📄 GPS helper active on form edit page");

    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.");
        return;
      }

      // Convert DMS to decimal
      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("❌ GPS conversion failed.");
        return;
      }

      var gpsValue = latDec + ", " + lonDec;

      // Add button next to GPSLocation input field
      var $input = $('input[name$="[GPSLocation]"], textarea[name$="[GPSLocation]"]');

      if ($input.length === 0) {
        console.log("⚠️ Could not find GPSLocation input field.");
        return;
      }

      var $button = $('<button>')
        .text("📋 Use EXIF GPS: " + gpsValue)
        .css({ marginLeft: "1em", fontSize: "90%" })
        .click(function (e) {
          e.preventDefault();
          $input.val(gpsValue).focus();
          alert("✅ GPS inserted into field.");
        });

      $input.after($button);

    }, 500); // Wait for DOM and EXIF to fully render
  }
});