User:Bebiezaza/common.js

//loading JS and CSS locally // mw.loader.load('http://localhost/minecraftwikiskinedit/common.css','text/css'); // mw.loader.load('http://localhost/minecraftwikiskinedit/common.js','text/javascript');

/* -- Functions -- */ /** * Better createElement, with the ability to add child element and attributes as you create the element * * @param { String } nodename Required. The name of the element you want to create * @param { Array } child Child elements you want to put inside this element * @param { Object } attributes Styles, Classes, IDs, etc * @returns HTML Node */ function createElement(nodename, child, attributes) { var element = document.createElement(nodename); if (!(child instanceof Array)) child = [child];

for (var i = 0; i < child.length; i++) { var child_members = child[i]; if (typeof child_members == 'string') child_members = document.createTextNode(child_members); if (child_members) element.appendChild(child_members); }

if (typeof attributes == 'object') { for (var k in attributes) { switch (k) { case 'class': element.className = attributes[k]; break; default: element.setAttribute(k, attributes[k]); }       }    }    return element; }

/** * This function moves Fandom topnav to original Mediawiki leftnav * * @param { Number } element_no Required. The number of the element, starting 1 at the menu after "Explore" * @param { String } header_name Required. The name you want the header of the section to appear * @returns HTML Node */ function makeSidebar(element_no, header_name) { var header_name_squashed = header_name.replace(/\ /g, ""); var fandomtop = document.getElementsByClassName("wds-tabs")[0].getElementsByClassName("wds-dropdown")[element_no].childNodes[3].firstElementChild; var sidebar_count = fandomtop.childElementCount; var sidebar = createElement('ul', "", {       'class': "vector-menu-content-list"    }); var sidebar_header = createElement('h3', "", { 'id': "p-" + header_name_squashed + "-label" }); sidebar_header.innerHTML = header_name;

for (var i = 0; i < sidebar_count; i++) { var count = i+1; sidebar.appendChild(fandomtop.childNodes[count]); }

var sidebar_div = createElement('div', sidebar, {       'class': "vector-menu-content"    });

var sidebar_div_top = createElement('div', [sidebar_header, sidebar_div], {       'id': "p-" + header_name_squashed,        'role': "navigation",        'class': "sidebar-header vector-menu-portal",        'aria-labelledby': "p-" + header_name_squashed + "-label"    });

return sidebar_div_top; }

/* -- Functions end -- */

/* -- Fandom nav manipulation -- */ // remove Fandom left navbar topic elements var nav_bar = document.querySelector(".global-navigation__top nav"); nav_bar.removeChild(nav_bar.childNodes[5]);

// remove Fandom brand text var nav_brand = document.querySelector(".global-navigation__nav a"); nav_brand.removeChild(nav_brand.lastElementChild);

/* -- Remove popup Fandom main header -- */ document.getElementsByClassName("fandom-sticky-header")[0].remove;

/* -- Recreate mw-panel left navigation -- */ function mwPanelRecreator { var logo = createElement('div', "", {       'id': "p-logo",        'role': "banner"    }); logo.appendChild(document.getElementsByClassName("fandom-community-header__image")[0]);

var panel_div = createElement('div', logo, {       'id': "mw-panel"    });

// move fandom topnav to normal leftnav panel_div.appendChild(makeSidebar(1, "Minecraft Wiki")); panel_div.appendChild(makeSidebar(2, "Games")); panel_div.appendChild(makeSidebar(3, "Useful Pages")); panel_div.appendChild(makeSidebar(4, "Minecraft Links")); panel_div.appendChild(makeSidebar(5, "Gamepedia")); if(mw.config.get("wgAction") == "view" && !mw.config.get("wgIsMainPage")) { // move page tools back to leftnav var toolbox = createElement('ul', "", {           'class': "vector-menu-content-list"        }); var toolbox_div = createElement('div', toolbox, {           'class': "vector-menu-content"        }); var toolbox_header = createElement('h3', "", { 'id': "p-tb-label" }); toolbox_header.innerHTML = "Page Tools";

var toolbox_div_top = createElement('div', [toolbox_header, toolbox_div], {           'id': "p-tb",            'role': "navigation",            'class': "sidebar-header vector-menu-portal",            'aria-labelledby': "p-tb-label"        }); panel_div.appendChild(toolbox_div_top); setTimeout(function { // wait for page tools to load bruh           var fandom_tb = document.getElementById("p-tb").lastElementChild;            if (fandom_tb) {                var fandom_tb_count = fandom_tb.childElementCount;

for (var i = 0; i < fandom_tb_count; i++) { var count = i+1; toolbox.appendChild(fandom_tb.childNodes[count]); }           }        }, 1000);    }

// move languages back to leftnav if (document.getElementsByClassName("page-header__languages")[0]) { var fandom_lang = document.getElementsByClassName("page-header__languages")[0].lastElementChild.lastElementChild.lastElementChild; var fandom_lang_count = fandom_lang.childElementCount; var lang = createElement('ul', "", {           'class': "vector-menu-content-list"        }); var lang_header = createElement('h3', "", { 'id': "p-lang-label" }); lang_header.innerHTML = "Languages"; for (var i = 0; i < fandom_lang_count; i++) { var count = i+1; lang.appendChild(fandom_lang.childNodes[count]); }       var lang_div = createElement('div', lang, {            'class': "vector-menu-content"        }); var lang_div_top = createElement('div', [lang_header, lang_div], {           'id': "p-lang",            'role': "navigation",            'class': "sidebar-header vector-menu-portal",            'aria-labelledby': "p-lang-label"        }); panel_div.appendChild(lang_div_top); }

return document.body.appendChild(panel_div); }

/* -- Move Fandom rightnav ad to end of page -- */ function fandomADmove { if(mw.config.get("wgAction") == "view" && !mw.config.get("wgIsMainPage")) document.getElementsByClassName("resizable-container")[0].appendChild(document.getElementById("rail-boxad-wrapper")); }

/* -- Alters editsection button -- */ function editsectionButtonAlter { if(mw.config.get("wgAction") == "view") { var editbuttons = document.getElementsByClassName("mw-editsection"); var editbuttons_count = editbuttons.length; for (var i = 0; i < editbuttons_count; i++) { if (!editbuttons[i].childNodes[1].firstChild) { editbuttons[i].childNodes[2].remove; editbuttons[i].childNodes[2].innerHTML = "edit"; } else editbuttons[i].childNodes[1].innerHTML = "edit"; }   } }

/* -- Remove Fandom fluffs -- */ function fandomFluffsRemover { document.getElementsByClassName("community-header-wrapper")[0].remove; // remove Fandom main header document.getElementsByClassName("page-side-tools__wrapper")[0].remove; // remove page side tools if(mw.config.get("wgAction") == "view") { document.getElementsByClassName("page-header__top")[0].remove; // remove Fandom meta & language header if(!mw.config.get("wgIsMainPage")) { if(mw.config.get("wgNamespaceNumber") == (0 || 1)) { document.getElementById("toc").childNodes[1].firstChild.firstChild.remove; // remove ToC svg }

setTimeout(function { // wait for page tools to load bruh               document.getElementsByClassName("page__right-rail")[0].remove; // remove Fandom rightnav            }, 1100); }   } }

/* -- Runtime combiner -- */ function runScript { mwPanelRecreator; fandomADmove; editsectionButtonAlter; fandomFluffsRemover; console.log("JS end"); }

/* -- Central Executor -- */ $(document).ready(function {   console.log("JS start");    runScript; });