Jump to content

MediaWiki:Common.js: Difference between revisions

From Costa Sano MediaWiki
No edit summary
No edit summary
Line 1: Line 1:
console.log("Common.js loaded");
console.log("Common.js loaded");


// Page Forms hook: runs AFTER the form is inserted into the page
// Watch for dynamically inserted form fields
window.pfOnFormLoad = function() {
const observer = new MutationObserver(() => {
    console.log("pfOnFormLoad fired");
    const addr = document.querySelector('input[name="address"]');
    if (addr) {
        console.log("Address field detected!");


    const addr = $('input[name="address"]');
        // Stop observing once found
    const lat  = $('input[name="latitude"]');
        observer.disconnect();
    const lon  = $('input[name="longitude"]');


    console.log("Address fields found:", addr.length);
        const lat = document.querySelector('input[name="latitude"]');
        const lon = document.querySelector('input[name="longitude"]');


    addr.on('change', function() {
        // Attach geocoding
        const q = encodeURIComponent(addr.val());
        addr.addEventListener('change', () => {
        if (!q) return;
            const q = encodeURIComponent(addr.value);
            if (!q) return;


        const url = 'https://nominatim.openstreetmap.org/search?format=json&q=' + q;
            const url = 'https://nominatim.openstreetmap.org/search?format=json&q=' + q;


        $.getJSON(url, function(data) {
            fetch(url)
            if (data && data.length > 0) {
                .then(r => r.json())
                lat.val(data[0].lat);
                .then(data => {
                lon.val(data[0].lon);
                    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 });