MediaWiki:Common.js: Porovnání verzí
Z Manta
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}`); }…“ |
Bez shrnutí editace |
||
| Řádek 1: | Řádek 1: | ||
/** | /** | ||
* Fetch data from the API and display it | * Automatically fetch and display API data when specific elements are present on a page. | ||
*/ | |||
$(document).ready(function () { | |||
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} apiUrl - The API endpoint to fetch data from. | ||
* @param {string} targetElementId - The ID of the element where the data will be displayed. | * @param {string} targetElementId - The ID of the element where the data will be displayed. | ||
| Řádek 13: | Řádek 26: | ||
}) | }) | ||
.then(data => { | .then(data => { | ||
const resultElement = document.getElementById(targetElementId); | const resultElement = document.getElementById(targetElementId); | ||
if (!resultElement) { | if (!resultElement) { | ||
| Řádek 32: | Řádek 44: | ||
}); | }); | ||
} | } | ||
Verze z 3. 1. 2025, 09:31
/**
* Automatically fetch and display API data when specific elements are present on a page.
*/
$(document).ready(function () {
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><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);
});
}