MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 631: | Строка 631: | ||
var ajaxElements = document.querySelectorAll('.ajax-load-link'); | var ajaxElements = document.querySelectorAll('.ajax-load-link'); | ||
function | const BATCH_SIZE = 100; | ||
var queue = []; | |||
var processing = false; | |||
function fetchParsedData(wikiText) { | |||
var apiUrl = "https://station14.ru/api.php?action=parse&format=json&prop=text&text=" + encodeURIComponent(wikiText) + "&origin=*"; | |||
return $.ajax({ | |||
url: apiUrl, | |||
method: "GET", | |||
dataType: "json" | |||
}); | |||
} | |||
function applyParsedHTML(parsedHTML, placeholder) { | |||
var newContainer = document.createElement("div"); | |||
newContainer.innerHTML = parsedHTML; | |||
if (placeholder && placeholder.parentNode) { | |||
placeholder.replaceWith(newContainer); | |||
} | } | ||
var | 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'); | |||
} | |||
async function processQueue() { | |||
if (processing) return; | |||
processing = true; | |||
while (queue.length > 0) { | |||
var batch = queue.splice(0, BATCH_SIZE); | |||
var promises = batch.map(function (job) { | |||
return fetchParsedData(job.wikiText).then(function (data) { | |||
if (data.parse && data.parse.text) { | |||
var parsedHTML = data.parse.text["*"] || ""; | |||
applyParsedHTML(parsedHTML, job.placeholder); | |||
} else { | |||
if (job.placeholder) job.placeholder.textContent = "API не вернул ожидаемых данных."; | |||
console.warn("Ответ API:", data); | |||
} | |||
}).catch(function () { | |||
if (job.placeholder) job.placeholder.textContent = "Ошибка при выполнении запроса к API."; | |||
}); | |||
}); | |||
await Promise.allSettled(promises); | |||
} | |||
processing = false; | |||
} | } | ||
| Строка 707: | Строка 719: | ||
content.replaceWith(placeholder); | content.replaceWith(placeholder); | ||
queue.push({ wikiText: wikiText, placeholder: placeholder }); | |||
} | } | ||
}); | }); | ||
| Строка 726: | Строка 738: | ||
element.replaceWith(placeholder); | element.replaceWith(placeholder); | ||
queue.push({ wikiText: wikiText, placeholder: placeholder }); | |||
processQueue(); | |||
}); | }); | ||
}); | }); | ||
if (queue.length > 0) processQueue(); | |||
} | } | ||
const currentPageTitle = document.title; | const currentPageTitle = document.title; | ||