MediaWiki:Common.js: Difference between revisions

From Heroes 3 wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
function setPreferredExpansion() {
function getCookie(cname) {
// for (let hota of document.querySelectorAll('.hota')) {
let name = cname + "=";
//    console.log(hota)
let decodedCookie = decodeURIComponent(document.cookie);
// }
let ca = decodedCookie.split(';');
 
for (let i = 0; i <ca.length; i++) {
var userlink = document.querySelector('#pt-anonuserpage')
let 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) {
for (var elem of document.querySelectorAll(desc)) {
    elem.style = '';
}
}
function hideElements(desc) {
for (var elem of document.querySelectorAll(desc)) {
    elem.style = 'display:none;';
}
}
function togglePreferredExpansion() {
preferredExpansion = getCookie('preferredExpansion');
var switchExpansion = document.querySelector('#switchExpansion');
if (preferredExpansion == 'hota') {
preferredExpansion = 'sod';
switchExpansion.textContent = 'Switch to Horn of the Abyss';
showElements('.onlysod');
hideElements('.onlyhota');
} else {
preferredExpansion = 'hota';
switchExpansion.textContent = 'Switch to Shadow of Death';
showElements('.onlyhota');
hideElements('.onlysod');
}
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) {
if (!userlink) {
userlink = document.querySelector('#pt-userpage')
userlink = document.querySelector('#pt-userpage');
}
}
if (userlink && !document.querySelector('#switchExpansion')) {
if (userlink) {
var newElement = document.createElement("li")
var switchExpansion = document.querySelector('#switchExpansion');
newElement.style = 'cursor: pointer; color: rgb(102, 177, 250); text-decoration: underline;'
if (!switchExpansion) {
newElement.id = 'switchExpansion'
switchExpansion = document.createElement("li");
newElement.textContent = 'Switch to Horn of the Abyss'
switchExpansion.style = 'cursor: pointer; color: rgb(102, 177, 250); text-decoration: underline;';
userlink.parentElement.insertBefore(newElement, null)
switchExpansion.id = 'switchExpansion';
switchExpansion.addEventListener("click", togglePreferredExpansion);
userlink.parentElement.insertBefore(switchExpansion, null);
}
if (getCookie('preferredExpansion') == 'hota') {
switchExpansion.textContent = 'Switch to Horn of the Abyss';
showElements('.onlyhota');
hideElements('.onlysod');
} else {
switchExpansion.textContent = 'Switch to Shadow of Death';
showElements('.onlysod');
hideElements('.onlyhota');
}
}
}
// var CookieDate = new Date;
// CookieDate.setFullYear(CookieDate.getFullYear() + 1);
// document.cookie = 'preferredExpansion=hota; expires=' + CookieDate.toUTCString() + ';';
}
}
window.addEventListener('readystatechange', function() {
window.addEventListener('readystatechange', function() {
     setPreferredExpansion()
     initPreferredExpansion();
})
});
window.addEventListener('DOMContentLoaded', function() {
window.addEventListener('DOMContentLoaded', function() {
     setPreferredExpansion()
     initPreferredExpansion();
})
});
window.addEventListener('load', function() {
window.addEventListener('load', function() {
     setPreferredExpansion()
     initPreferredExpansion();
})
});
initPreferredExpansion();

Revision as of 01:57, 25 January 2024

/* Any JavaScript here will be loaded for all users on every page load. */
function getCookie(cname) {
	let name = cname + "=";
	let decodedCookie = decodeURIComponent(document.cookie);
	let ca = decodedCookie.split(';');
	for (let i = 0; i <ca.length; i++) {
		let 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) {
	for (var elem of document.querySelectorAll(desc)) {
	    elem.style = '';
	}	
}
function hideElements(desc) {
	for (var elem of document.querySelectorAll(desc)) {
	    elem.style = 'display:none;';
	}	
}
function togglePreferredExpansion() {
	preferredExpansion = getCookie('preferredExpansion');
	var switchExpansion = document.querySelector('#switchExpansion');
	if (preferredExpansion == 'hota') {
		preferredExpansion = 'sod';
		switchExpansion.textContent = 'Switch to Horn of the Abyss';
		showElements('.onlysod');
		hideElements('.onlyhota');
	} else {
		preferredExpansion = 'hota';
		switchExpansion.textContent = 'Switch to Shadow of Death';
		showElements('.onlyhota');
		hideElements('.onlysod');
	}
	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); text-decoration: underline;';
			switchExpansion.id = 'switchExpansion';
			switchExpansion.addEventListener("click", togglePreferredExpansion);
			userlink.parentElement.insertBefore(switchExpansion, null);
		}
		if (getCookie('preferredExpansion') == 'hota') {
			switchExpansion.textContent = 'Switch to Horn of the Abyss';
			showElements('.onlyhota');
			hideElements('.onlysod');
		} else {
			switchExpansion.textContent = 'Switch to Shadow of Death';
			showElements('.onlysod');
			hideElements('.onlyhota');
		}
	}
}
window.addEventListener('readystatechange', function() {
    initPreferredExpansion();
});
window.addEventListener('DOMContentLoaded', function() {
    initPreferredExpansion();
});
window.addEventListener('load', function() {
    initPreferredExpansion();
});
initPreferredExpansion();