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 for reading EXIF metadata | ||
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'); | ||
$(document).ready(function () { | |||
$(document).on('change', 'input[type="file"]', 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"); | |||
function convertDMSToDecimal(dms, ref) { | |||
var deg = dms[0]; | |||
var min = dms[1]; | |||
var 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; | |||
var textarea = document.querySelector('textarea[name="description"]'); | |||
if (textarea) { | |||
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; | |||
} | } | ||
} | } | ||
}); | } else { | ||
console.log("No GPS EXIF data found."); | |||
} | |||
}); | }); | ||
}); | }); | ||
} | }); |
Revision as of 14:20, 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 for reading EXIF metadata mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.min.js'); $(document).ready(function () { $(document).on('change', 'input[type="file"]', 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"); function convertDMSToDecimal(dms, ref) { var deg = dms[0]; var min = dms[1]; var 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; var textarea = document.querySelector('textarea[name="description"]'); if (textarea) { 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; } } } else { console.log("No GPS EXIF data found."); } }); }); });