User:Williek11/global.js

// // // import scripts // //

importArticles({	type: 'script',	articles: [		'u:dev:MediaWiki:Rollback/code.js',		'u:dev:MediaWiki:AnchoredRollback/code.js',		'u:dev:MediaWiki:RemoveTracking.js',		'u:dev:MediaWiki:Modal.js',		'u:dev:MediaWiki:WDSIcons/code.js',	] });

// // // global variables // //

const lang = mw.config.get("wgContentLanguage"); const pageName = mw.config.get("wgPageName");

// // // global functions // //

function linkify (text) { return lang === "en" ? "/wiki/"+text : "/"+lang+"/wiki/"+text; }

// // // modals // //

mw.hook("dev.modal").add(function(modal){	/* new modal.Modal({ id: "comments", title: "Comments", size: "large", }); */	new modal.Modal({ id: "talk", title: "Talk page", size: "large", content: "Loading...", buttons: (function{			const talkObject = new mw.Title(pageName).getTalkPage;			if (talkObject === null) { return []; }			const talkPage = talkObject.getPrefixedText;			return [		       { text: 'Go to talk page', href: linkify(talkPage), type: "link", },		        { text: 'Edit talk page', href: linkify(talkPage+"?action=edit"), type: "link", },		        { text: 'Talk page history', href: linkify(talkPage+"?action=history"), type: "link", },			];		}), }).create.then(function{ mw.hook("dev.talkPage.modal").fire; }); });

// // // theme switch api // huge thanks to ThemeToggler on dev wiki // https://dev.fandom.com/wiki/ThemeToggler // //

$(function {	const fullBackground = $('.fandom-community-header__background.fullScreen');	window.dev.Williek11 = {};	window.dev.Williek11.toggleWithoutReload = function {		var theme = $( 'body' ).hasClass( 'theme-fandomdesktop-light' ) ? 'light' : 'dark';		var newTheme = theme === 'light' ? 'dark' : 'light';

// Implementation by User:Pcj from ThemeSwitcher $.when(			$.get(mw.util.wikiScript( 'wikia' ) + '?controller=ThemeApi&method=themeVariables&variant=' + newTheme + '&cb=' + ( new Date.getTime ) ),			$.get( mw.util.wikiScript( 'load' ) + '?modules=ext.fandom.GlobalComponents.GlobalComponentsTheme.' + newTheme + '.css%7Cext.fandom.GlobalComponents.GlobalComponentsTheme.nav-' + newTheme + '.css&only=styles' )		) .done( function( wikiTheme, brandTheme ) {			var css = wikiTheme[0] + brandTheme[0];			var $s = $( '#pcjThemeSwitch' )[0] || $( ' ' ).attr( 'id', 'pcjThemeSwitch' ).appendTo('body');			$($s).text(css);			if (fullBackground.length) {				var bgMatch = wikiTheme[0].match(/--theme-body-background-image\s*?:\s*?url\((.*?)\)/);				if (bgMatch) {					var bgImage = new Image;					bgImage.onload = function {						fullBackground.css('--image-ratio', (this.height / this.width).toString);					};					bgImage.src = bgMatch[1];				}			}			$( 'body' ).removeClass( 'theme-fandomdesktop-light theme-fandomdesktop-dark' ).addClass( 'theme-fandomdesktop-' + newTheme );			mw.config.set( 'isDarkTheme', !mw.config.get( 'isDarkTheme' ) );		} ); }; } );

// // // instantiate objects // //

(function instantiateObjects {	window.sidebar = {};	window.sidebar.addCollapse = function (container, toCollapse) {		const collapseNode = document.createElement("span");		const title = container.getElementsByClassName("side_items-title")[0];		const ul = container.getElementsByClassName("side_items-list")[0];		collapseNode.classList.add("side_items-title_collapse");		Object.freeze(ul);		Object.freeze(container);		var isCollapsed = false;		function setHeight (h) {ul.style.height = h;}		if (toCollapse === true) {			collapseNode.textContent = "▼";			setHeight(0);			isCollapsed = true;		} else {			collapseNode.textContent = "▲";		}		title.appendChild(collapseNode);		collapseNode.addEventListener("click", function { if (isCollapsed === false) { collapseNode.textContent = "▼"; setHeight(0); isCollapsed = true; } else { collapseNode.textContent = "▲"; setHeight(""); isCollapsed = false; }		});	};	window.sidebar.addNodeToSide = function (obj, sideContainer, startCollapsed) {		const node = document.createElement("div");		const titleNode = document.createElement("div");		const ulNode = document.createElement("ul");		node.className = "side_item";		ulNode.className = "side_items-list";		titleNode.className = "side_items-title";		titleNode.innerHTML = obj.title;		node.appendChild(titleNode);		node.appendChild(ulNode);		if (startCollapsed === true) {			window.sidebar.addCollapse(node, true);		} else {			window.sidebar.addCollapse(node);		}		for (var i = 0, l = obj.items.length; i < l; i++) {			const currentItem = obj.items[i];			const itemLink = currentItem.link;			const itemHTML = currentItem.html;			const itemNode = document.createElement("li");			const linkNode = document.createElement("a");			if (currentItem.id !== undefined) {				itemNode.id = currentItem.id;			}			linkNode.href = itemLink; linkNode.innerHTML = itemHTML; itemNode.appendChild(linkNode); ulNode.appendChild(itemNode); }		sideContainer.appendChild(node); };	window.tabs = {}; window.tabs.createTab = function (obj) { const aNode = document.createElement("a"); const node = document.createElement("div"); node.className = "community-header-tab"; node.innerText = obj.text; if (obj.event) { node.addEventListener("click", obj.event); } if (obj.link) { aNode.href = obj.link; aNode.appendChild(node); return aNode; } return node; };	window.tabs.createTabBasedOnNode = function (name) { const miscNode = $("#ca-"+name)[0]; if (miscNode === undefined) { return null; } return window.tabs.createTab({			text: miscNode.textContent.trim,			link: miscNode.href		}); }; });

// // // content adjustments // //

mw.hook("wikipage.content").add(function self {setTimeout(function{ mw.hook("wikipage.content").remove(self); const MPLang = mw.storage.get("MP-"+lang); const mainContainer = $(".main-container")[0]; const resizableContainer = $(".resizable-container")[0]; const communityHeader = $(".community-header-wrapper")[0]; const navigationHeader = document.createElement("div"); const sideContainer = document.createElement("div"); const dualContainer = document.createElement("div"); const screenSwitch = document.createElement("div"); const themeSwitch = document.createElement("div"); navigationHeader.className = "navigation-header"; sideContainer.className = "side-container"; dualContainer.className = "dual-container"; screenSwitch.appendChild($(".content-size-toggle")[0].firstElementChild); $(themeSwitch).append(' '); navigationHeader.appendChild($(".notifications")[0]); navigationHeader.appendChild(themeSwitch); navigationHeader.appendChild(screenSwitch); dualContainer.appendChild(sideContainer); dualContainer.appendChild(resizableContainer); mainContainer.insertBefore(navigationHeader, $(".global-footer")[0]); mainContainer.insertBefore(dualContainer, $(".global-footer")[0]); $(themeSwitch).click(window.dev.Williek11.toggleWithoutReload) window.sidebar.addNodeToSide({title: "Profile", items: [		{link: linkify("User:Williek11"), html: "Own profile"},		{link: linkify("Message Wall:Williek11"), html: "Message wall"},		{link: linkify("User blog:Williek11"), html: "User blog"},		{link: linkify("Special:Contributions/Williek11"), html: "Contributions"},		{link: linkify("Special:UserProfileActivity/Williek11"), html: "User activity"},		{link: linkify("User:Williek11/Sandbox"), html: "Sandbox"},	]}, sideContainer); window.sidebar.addNodeToSide({title: "Site tools", items: [		{link: MPLang, html: "Main page"},		{link: linkify("Special:RecentChanges"), html: "Recent changes"},		{link: linkify("Special:SocialActivity"), html: "Social activity"},		{link: linkify("Special:NewFiles"), html: "New files"},		{link: linkify("Category:Maintenance"), html: "Maintenance"},		{link: linkify("Category:Templates"), html: "Templates"},		{link: linkify("Special:SpecialPages"), html: "Special pages"},		{link: linkify("Special:Preferences"), html: "Change preferences"},	]}, sideContainer); window.sidebar.addNodeToSide({title: "Page tools", items: [		{link: linkify("Special:WhatLinksHere/"+pageName), html: "What links here", id:"side_item-what_links_here"},		{link: linkify(pageName+"?action=info"), html: "Information"},	]}, sideContainer); window.sidebar.addNodeToSide({title: "Scripts", items: [		{link: linkify("User:Williek11/common.js?action=edit"), html: "Common JS"},		{link: linkify("User:Williek11/common.css?action=edit"), html: "Common CSS"},		{link: "https://community.fandom.com/wiki/User:Williek11/global.js?action=edit", html: "Global JS"},		{link: "https://community.fandom.com/wiki/User:Williek11/global.css?action=edit", html: "Global CSS"},		{link: linkify(pageName+"?useuserjs=0&useusercss=0"), html: "Remove User Scripts"},		{link: linkify(pageName+"?usesitejs=0&usesitecss=0"), html: "Remove Site Scripts"},		{link: linkify(pageName+"?safemode=1"), html: "Remove Both Scripts"},	]}, sideContainer); if (MPLang === null) { $.ajax({			url: linkify("MediaWiki:Mainpage?action=raw"),			type: "GET",			success: function (data) {				$("[href='null']")[0].href = linkify(data);				mw.storage.set("MP-"+lang, linkify(data))			}		}); }	communityHeader.replaceChildren; })}, 50);

// // // navbar adjustments // //

mw.hook("dev.talkPage.modal").add(function {	const communityHeader = $(".community-header-wrapper")[0];	const arrayOfTabs = [		(function{ const talkNode = $("#ca-talk")[0]; if (talkNode === undefined) { return null; } const talkModal = window.dev.modal.modals.talk; const text = talkNode.textContent.trim; const amount = parseInt(text.substr(6)); var eventFunction; if (amount === 0) { talkModal.setContent("The talk page does not exist."); eventFunction = function {talkModal.show} } else { eventFunction = function { talkModal.show; if (window.loadedTalkPage === true) {return;} $.ajax({				       url: talkNode.href+"?action=render",				        type: 'GET',				        success: function (data) {				        	talkModal.setContent(data);				        	talkModal.setSize("large");				        	window.loadedTalkPage = true;				        },				    }); }			}			return window.tabs.createTab({				text: text,				event: eventFunction			}); }),		window.tabs.createTabBasedOnNode("edit"),		window.tabs.createTabBasedOnNode("move"),		window.tabs.createTabBasedOnNode("history"),		window.tabs.createTabBasedOnNode("delete"),		window.tabs.createTabBasedOnNode("protect"),		window.tabs.createTabBasedOnNode("unprotect"),		window.tabs.createTab({ text: "Purge", link: linkify(pageName+"?action=purge"), }),	];	for (var i = 0, l = arrayOfTabs.length; i < l; i++) {		const node_or_null = arrayOfTabs[i];		if (node_or_null === null) {continue}		communityHeader.appendChild(node_or_null);	} });

// // // scripts with mw.Api // //

mw.loader.using('mediawiki.api').then(function{	new mw.Api.get({ action: 'query', format: 'json', list: 'backlinks', bltitle: pageName, }).done(function(data) { mw.hook("wikipage.content").add(function self{			mw.hook("wikipage.content").remove(self);			const text = "What links here: "+data.query.backlinks.length;			$("#side_item-what_links_here")[0].firstElementChild.innerText = text;		}); });	/* THEME SWITCH	$(".wiki-tools__theme-switch")[1].addEventListener("click", function { new mw.Api.get({action:"query", meta:"tokens"}).done(function(e){			var change;			if ($("body")[0].classList.contains("theme-fandomdesktop-dark")) {				change = 'theme=light';			} else {				change = 'theme=dark';			}			new mw.Api.post({ action: 'options', format: 'json', change: change, token: e.query.tokens.csrftoken }).done(function(e){ if (e.options === "success") { location.reload; } else { console.error(e); }			});		});	}); */

});