MediaWiki:Common.js

Z Manta
Verze z 3. 1. 2025, 09:29, kterou vytvořil MantaUser (diskuse | příspěvky) (založena nová stránka s textem „→‎* * Fetch data from the API and display it on the MediaWiki page. * @param {string} apiUrl - The API endpoint to fetch data from. * @param {string} targetElementId - The ID of the element where the data will be displayed.: function fetchAndDisplayData(apiUrl, targetElementId) { fetch(apiUrl) .then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); }…“)
(rozdíl) ← Starší verze | zobrazit aktuální verzi (rozdíl) | Novější verze → (rozdíl)
Přejít na:navigace, hledání

Poznámka: Po zveřejnění musíte vyprázdnit cache vašeho prohlížeče, jinak změny neuvidíte.

  • Firefox / Safari: Při kliknutí na Aktualizovat držte Shift nebo stiskněte Ctrl-F5 nebo Ctrl-R (na Macu ⌘-R)
  • Google Chrome: Stiskněte Ctrl-Shift-R (na Macu ⌘-Shift-R)
  • Internet Explorer / Edge: Při kliknutí na Aktualizovat držte Ctrl nebo stiskněte Ctrl-F5
  • Opera: Stiskněte Ctrl-F5.
/**
 * Fetch data from the API and display it on the MediaWiki page.
 * @param {string} apiUrl - The API endpoint to fetch data from.
 * @param {string} targetElementId - The ID of the element where the data will be displayed.
 */
function fetchAndDisplayData(apiUrl, targetElementId) {
    fetch(apiUrl)
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.json();
        })
        .then(data => {
            // Format and display the data
            const resultElement = document.getElementById(targetElementId);
            if (!resultElement) {
                console.error(`Element with ID ${targetElementId} not found.`);
                return;
            }

            const formattedData = `
                <div>
                    <p><strong>Status:</strong> ${data.result.status}</p>
                    <p><strong>Message:</strong> ${data.result.message}</p>
                </div>
            `;
            resultElement.innerHTML = formattedData;
        })
        .catch(error => {
            console.error("Error fetching data:", error);
        });
}

// Expose the function to the global window object for use in MediaWiki pages
window.fetchAndDisplayData = fetchAndDisplayData;