MediaWiki:Common.js: Difference between revisions
From Jcastle.info
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.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..."); | |||
if ( | function convertDMSToDecimal(dms, ref) { | ||
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) { | |||
EXIF.getData(file, function () { | |||
console.log("📦 EXIF loaded from", file.name); | |||
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 && latRef && lon && lonRef)) { | |||
console.log("❌ No GPS EXIF data found."); | |||
return; | |||
} | |||
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"); | |||
}); | |||
} | |||
// Use MutationObserver to watch for form elements | |||
var observer = new MutationObserver(function () { | |||
var fileInputs = document.querySelectorAll('input[type="file"].fileupload'); | |||
var textarea = document.querySelector('textarea[name="wfUploadDescription"]'); | |||
if (fileInputs.length > 0 && textarea) { | |||
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(" | console.log("📂 File selected:", e.target.files[0].name); | ||
handleFileSelect(e.target.files[0], textarea); | |||
} | } | ||
}); | }); | ||
}); | }); | ||
observer.disconnect(); | |||
} | } | ||
})(); | }); | ||
// Start observing the document for dynamic form loading | |||
observer.observe(document.body, { childList: true, subtree: true }); |
Revision as of 14:40, 3 May 2025
/* Any JavaScript here will be loaded for all users on every page load. */ 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..."); function convertDMSToDecimal(dms, ref) { 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) { EXIF.getData(file, function () { console.log("📦 EXIF loaded from", file.name); 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 && latRef && lon && lonRef)) { console.log("❌ No GPS EXIF data found."); return; } 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"); }); } // Use MutationObserver to watch for form elements var observer = new MutationObserver(function () { var fileInputs = document.querySelectorAll('input[type="file"].fileupload'); var textarea = document.querySelector('textarea[name="wfUploadDescription"]'); if (fileInputs.length > 0 && textarea) { 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(); } }); // Start observing the document for dynamic form loading observer.observe(document.body, { childList: true, subtree: true });