User:Fumple/common.js

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); }; //Had to do a one line string due to MediaWiki JS restrictions var contextmenu = " File not set Open file page Open file page in new tab "; //I know I could avoid some lines but I don't really want to change how it is var el = document.createElement("div"); el.innerHTML = contextmenu; document.body.append(el); $(".load-media").contextmenu(function (e) {   if (e.target.tagName != "DIV") {        return;    }    $(".-context-menu").css("display", "block")        .css("top", e.pageY)        .css("left", e.pageX);    if (e.target.id == "") {        $(".-context-menu-media-load-file").text(resolveID(e.target.parentElement.id.split("|")[0]));    }    else {        $(".-context-menu-media-load-file").text(resolveID(e.target.id.split("|")[0]));    }    e.preventDefault;    return false; }); $(document).click(function (e) {   var t = $(e.target);    if (t.hasClass("-context-menu-text") || t.hasClass("-context-menu-item")) {        return;    }    $(".-context-menu").css("display", "none") }) $(document).contextmenu(function (e) {   $(".-context-menu").css("display", "none") }) $(".-context-menu-media-load-open").click(function (e) { window.location = "/wiki/" + e.target.previousSibling.innerHTML }) $(".-context-menu-media-load-tab").click(function (e) { window.open("/wiki/" + e.target.previousSibling.previousSibling.innerHTML) })

//Page-specific script var encodedPage = encodeURIComponent(mw.config.get("wgPageName")); var encodedUser = encodeURIComponent(mw.config.get("wgUserName") || "");

//Check page specific files onApiReady(function {    //Check for JS    new mw.Api.get({ action: 'parse', page: 'MediaWiki:Common.js/' + mw.config.get("wgPageName"), prop: "" }).done(function (data) { afterJS(true) }).fail(function (data) { afterJS(false) }); });

function afterJS(exist) { if (exist) { mw.loader.load('/w/index.php?title=MediaWiki:Common.js/' + encodedPage + '&action=raw&ctype=text/javascript'); $("#p-namespaces ul").append(' Page JS '); }   else { $("#p-namespaces ul").append(' Page JS '); }   new mw.Api.get({        action: 'parse',        page: 'MediaWiki:Common.css/' + mw.config.get("wgPageName"),        prop: ""    }).done(function (data) {        afterCSS(true);    }).fail(function (data) {        afterCSS(false);    }); }

function afterCSS(exist) { if (exist) { mw.loader.load('/w/index.php?title=MediaWiki:Common.css/' + encodedPage + '&action=raw&ctype=text/css', 'text/css'); $("#p-namespaces ul").append(' Page CSS '); }   else { $("#p-namespaces ul").append(' Page CSS </li>'); }   if (mw.config.get("wgUserName") != null) { new mw.Api.get({           action: 'parse',            page: 'User:' + mw.config.get("wgUserName") + '/common.js/' + mw.config.get("wgPageName"),            prop: ""        }).done(function (data) {            afterUserJS(true)        }).fail(function (data) {            afterUserJS(false)        }); } }

function afterUserJS(exist) { if (exist) { mw.loader.load('/w/index.php?title=User:' + encodedUser + '/common.js/' + encodedPage + '&action=raw&ctype=text/javascript'); }   new mw.Api.get({        action: 'parse',        page: 'User:' + mw.config.get("wgUserName") + '/common.css/' + mw.config.get("wgPageName"),        prop: ""    }).done(function (data) {        afterUserCSS(true)    }).fail(function (data) {        afterUserCSS(false)    }) }

function afterUserCSS(exist) { if (exist) { mw.loader.load('/w/index.php?title=User:' + encodedUser + '/common.css/' + encodedPage + '&action=raw&ctype=text/javascript'); } }

function onApiReady(callback) { if (mw.Api != null) { setTimeout(callback, 0); } else { setTimeout(onApiReady.bind(null, callback), 0); } }