MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) м port MediaWiki:Common.js from ruwiki |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 627: | Строка 627: | ||
document.querySelectorAll('.z-index-position').forEach(function(domEl) { | document.querySelectorAll('.z-index-position').forEach(function(domEl) { | ||
domEl.style.zIndex = z_index--; | domEl.style.zIndex = z_index--; | ||
}); | |||
} | |||
// Для "Шаблон:Ajax" | |||
function initAjaxLoader() { | |||
const ajaxElements = document.querySelectorAll('.ajax-load-link'); | |||
ajaxElements.forEach(function(element) { | |||
element.addEventListener('click', function() { | |||
const wikiText = element.getAttribute('id'); | |||
if (!wikiText) { | |||
console.warn("У элемента отсутствует атрибут id с вики-разметкой."); | |||
return; | |||
} | |||
const placeholder = document.createElement("div"); | |||
placeholder.textContent = "Пожалуйста, подождите, содержимое загружается..."; | |||
element.replaceWith(placeholder); | |||
const apiUrl = `https://station14.ru/api.php?action=parse&format=json&prop=text&text=${encodeURIComponent(wikiText)}&origin=*`; | |||
GM_xmlhttpRequest({ | |||
method: "GET", | |||
url: apiUrl, | |||
onload: function(response) { | |||
try { | |||
const data = JSON.parse(response.responseText); | |||
if (data.parse && data.parse.text) { | |||
const parsedHTML = data.parse.text["*"] || ""; | |||
const newContainer = document.createElement("div"); | |||
newContainer.innerHTML = parsedHTML; | |||
placeholder.replaceWith(newContainer); | |||
} else { | |||
placeholder.textContent = "API не вернул ожидаемых данных."; | |||
console.warn("Ответ API:", data); | |||
} | |||
} catch (err) { | |||
placeholder.textContent = "Ошибка при разборе ответа."; | |||
console.error("Ошибка JSON.parse:", err); | |||
} | |||
}, | |||
onerror: function() { | |||
placeholder.textContent = "Ошибка при выполнении запроса к API."; | |||
} | |||
}); | |||
}); | |||
}); | }); | ||
} | } | ||
| Строка 721: | Строка 766: | ||
if (layerIndexLoad.length > 0) { | if (layerIndexLoad.length > 0) { | ||
layerIndex(); | layerIndex(); | ||
} | |||
if (document.querySelectorAll('.ajax-load-link').length > 0) { | |||
initAjaxLoader(); | |||
} | } | ||
}); | }); | ||