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'); | ||
// Load exif-js | // Load exif-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'); | ||
// Wait until both the file input and textarea are on the page | |||
(function waitForFormElements() { | |||
var fileInput = document.querySelector('input[type="file"]'); | |||
var textarea = document.querySelector('textarea[name="wfUploadDescription"]'); | |||
if (fileInput && textarea) { | |||
console.log("✅ File input and textarea detected. Setting up listener."); | |||
fileInput.addEventListener('change', function (e) { | |||
var | var file = e.target.files[0]; | ||
if (!file || file.type.indexOf('image/') !== 0) { | |||
console.log("⚠️ Not an image file or nothing selected."); | |||
return; | |||
} | |||
function convertDMSToDecimal(dms, ref) { | console.log("📷 File selected:", file.name); | ||
EXIF.getData(file, function () { | |||
console.log("🔍 EXIF loaded"); | |||
var lat = EXIF.getTag(this, "GPSLatitude"); | |||
var latRef = EXIF.getTag(this, "GPSLatitudeRef"); | |||
var lon = EXIF.getTag(this, "GPSLongitude"); | |||
var lonRef = EXIF.getTag(this, "GPSLongitudeRef"); | |||
console.log("📡 Raw GPS tags:", lat, latRef, lon, lonRef); | |||
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); | |||
} | |||
if (lat && lon && latRef && lonRef) { | |||
var latDec = convertDMSToDecimal(lat, latRef); | |||
var lonDec = convertDMSToDecimal(lon, lonRef); | |||
var gpsString = latDec + ", " + lonDec; | |||
console.log("✅ GPS Coordinates:", gpsString); | |||
var text = textarea.value; | var text = textarea.value; | ||
if (text.indexOf("| | if (text.indexOf("|GPSLOCATION=") !== -1) { | ||
textarea.value = text.replace(/(\| | textarea.value = text.replace(/(\|GPSLOCATION=)[^\n]*/, "$1" + gpsString); | ||
} else if (text.indexOf("{{Castle PhotoBatch") !== -1) { | } else if (text.indexOf("{{Castle PhotoBatch") !== -1) { | ||
textarea.value = text.replace( | textarea.value = text.replace( | ||
/(\{\{Castle PhotoBatch[\s\S]*?)(\|[^=]*=)/, | /(\{\{Castle PhotoBatch[\s\S]*?)(\|[^=]*=)/, | ||
"$1| | "$1|GPSLOCATION=" + gpsString + "\n$2" | ||
); | ); | ||
} else { | } else { | ||
textarea.value = | textarea.value = | ||
"{{Castle PhotoBatch\n|CASTLENAME=\n|SOURCE=\n|PHOTODATE=\n| | "{{Castle PhotoBatch\n|CASTLENAME=\n|SOURCE=\n|PHOTODATE=\n|GPSLOCATION=" + | ||
gpsString + | gpsString + "\n|OTHER=\n}}\n\n" + text; | ||
} | } | ||
console.log("✅ Inserted GPSLOCATION into template"); | |||
} else { | |||
console.log("❌ No GPS EXIF data found."); | |||
} | } | ||
} | }); | ||
}); | }); | ||
}); | } else { | ||
}); | // Retry after a short delay if inputs not yet ready | ||
setTimeout(waitForFormElements, 500); | |||
} | |||
})(); |
Revision as of 14:37, 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'); // Wait until both the file input and textarea are on the page (function waitForFormElements() { var fileInput = document.querySelector('input[type="file"]'); var textarea = document.querySelector('textarea[name="wfUploadDescription"]'); if (fileInput && textarea) { console.log("✅ File input and textarea detected. Setting up listener."); fileInput.addEventListener('change', function (e) { var file = e.target.files[0]; if (!file || file.type.indexOf('image/') !== 0) { console.log("⚠️ Not an image file or nothing selected."); return; } console.log("📷 File selected:", file.name); EXIF.getData(file, function () { console.log("🔍 EXIF loaded"); var lat = EXIF.getTag(this, "GPSLatitude"); var latRef = EXIF.getTag(this, "GPSLatitudeRef"); var lon = EXIF.getTag(this, "GPSLongitude"); var lonRef = EXIF.getTag(this, "GPSLongitudeRef"); console.log("📡 Raw GPS tags:", lat, latRef, lon, lonRef); 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); } if (lat && lon && latRef && lonRef) { var latDec = convertDMSToDecimal(lat, latRef); var lonDec = convertDMSToDecimal(lon, lonRef); var gpsString = latDec + ", " + lonDec; console.log("✅ GPS Coordinates:", 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("✅ Inserted GPSLOCATION into template"); } else { console.log("❌ No GPS EXIF data found."); } }); }); } else { // Retry after a short delay if inputs not yet ready setTimeout(waitForFormElements, 500); } })();