Jump to content

MediaWiki:Common.js

From Costa Sano MediaWiki
Revision as of 14:41, 14 February 2026 by Mngr (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
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 });