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

/* 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); new mw.Api.get({               action: 'parse',                text: ""+id+"",                title: mw.config.values.wgPageName,            }).done(function (data) {                const wikitext = data.parse.text['*'];                if (wikitext)                    text.innerHTML = "Unload audio "+" "+wikitext;            }); }       else{ text.innerHTML = unloadedtext+text.id+unloadedtext2; }   }catch(error){ text.innerText = "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); new mw.Api.get({               action: 'parse',                text: ""+id+"",                title: mw.config.values.wgPageName,            }).done(function (data) {                const wikitext = data.parse.text['*'];                if (wikitext)                    text.innerHTML = "Unload audio "+" "+wikitext;            }); }   }catch(error){ text.innerText = "Error "; console.error(error); } }

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

/* Interactive Deepsea Metro Map */ /* var maps = getImagesByAlt("DeepseaMetroMap");

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"){ area.href = "javascript:alert"; }       }    } } var $popup = $('.popup'); $('area').on({ mouseover : function(e){    var $this = $(this),        $obj = $('#'+$this.prop('alt'));    $popup.text($obj.text).css({ top: e.pageY + 25, left: e.pageX - ($popup.width/2), }).show; },  mouseout: function{    var $this = $(this),        $obj = $('#'+$this.prop('alt'));              $popup.hide(0).empty;  } });