User:Bebiezaza/vectorDesktop.js

/* -- 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-head top navigation -- */ function mwHeadRecreator { var head_div = createElement('div', "", {       'id': "mw-head"    }); document.body.appendChild(head_div);

/* left navigation */ var pageLink, pageLink_talk; if (mw.config.get('wgNamespaceNumber') % 2 == 0) { pageLink = mw.config.get('wgPageName'); if (mw.config.get('wgNamespaceNumber') == 0) pageLink_talk = "Talk:" + mw.config.get('wgTitle'); else pageLink_talk = mw.config.get('wgCanonicalNamespace') + " talk:" + mw.config.get('wgTitle'); } else if (mw.config.get('wgNamespaceNumber') % 2 == 1) { pageLink_talk = mw.config.get('wgPageName'); if (mw.config.get('wgNamespaceNumber') == 0) pageLink = mw.config.get('wgTitle'); else pageLink = mw.config.get('wgPageName').replace(/_talk:/ig, ":"); }

var namespaceTalk_title = "Talk"; var namespaceMain_title; if ([0, 1].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) namespaceMain_title = "Article"; else if ([2, 3].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) namespaceMain_title = "User page"; else if ([4, 5].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) namespaceMain_title = "Project page"; else if ([6, 7].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) namespaceMain_title = "File"; else if ([8, 9].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) namespaceMain_title = "Interface page"; else if ([10, 11].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) namespaceMain_title = "Template"; else if ([12, 13].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) namespaceMain_title = "Help page"; else if ([14, 15].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) namespaceMain_title = "Category"; else namespaceMain_title = "Page";

var namespaceMain_link = createElement('a', namespaceMain_title, {       'href': "/wiki/" + pageLink,        'title': "View the content page"    });

var namespaceTalk_link = createElement('a', namespaceTalk_title, {       'href': "/wiki/" + pageLink_talk,        'title': "Discuss improvements to the content page"    });

var namespaceMain = createElement('li', namespaceMain_link, {       'id': "ca-main",        'class': "mw-list-item-left"    }); var namespaceTalk = createElement('li', namespaceTalk_link, {       'id': "ca-talk",        'class': "mw-list-item-left"    }); if (mw.config.get('wgNamespaceNumber') % 2 == 0) namespaceMain.classList.add("selected"); else if (mw.config.get('wgNamespaceNumber') % 2 == 1) namespaceTalk.classList.add("selected");

var namespace = createElement('ul', [namespaceMain, namespaceTalk], {       'class': "vector-menu-content-list"    });

var namespace_div = createElement('div', [namespace], {       'class': "vector-menu-content"    });

var namespace_nav = createElement('nav', namespace_div, {       'id': "p-namespaces",        'role': "navigation",        'class': "vector-menu vector-menu-tabs"    });

var leftNav_div = createElement('div', namespace_nav, {       'id': "left-navigation"    }); head_div.appendChild(leftNav_div);

/* right navigation */ var view_link = createElement('a', "Read", {       'href': "/wiki/" + mw.config.get('wgPageName')    }); var view = createElement('li', view_link, {       'id': "ca-view",        'class': "mw-list-item-right"    }); if (mw.config.get('wgAction') == "view") view.classList.add("selected");

var edit, source, history; if (document.getElementById("ca-edit")) { var original_edit = document.getElementById("ca-edit"); var edit_link = createElement('a', "", {           'href': original_edit.href        }); edit_link.innerHTML = "Edit"; original_edit.remove; var edit = createElement('li', edit_link, {           'id': "ca-edit",            'class': "mw-list-item-right"        }); if (mw.config.get('wgAction') == "edit") edit.classList.add("selected"); }

if (document.getElementById("ca-viewsource")) { var original_source = document.getElementById("ca-viewsource"); var source_link = createElement('a', "", {           'href': original_source.href        }); source_link.innerHTML = "View source"; original_source.remove; var source = createElement('li', source_link, {           'id': "ca-viewsource",            'class': "mw-list-item-right"        }); if (mw.config.get('wgAction') == "edit") source.classList.add("selected"); }   if (document.getElementById("ca-history")) { var original_history = document.getElementById("ca-history"); var history_link = createElement('a', "", {           'href': original_history.href        }); history_link.innerHTML = "View history"; original_history.parentNode.remove; var history = createElement('li', history_link, {           'id': "ca-history",            'class': "mw-list-item-right"        }); if (mw.config.get('wgAction') == "history") history.classList.add("selected"); }

var pages = createElement('ul', [view, edit, source, history], {       'class': "vector-menu-content-list"    });

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

var pages_nav = createElement('nav', pages_div, {       'id': "p-pages",        'role': "navigation",        'class': "vector-menu vector-menu-tabs"    }); //

var actions = createElement('ul', "", {       'class': "vector-menu-content-list"    }); var actions_count = document.querySelectorAll("#p-cactions li").length; for (var i = 0; i < actions_count; i++) { var original_actions = document.querySelectorAll("#p-cactions li")[i]; if (!original_actions.firstElementChild?.id) continue; if (original_actions.firstElementChild.id == "ca-talk") continue; var actions_link = createElement('a', original_actions.firstElementChild.innerText.trim, {           'href': original_actions.firstElementChild.href        }); var actions_list = createElement('li', actions_link, {           'id': original_actions.firstElementChild.id,            'class': "mw-dropdown-item"        }); actions.appendChild(actions_list); }

var actions_div = createElement('div', actions, {       'class': "vector-menu-content"    }); var actions_header = createElement('span', "More", "");

var actions_nav = createElement('nav', [actions_header, actions_div], {       'id': "p-cactions",        'role': "navigation",        'class': "vector-menu vector-menu-dropdown"    });

var rightNav_div = createElement('div', [pages_nav, actions_nav], {       'id': "right-navigation"    }); head_div.appendChild(rightNav_div); }

/* -- 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("wgIsArticle") && !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("wgIsArticle") && !mw.config.get("wgIsMainPage") && document.getElementById("rail-boxad-wrapper")) 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 document.getElementsByClassName("page-header__actions")[0].remove; // remove Fandom page actions if(mw.config.get("wgAction") == "view") { document.getElementsByClassName("page-header__top")[0].remove; // remove Fandom meta & language header if(!mw.config.get("wgIsMainPage")) { setTimeout(function { // wait for page tools to load bruh               if (document.getElementsByClassName("page__right-rail")) document.getElementsByClassName("page__right-rail")[0].remove; // remove Fandom rightnav            }, 1100); }   } }

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

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