MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
console.log("Common.js loaded"); | console.log("Common.js loaded"); | ||
// | // Watch for dynamically inserted form fields | ||
const observer = new MutationObserver(() => { | |||
const addr = document.querySelector('input[name="address"]'); | |||
if (addr) { | |||
console.log("Address field detected!"); | |||
// Stop observing once found | |||
observer.disconnect(); | |||
const lat = document.querySelector('input[name="latitude"]'); | |||
const lon = document.querySelector('input[name="longitude"]'); | |||
// Attach geocoding | |||
addr.addEventListener('change', () => { | |||
const q = encodeURIComponent(addr.value); | |||
if (!q) return; | |||
const url = 'https://nominatim.openstreetmap.org/search?format=json&q=' + q; | |||
fetch(url) | |||
.then(r => r.json()) | |||
.then(data => { | |||
if (data && data.length > 0) { | |||
lat.value = data[0].lat; | |||
lon.value = data[0].lon; | |||
} | |||
}); | |||
}); | }); | ||
}); | } | ||
}; | }); | ||
// Start observing the whole document | |||
observer.observe(document.body, { childList: true, subtree: true }); | |||
Revision as of 14:41, 14 February 2026
console.log("Common.js loaded");
// Watch for dynamically inserted form fields
const observer = new MutationObserver(() => {
const addr = document.querySelector('input[name="address"]');
if (addr) {
console.log("Address field detected!");
// Stop observing once found
observer.disconnect();
const lat = document.querySelector('input[name="latitude"]');
const lon = document.querySelector('input[name="longitude"]');
// Attach geocoding
addr.addEventListener('change', () => {
const q = encodeURIComponent(addr.value);
if (!q) return;
const url = 'https://nominatim.openstreetmap.org/search?format=json&q=' + q;
fetch(url)
.then(r => r.json())
.then(data => {
if (data && data.length > 0) {
lat.value = data[0].lat;
lon.value = data[0].lon;
}
});
});
}
});
// Start observing the whole document
observer.observe(document.body, { childList: true, subtree: true });