User:Fumple/common.js

/* // Made by FunPL with slight help from google

// Utility functions

function resolveID(id){ var replaced = id.replaceAll(".26","&"); replaced = replaced.replaceAll(".21","!"); replaced = replaced.replaceAll(".28","(");   replaced = replaced.replaceAll(".29",")"); replaced = replaced.replaceAll(".CF.89","ω"); replaced = replaced.replaceAll(".7E","~"); replaced = replaced.replaceAll(".27","'"); replaced = replaced.replaceAll(".40","@"); replaced = replaced.replaceAll(".23","#"); replaced = replaced.replaceAll(".24","$"); replaced = replaced.replaceAll(".25","%"); return replaced; } String.prototype.replaceAll = function(search, replacement) { var target = this; return target.split(search).join(replacement); };

var firstParse = true;

function parseApi(parsetext, fn) { if (firstParse) { setTimeout(parseApi, 1000, parsetext, fn) setTimeout(function {           firstParse = false        }, 900) return }   new mw.Api.get({        action: 'parse',        text: parsetext,        title: mw.config.values.wgPageName,    }).done(function(data) {        const wikitext = data.parse.text['*'];        if (wikitext) {            fn(wikitext);        } else {            fn("")        }    }); }

function parseApiAdd(parsetext, add, fn) { if (firstParse) { setTimeout(parseApiAdd, 1000, parsetext, add, fn) setTimeout(function {           firstParse = false        }, 900) return }   new mw.Api.get({        action: 'parse',        text: parsetext,        title: mw.config.values.wgPageName,    }).done(function(data) {        const wikitext = data.parse.text['*'];        if (wikitext) {            fn(wikitext, add);        } else {            fn("", null)        }    }); }

// Audio players

// Initialize all load buttons

// Load/Unload var unloadedtext = "Load "; var loadels = document.getElementsByClassName("load-media"); var loadmediadata = {}; [].forEach.call(loadels, function(el) {   var nr = 0;    if(el.children[nr] == null){        el.innerHTML = "Error! Please report this"    }    if(el.children[nr].nodeName != "P"){        nr = 1;        if(el.children[nr] == null){            el.innerHTML = "Error! Please report this"        }    }    el.id = el.id + '|' + el.children[nr].children[1].innerHTML;    var loadname = el.children[nr].children[0].innerHTML;    if(loadname == ""){        loadname = "media"    }    loadmediadata[el.id] = loadname;    el.innerHTML = unloadedtext + el.id + unloadedtext2 + loadname + " ";    el.classList.add("load-media-group-"+loadname) });

// Load/Unload all var loadalltext = "Load all "; var unloadalltext = "Unload all "; var allels = document.getElementsByClassName("loadall-media"); var allels2 = document.getElementsByClassName("unloadall-media"); [].forEach.call(allels, function(el) {   var group = el.parentElement.classList[0].split("-")[3]    if(group == ""){        group = "media";    }    el.innerHTML = loadalltext + group + " files "; }); [].forEach.call(allels2, function(el) {   var group = el.parentElement.classList[0].split("-")[3]    if(group == ""){        group = "media";    }    el.innerHTML = unloadalltext + group + " files "; });

function loadmedia(text) { try { var loadname = loadmediadata[text.id]; if (text.innerHTML == unloadedtext + text.id + unloadedtext2 + loadname + " ") { text.innerHTML = "Wait "; var id = resolveID(text.id); console.log(text.id); console.log(id); parseApi("" + id + "", function(wikitext) {               text.innerHTML = "Unload "+loadname+" " + " " + wikitext;            }); } else { text.innerHTML = unloadedtext + text.id + unloadedtext2 + loadname + " "; }   } catch (error) { text.innerHTML = "Error "; console.error(error); } } function loadallmedia(allel) { try { if(allel.parentElement.parentElement.classList[0] != "loadall-media-group-"){ var group = allel.parentElement.parentElement.classList[0].split("-")[3]; var thisloadels = document.getElementsByClassName("load-media-group-"+group) }       else{ var thisloadels = loadels; }       [].forEach.call(thisloadels, function(el) {            loadforce(el)        }); } catch (error) { console.error(error); } }

function unloadallmedia(allel) { try { if(allel.parentElement.parentElement.classList[0] != "loadall-media-group-"){ var group = allel.parentElement.parentElement.classList[0].split("-")[3]; var thisloadels = document.getElementsByClassName("load-media-group-"+group) }       else{ var thisloadels = loadels; }       [].forEach.call(thisloadels, function(el) {            unloadforce(el)        }); } catch (error) { console.error(error); } }

function loadforce(text) { try { var loadname = loadmediadata[text.id]; if (text.innerHTML == unloadedtext + text.id + unloadedtext2 + loadname + " ") { text.innerHTML = "Wait "; var id = resolveID(text.id); console.log(text.id); console.log(id); parseApi("" + id + "", function(wikitext) {               text.innerHTML = "Unload "+loadname+" " + " " + wikitext;            }); }   } catch (error) { text.innerHTML = "Error "; console.error(error); } }

function unloadforce(text) { try { var loadname = loadmediadata[text.id]; if (text.innerHTML != unloadedtext + text.id + unloadedtext2 + loadname + " ") { text.innerHTML = unloadedtext + text.id + unloadedtext2 + loadname + " "; }   } catch (error) { text.innerHTML = "Error "; console.error(error); } }