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 media "; var loadels = document.getElementsByClassName("load-media"); [].forEach.call(loadels, function(el) {   el.innerHTML = unloadedtext + el.id + unloadedtext2; });

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

function loadmedia(text) { try { if (text.innerHTML == unloadedtext + text.id + unloadedtext2) { text.innerHTML = "Wait "; var id = resolveID(text.id); console.log(text.id); console.log(id); if(id.endsWith('mp4')){ id = id + "|500px" }           parseApi("" + id + "", function(wikitext) {                text.innerHTML = "Unload media " + " " + wikitext;            }); } else { text.innerHTML = unloadedtext + text.id + unloadedtext2; }   } catch (error) { text.innerHTML = "Error "; console.error(error); } } function loadallmedia { try { [].forEach.call(loadels, function(el) {           loadforce(el)        }); } catch (error) { console.error(error); } }

function unloadallmedia { try { [].forEach.call(loadels, function(el) {           unloadforce(el)        }); } catch (error) { console.error(error); } }

function loadforce(text) { try { if (text.innerHTML == unloadedtext + text.id + unloadedtext2) { text.innerHTML = "Wait "; var id = resolveID(text.id); console.log(text.id); console.log(id); if(id.endsWith('mp4')){ id = id + "|500px" }           parseApi("" + id + "", function(wikitext) {                text.innerHTML = "Unload audio  " + wikitext;            }); }   } catch (error) { text.innerHTML = "Error "; console.error(error); } }

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