MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 631: | Строка 631: | ||
var ajaxElements = document.querySelectorAll('.ajax-load-link'); | var ajaxElements = document.querySelectorAll('.ajax-load-link'); | ||
var BATCH_SIZE = 100; | |||
var queue = []; | var queue = []; | ||
var processing = false; | var processing = false; | ||
| Строка 663: | Строка 663: | ||
try { | try { | ||
$.globalEval(scr.textContent || scr.innerText || ""); | $.globalEval(scr.textContent || scr.innerText || ""); | ||
} catch (e) { | } catch (e) {} | ||
parent.removeChild(scr); | parent.removeChild(scr); | ||
} | } | ||
| Строка 676: | Строка 674: | ||
mw.hook('wikipage.content').fire(newContainer); | mw.hook('wikipage.content').fire(newContainer); | ||
mw.loader.load('//station14.ru/w/index.php?title=MediaWiki:Common.js&action=raw&ctype=text/javascript'); | mw.loader.load('//station14.ru/w/index.php?title=MediaWiki:Common.js&action=raw&ctype=text/javascript'); | ||
} | } | ||
function processQueue() { | |||
if (processing) return; | if (processing) return; | ||
processing = true; | processing = true; | ||
function nextBatch() { | |||
if (queue.length === 0) { | |||
processing = false; | |||
return; | |||
} | |||
var batch = queue.splice(0, BATCH_SIZE); | var batch = queue.splice(0, BATCH_SIZE); | ||
var | var remaining = batch.length; | ||
batch.forEach(function (job) { | |||
fetchParsedData(job.wikiText) | |||
.done(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 не вернул ожидаемых данных."; | |||
} | |||
}) | |||
.fail(function () { | |||
if (job.placeholder) job.placeholder.textContent = "Ошибка при выполнении запроса к API."; | |||
}) | |||
.always(function () { | |||
remaining--; | |||
if (remaining === 0) { | |||
nextBatch(); | |||
} | |||
}); | |||
}); | }); | ||
} | } | ||
nextBatch(); | |||
} | } | ||
| Строка 710: | Строка 719: | ||
if (!content.closest('.ajax-load-link')) { | if (!content.closest('.ajax-load-link')) { | ||
var wikiText = content ? (content.textContent || content.innerText) : null; | var wikiText = content ? (content.textContent || content.innerText) : null; | ||
if (!wikiText) | if (!wikiText) return; | ||
var placeholder = document.createElement("div"); | var placeholder = document.createElement("div"); | ||
| Строка 725: | Строка 731: | ||
ajaxElements.forEach(function (element) { | ajaxElements.forEach(function (element) { | ||
element.addEventListener('click', function () { | element.addEventListener('click', function () { | ||
var content = element.querySelector('.ajax-load-content'); | var content = element.querySelector('.ajax-load-content'); | ||
var wikiText = content ? (content.textContent || content.innerText) : null; | var wikiText = content ? (content.textContent || content.innerText) : null; | ||
if (!wikiText) return; | |||
if (!wikiText) | |||
var placeholder = document.createElement("div"); | var placeholder = document.createElement("div"); | ||
| Строка 739: | Строка 740: | ||
queue.push({ wikiText: wikiText, placeholder: placeholder }); | queue.push({ wikiText: wikiText, placeholder: placeholder }); | ||
processQueue(); | processQueue(); | ||
}); | }); | ||