User:Fumple/common.js

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

/* Utility functions */

function getImagesByAlt(alt) { var allImages = document.getElementsByTagName("img"); var images = []; for (var i = 0, len = allImages.length; i < len; ++i) { if (allImages[i].alt == alt) { images.push(allImages[i]); }   }    return images; }

String.prototype.replaceAll = function(search, replacement) { var target = this; return target.split(search).join(replacement); };

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; }

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

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

function loadaudio(text){ try{ var api = new mediaWiki.Api; if(text.innerHTML == unloadedtext+text.id+unloadedtext2){ text.innerHTML = "Wait "; var id = resolveID(text.id); console.log(text.id); console.log(id); parseApi(""+id+"", function (wikitext) {               text.innerHTML = "Unload audio "+" "+wikitext;            }); }       else{ text.innerHTML = unloadedtext+text.id+unloadedtext2; }   }catch(error){ text.innerHTML = "Error "; console.error(error); } }

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

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

function load(text){ try{ var api = new mediaWiki.Api; if(text.innerHTML == unloadedtext+text.id+unloadedtext2){ text.innerHTML = "Wait "; var id = resolveID(text.id); console.log(text.id); console.log(id); parseApi(""+id+"", function (wikitext) {               text.innerHTML = "Unload audio "+" "+wikitext;            }); }   }catch(error){ text.innerHTML = "Error "; console.error(error); } }

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

/* Interactive Deepsea Metro Map */

try{ var maps = getImagesByAlt("DeepseaMetroMap");

var stationshow = document.getElementById("cstation");

if(maps.length > 0){ if(maps.length > 1){ //Support multiple maps on one page }   else{ var map = getImagesByAlt("DeepseaMetroMap")[0]; var imgmapid = map.getAttribute("usemap").replace("#", ""); console.log(imgmapid); var imgmap = document.getElementsByName(imgmapid)[0]; for(var i = 0, len = imgmap.children.length;i<len;i++){ var area = imgmap.children[i]; if(area.tagName == "AREA"){ if(area.alt == "A??"){var alt = "Abandoned Station"}else{var alt = area.alt} area.onmouseover = hover; area.onmouseout = unhover; parseApiAdd(""+alt+"", area, function (wikitext, add) {                   var elements = document.createRange.createContextualFragment(wikitext);                    add.href = elements.children[0].children[0].children[0].href;                }); }       }    } }

function hover(station){ parseApi(" ", function (wikitext) {   cstation.innerHTML = wikitext; }); }

function unhover{ parseApi(" ", function (wikitext) {   cstation.innerHTML = wikitext; }); }

var loadingtext = document.getElementsByClassName("loadingmap"); for(var i = 0, len = loadingtext.length;i<len;i++){ loadingtext[i].innerHTML = ""; }

}catch(err){ console.log(err); var loadingtext = document.getElementsByClassName("loadingmap"); for(var i = 0, len = loadingtext.length;i<len;i++){ loadingtext[i].innerHTML = "Something went wrong while loading the map! Try refreshing the page."; } }