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:DisplayTimer/code.js',		'u:dev:MediaWiki:Modal.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; }); });

// // // 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 {	mw.hook("wikipage.content").remove(self);	const mainContainer = $(".main-container")[0];	const resizableContainer = $(".resizable-container")[0];	const communityHeader = $(".community-header-wrapper")[0];	const profilePicture = $(".wds-avatar")[0];	const navigationHeader = document.createElement("div");	const sideContainer = document.createElement("div");	const dualContainer = document.createElement("div");	navigationHeader.className = "navigation-header";	sideContainer.className = "side-container";	dualContainer.className = "dual-container";	navigationHeader.appendChild($(".notifications")[0]);	dualContainer.appendChild(sideContainer);	dualContainer.appendChild(resizableContainer);	mainContainer.insertBefore(navigationHeader, $(".global-footer")[0]);	mainContainer.insertBefore(dualContainer, $(".global-footer")[0]);	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: linkify("Main Page"), 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: "Profile", 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"}, ]}, sideContainer);	$.ajax({ url: linkify("MediaWiki:Mainpage?action=raw"), type: "GET", success: function (data) { $("[href='"+linkify("Main Page")+"']")[0].href = linkify(data); }	});	communityHeader.replaceChildren; });

// // // 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)); if (amount === 0) { talkModal.setContent("The talk page does not exist."); } else { $.ajax({			       url: talkNode.href+"?action=render",			        type: 'GET',			        success: function (data) { talkModal.setContent(data); talkModal.setSize("large"); },			    }); }			return window.tabs.createTab({				text: text,				event: function { talkModal.show }			}); }),		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); }			});		});	}); */

});