MediaWiki:Common.js: Difference between revisions
From Jcastle.info
No edit summary |
No edit summary |
||
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'); | ||
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'); | ||
function convertDMSToDecimal(dms, ref) { | function convertDMSToDecimal(dms, ref) { | ||
Line 13: | Line 10: | ||
} | } | ||
function | function insertGPS(textarea, gpsString) { | ||
var text = textarea.value; | |||
if (text.indexOf("|GPSLOCATION=") !== -1) { | |||
// Replace existing GPSLOCATION | |||
textarea.value = text.replace(/(\|GPSLOCATION=)[^\n]*/, "$1" + gpsString); | |||
} else if (text.indexOf("{{Castle PhotoBatch") !== -1) { | |||
// Insert into template | |||
textarea.value = text.replace( | |||
/(\{\{Castle PhotoBatch[\s\S]*?)(\|[^=]*=)/, | |||
"$1|GPSLOCATION=" + gpsString + "\n$2" | |||
); | |||
} else { | |||
console.log("⚠️ Could not find template to insert GPS."); | |||
} | |||
console.log("✅ Injected GPSLOCATION:", gpsString); | |||
} | |||
function attachGPSListener(input, textarea) { | |||
if (input.dataset.gpsListenerAttached) return; | |||
input.dataset.gpsListenerAttached = "true"; | |||
var | input.addEventListener('change', function (e) { | ||
var file = e.target.files[0]; | |||
if (!file || file.type.indexOf('image/') !== 0) return; | |||
EXIF.getData(file, function () { | |||
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 && lon && latRef && lonRef)) { | |||
console.log("❌ No GPS EXIF found."); | |||
return; | |||
} | |||
var latDec = convertDMSToDecimal(lat, latRef); | |||
var lonDec = convertDMSToDecimal(lon, lonRef); | |||
insertGPS(textarea, latDec + ", " + lonDec); | |||
}); | |||
}); | }); | ||
} | } | ||
var observer = new MutationObserver(function () { | var observer = new MutationObserver(function () { | ||
var fileInputs = document.querySelectorAll('input[type="file"].fileupload'); | var fileInputs = document.querySelectorAll('input[type="file"].fileupload'); | ||
var textarea = document.querySelector('textarea[name="wfUploadDescription"]'); | var textarea = document.querySelector('textarea[name="wfUploadDescription"]'); | ||
if (fileInputs.length | if (fileInputs.length && textarea) { | ||
fileInputs.forEach(function (input) { | fileInputs.forEach(function (input) { | ||
attachGPSListener(input, textarea); | |||
}); | }); | ||
observer.disconnect(); | observer.disconnect(); | ||
} | } | ||
}); | }); | ||
observer.observe(document.body, { childList: true, subtree: true }); | observer.observe(document.body, { childList: true, subtree: true }); |
Revision as of 14:49, 3 May 2025
/* Any JavaScript here will be loaded for all users on every page load. */ mw.loader.load('//use.fontawesome.com/053a76b93c.js'); mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js'); 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 insertGPS(textarea, gpsString) { var text = textarea.value; if (text.indexOf("|GPSLOCATION=") !== -1) { // Replace existing GPSLOCATION textarea.value = text.replace(/(\|GPSLOCATION=)[^\n]*/, "$1" + gpsString); } else if (text.indexOf("{{Castle PhotoBatch") !== -1) { // Insert into template textarea.value = text.replace( /(\{\{Castle PhotoBatch[\s\S]*?)(\|[^=]*=)/, "$1|GPSLOCATION=" + gpsString + "\n$2" ); } else { console.log("⚠️ Could not find template to insert GPS."); } console.log("✅ Injected GPSLOCATION:", gpsString); } function attachGPSListener(input, textarea) { if (input.dataset.gpsListenerAttached) return; input.dataset.gpsListenerAttached = "true"; input.addEventListener('change', function (e) { var file = e.target.files[0]; if (!file || file.type.indexOf('image/') !== 0) return; EXIF.getData(file, function () { 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 && lon && latRef && lonRef)) { console.log("❌ No GPS EXIF found."); return; } var latDec = convertDMSToDecimal(lat, latRef); var lonDec = convertDMSToDecimal(lon, lonRef); insertGPS(textarea, latDec + ", " + lonDec); }); }); } var observer = new MutationObserver(function () { var fileInputs = document.querySelectorAll('input[type="file"].fileupload'); var textarea = document.querySelector('textarea[name="wfUploadDescription"]'); if (fileInputs.length && textarea) { fileInputs.forEach(function (input) { attachGPSListener(input, textarea); }); observer.disconnect(); } }); observer.observe(document.body, { childList: true, subtree: true });