MediaWiki:Common.js

Z Manta
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.
/**
 * Automatically fetch and display API data when specific elements are present on a page.
 */
$(document).ready(function () {
	
	 if (mw.config.get("wgPageName") === "Hlavní_strana") {
        const tocSidebar = document.getElementById("toc-sidebar");
        if (tocSidebar) {
            tocSidebar.style.display = "none";
        }
    }
    
    const apiContainerId = "api-status-container";

    // Check if the target container exists on the page
    if (document.getElementById(apiContainerId)) {
        // Fetch and display the API data
        fetchAndDisplayData('http://manta.nvsp.cz/hub/prod/api/status', apiContainerId);
    }
});

/**
 * Fetch data from the API and display it.
 * @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 => {
            const resultElement = document.getElementById(targetElementId);
            if (!resultElement) {
                console.error(`Element with ID ${targetElementId} not found.`);
                return;
            }

            const formattedData = `
                <div>
                    <p>${JSON.stringify(data)}</p>
                </div>
            `;
            resultElement.innerHTML = formattedData;
        })
        .catch(error => {
            console.error("Error fetching data:", error);
        });
}