MediaWiki:DynamicTemplates.js

var types = ["inlinesubst"]; var prefix = "js-special-div js-special-unprocessed";

var promises = [];

var locked = false; var lock_counter = 0;

function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); };

function randint(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random * (max - min + 1)) + min; };

function getElementsByClassName(array, class_) { var out = []; for (var itr = 0; itr < array.length; itr++) if (array[itr].classList.contains(class_)) out.push(array[itr]); return out; };

async function doit { var elements = get_unprocessed(document.body); while (elements.length) { doit_(elements); locked = true; lock_counter = 0; if (promises.length) Promise.all(promises).then(function {locked = false;}); promises = []; while (locked) { await sleep(10); lock_counter++; };       elements = get_unprocessed(document.body); }; };

function doit_(elements) { console.log(elements); types.forEach(function (type) {getElementsByClassName(elements, "js-special-type-" + type).forEach(eval("doit_" + type));}); };

function get_unprocessed(element) { var array = []; Array.from(element.getElementsByClassName(prefix)).forEach(function (item) {Array.prototype.push.apply(array, get_unprocessed(item));}); if (element.classList.contains("js-special-div") && element.classList.contains("js-special-unprocessed")) array.push(element); return array; };

function doit_inlinesubst(div, index, array) { var data = div.innerHTML; var restorevars = div.getAttribute("data-restorevars"); var revid = div.getAttribute("data-revid"); if (revid === "") revid = 1; else revid = parseInt(revid); var pagetitle = div.getAttribute("data-pagetitle"); var id = div.id; div.className = "js-special-div js-special-placeholder"; div.id = id.replace("unprocessed", "placeholder"); div.innerHTML = ""; div.removeAttribute("data-restorevars"); div.removeAttribute("data-revid"); div.removeAttribute("data-pagetitle"); div.style = ""; div.style.position = "absolute"; div.style.display = "none"; div.style.width = "0%"; div.style.height = "0%"; div.style.opacity = 0; div.style["z-index"] = 0; var open_template = ""; var wikitext = open_template + "subst:restorevars|" + restorevars + close_template + open_template + "#vardefine:__var_list|" + restorevars + close_template + open_template + "vardefine|seedcnt|" + randint(0, 65536).toString + close_template + $(" ").html(data).text.toString.replace(/\[escape:1\]/g, "").replace(/\[escape:(\d+)\]/g, function(_, value) {return "[escape:" + (parseInt(value) - 1).toString + "]";}); console.log(wikitext); var api = new mw.Api; promises.push(api.post({ "action": "parse", "format": "json", "title": pagetitle, "text": wikitext, "revid": revid, "pst": true, "disablelimitreport": false, "disableeditsection": true, "disablestylededuplication": false, "preview": true, "wrapoutputclass": "js-special-div js-special-processed-inner", "effectivelanglinks": true }).then(function (response) { var parsed = response.parse.text["*"]; var parsed_div = document.createElement("div"); parsed_div.innerHTML = parsed; parsed_div.className = "js-special-div js-special-processed js-special-type-inlinesubst"; parsed_div.id = id.replace("unprocessed", "processed"); div.parentNode.insertBefore(parsed_div, div.nextSibling); // document.write(JSON.stringify(response)); }).fail(function (error) {document.write("error: " + error + "\n\ndebug: title=" + pagetitle + ", revid=" + revid + ", text=" + wikitext);})); };

doit;