MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 630: | Строка 630: | ||
function initAjaxLoader() { | function initAjaxLoader() { | ||
var ajaxElements = document.querySelectorAll('.ajax-load-link'); | var ajaxElements = document.querySelectorAll('.ajax-load-link'); | ||
function parseWikiText(wikiText, replaceNode) { | |||
if (!wikiText) { | |||
console.warn("Отсутствует вики-разметка для парсинга."); | |||
return; | |||
} | |||
var placeholder = replaceNode || document.createElement("div"); | |||
if (!replaceNode) { | |||
placeholder.textContent = "Пожалуйста, подождите, содержимое загружается..."; | |||
} | |||
var apiUrl = "https://station14.ru/api.php?action=parse&format=json&prop=text&text=" + encodeURIComponent(wikiText) + "&origin=*"; | |||
$.ajax({ | |||
url: apiUrl, | |||
method: "GET", | |||
dataType: "json", | |||
success: function (data) { | |||
if (data.parse && data.parse.text) { | |||
var parsedHTML = data.parse.text["*"] || ""; | |||
var newContainer = document.createElement("div"); | |||
newContainer.innerHTML = parsedHTML; | |||
if (replaceNode && replaceNode.parentNode) replaceNode.replaceWith(newContainer); | |||
var scripts = Array.prototype.slice.call(newContainer.querySelectorAll("script")); | |||
scripts.forEach(function (scr) { | |||
var parent = scr.parentNode; | |||
var scriptNode = document.createElement("script"); | |||
if (scr.src) { | |||
scriptNode.src = scr.src; | |||
if (scr.defer) scriptNode.defer = true; | |||
if (scr.async) scriptNode.async = true; | |||
parent.replaceChild(scriptNode, scr); | |||
} else { | |||
try { | |||
$.globalEval(scr.textContent || scr.innerText || ""); | |||
} catch (e) { | |||
console.warn("Ошибка при исполнении inline-скрипта:", e); | |||
} | |||
parent.removeChild(scr); | |||
} | |||
}); | |||
mw.loader.using(['jquery.tablesorter', 'jquery.makeCollapsible'], function () { | |||
$(newContainer).find('table.sortable').tablesorter(); | |||
$(newContainer).find('.mw-collapsible').makeCollapsible(); | |||
}); | |||
mw.hook('wikipage.content').fire(newContainer); | |||
mw.loader.load('//station14.ru/w/index.php?title=MediaWiki:Common.js&action=raw&ctype=text/javascript'); | |||
} else { | |||
if (replaceNode) replaceNode.textContent = "API не вернул ожидаемых данных."; | |||
console.warn("Ответ API:", data); | |||
} | |||
}, | |||
error: function () { | |||
if (replaceNode) replaceNode.textContent = "Ошибка при выполнении запроса к API."; | |||
} | |||
}); | |||
} | |||
var standaloneContents = document.querySelectorAll('.ajax-load-content'); | |||
standaloneContents.forEach(function (content) { | |||
if (!content.closest('.ajax-load-link')) { | |||
var wikiText = content ? (content.textContent || content.innerText) : null; | |||
if (!wikiText) { | |||
console.warn("У элемента отсутствует .ajax-load-content с вики-разметкой."); | |||
return; | |||
} | |||
var placeholder = document.createElement("div"); | |||
placeholder.textContent = "Пожалуйста, подождите, содержимое загружается..."; | |||
content.replaceWith(placeholder); | |||
parseWikiText(wikiText, placeholder); | |||
} | |||
}); | |||
ajaxElements.forEach(function (element) { | ajaxElements.forEach(function (element) { | ||
| Строка 646: | Строка 726: | ||
element.replaceWith(placeholder); | element.replaceWith(placeholder); | ||
parseWikiText(wikiText, placeholder); | |||
}); | }); | ||
}); | }); | ||