MediaWiki:Common.js

From Heroes 3 wiki
Revision as of 00:45, 6 February 2024 by Imahero (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

(function() {

function getCookie(cname) {
	var name = cname + '=';
	var decodedCookie = decodeURIComponent(document.cookie);
	var ca = decodedCookie.split(';');
	for (var i = 0; i <ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
			c = c.substring(1);
		}
		if (c.indexOf(name) == 0) {
			return c.substring(name.length, c.length);
		}
	}
	return '';
}

function showElements(desc) {
	var elems = document.querySelectorAll(desc);
	for (var i = 0; i < elems.length; i++) {
		elems[i].style = '';
	}
	elems = document.querySelectorAll('.switchExpansion' + desc);
	for (var i = 0; i < elems.length; i++) {
		elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);';
	}
	var main_page_tab = ''
	if (desc == '.onlyhota') {
		main_page_tab = document.querySelector('.mainpage.onlysod.onlyhota #tabs-input-1-2')
	} else {
		main_page_tab = document.querySelector('.mainpage.onlysod.onlyhota #tabs-input-1-1')
	}
	if (main_page_tab) {
		main_page_tab.click()
	}
}

function hideElements(desc) {
	var elems = document.querySelectorAll(desc);
	for (var i = 0; i < elems.length; i++) {
		elems[i].style = 'display:none;';
	}
	elems = document.querySelectorAll('.initialOnly');
	for (var i = 0; i < elems.length; i++) {
		elems[i].style = 'display:none;';
	}
}

function togglePreferredExpansion() {
	var preferredExpansion = getCookie('preferredExpansion');
	var switchExpansion = document.querySelector('#switchExpansion');
	if (preferredExpansion == 'hota') {
		preferredExpansion = 'sod';
		switchExpansion.textContent = 'Switch to Horn of the Abyss';
		hideElements('.onlyhota');
		showElements('.onlysod');
	} else {
		preferredExpansion = 'hota';
		switchExpansion.textContent = 'Switch to Shadow of Death';
		hideElements('.onlysod');
		showElements('.onlyhota');
	}
	var CookieDate = new Date();
	CookieDate.setFullYear(CookieDate.getFullYear() + 1);
	document.cookie = 'preferredExpansion=' + preferredExpansion + '; expires=' + CookieDate.toUTCString() + ';';
}

function initPreferredExpansion() {
	var userlink = 	document.querySelector('#pt-anonuserpage');
	if (!userlink) {
		userlink = document.querySelector('#pt-userpage');
	}
	if (userlink) {
		var switchExpansion = document.querySelector('#switchExpansion');
		if (!switchExpansion) {
			switchExpansion = document.createElement('li');
			switchExpansion.style = 'cursor: pointer; color: rgb(102, 177, 250);';
			switchExpansion.id = 'switchExpansion';
			switchExpansion.addEventListener('click', togglePreferredExpansion);
			userlink.parentElement.insertBefore(switchExpansion, null);
			var elems = document.querySelectorAll('.switchExpansion');
			for (var i = 0; i < elems.length; i++) {
				elems[i].addEventListener('click', togglePreferredExpansion);
				elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);';
			}
		}
		if (getCookie('preferredExpansion') == 'hota') {
			switchExpansion.textContent = 'Switch to Shadow of Death';
			hideElements('.onlysod');
			showElements('.onlyhota');
		} else {
			switchExpansion.textContent = 'Switch to Horn of the Abyss';
			hideElements('.onlyhota');
			showElements('.onlysod');
		}
	}
}

window.addEventListener('readystatechange', function() {
	initPreferredExpansion();
});

window.addEventListener('DOMContentLoaded', function() {
	initPreferredExpansion();
});

window.addEventListener('load', function() {
	initPreferredExpansion();
});

initPreferredExpansion();

})();