MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 28: | Line 28: | ||
elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);'; | elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);'; | ||
} | } | ||
var main_page_tab = '' | if (desc == '.onlyhota' || desc == '.onlysod') { | ||
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() | |||
} | |||
} | } | ||
} | } | ||
Line 51: | Line 53: | ||
function togglePreferredExpansion() { | function togglePreferredExpansion() { | ||
var | togglePreference('switchExpansion', 'preferredExpansion', 'sod', 'hota', 'Shadow of Death', 'Horn of the Abyss'); | ||
var switchExpansion = document.querySelector('# | } | ||
if (preferredExpansion == | |||
preferredExpansion = | function togglePreferredView() { | ||
switchExpansion.textContent = 'Switch to | togglePreference('switchView', 'preferredView', 'modern', 'legacy', 'Modern', 'Legacy'); | ||
hideElements('. | } | ||
showElements('. | |||
function togglePreference(linkSelector, cookieName, cookie1, cookie2, link1, link2) { | |||
var preference= getCookie(cookieName); | |||
var switchExpansion = document.querySelector('#' + linkSelector); | |||
if (preferredExpansion == cookie2) { | |||
preferredExpansion = cookie1; | |||
switchExpansion.textContent = 'Switch to ' + link2; | |||
hideElements('.only' + cookie2); | |||
showElements('.only' + cookie1); | |||
} else { | } else { | ||
preferredExpansion = | preferredExpansion = cookie2; | ||
switchExpansion.textContent = 'Switch to | switchExpansion.textContent = 'Switch to ' + link1; | ||
hideElements('. | hideElements('.only' + cookie1); | ||
showElements('. | showElements('.only' + cookie2); | ||
} | } | ||
var CookieDate = new Date(); | var CookieDate = new Date(); | ||
CookieDate.setFullYear(CookieDate.getFullYear() + 1); | CookieDate.setFullYear(CookieDate.getFullYear() + 1); | ||
document.cookie = ' | document.cookie = cookieName + '=' + preference + '; expires=' + CookieDate.toUTCString() + ';'; | ||
} | } | ||
function | function initPreference(linkSelector, cookieName, cookie1, cookie2, link1, link2, toggleFunc) { | ||
var userlink = document.querySelector('#pt-anonuserpage'); | var userlink = document.querySelector('#pt-anonuserpage'); | ||
if (!userlink) { | if (!userlink) { | ||
Line 75: | Line 85: | ||
} | } | ||
if (userlink) { | if (userlink) { | ||
var switchExpansion = document.querySelector('# | var switchExpansion = document.querySelector('#' + linkSelector); | ||
if (!switchExpansion) { | if (!switchExpansion) { | ||
switchExpansion = document.createElement('li'); | switchExpansion = document.createElement('li'); | ||
switchExpansion.style = 'cursor: pointer; color: rgb(102, 177, 250);'; | switchExpansion.style = 'cursor: pointer; color: rgb(102, 177, 250);'; | ||
switchExpansion.id = 'switchExpansion'; | switchExpansion.id = 'switchExpansion'; | ||
switchExpansion.addEventListener('click', | switchExpansion.addEventListener('click', toggleFunc); | ||
userlink.parentElement.insertBefore(switchExpansion, null); | userlink.parentElement.insertBefore(switchExpansion, null); | ||
var elems = document.querySelectorAll('. | var elems = document.querySelectorAll('.' + linkSelector); | ||
for (var i = 0; i < elems.length; i++) { | for (var i = 0; i < elems.length; i++) { | ||
elems[i].addEventListener('click', | elems[i].addEventListener('click', toggleFunc); | ||
elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);'; | elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);'; | ||
} | } | ||
} | } | ||
if (getCookie( | if (getCookie(cookieName) == cookie2) { | ||
switchExpansion.textContent = 'Switch to | switchExpansion.textContent = 'Switch to ' + link1; | ||
hideElements('. | hideElements('.only' + cookie1); | ||
showElements('. | showElements('.only' + cookie2); | ||
} else { | } else { | ||
switchExpansion.textContent = 'Switch to | switchExpansion.textContent = 'Switch to ' + link2; | ||
hideElements('. | hideElements('.only' + cookie2); | ||
showElements('. | showElements('.only' + cookie1); | ||
} | } | ||
} | } | ||
Line 101: | Line 111: | ||
window.addEventListener('readystatechange', function() { | window.addEventListener('readystatechange', function() { | ||
initPreference('switchExpansion', 'preferredExpansion', 'sod', 'hota', 'Shadow of Death', 'Horn of the Abyss', togglePreferredExpansion); | |||
initPreference('switchView', 'preferredView', 'modern', 'legacy', 'Modern', 'Legacy', togglePreferredView); | |||
}); | }); | ||
Revision as of 06:24, 7 February 2024
/* 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);';
}
if (desc == '.onlyhota' || desc == '.onlysod') {
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() {
togglePreference('switchExpansion', 'preferredExpansion', 'sod', 'hota', 'Shadow of Death', 'Horn of the Abyss');
}
function togglePreferredView() {
togglePreference('switchView', 'preferredView', 'modern', 'legacy', 'Modern', 'Legacy');
}
function togglePreference(linkSelector, cookieName, cookie1, cookie2, link1, link2) {
var preference= getCookie(cookieName);
var switchExpansion = document.querySelector('#' + linkSelector);
if (preferredExpansion == cookie2) {
preferredExpansion = cookie1;
switchExpansion.textContent = 'Switch to ' + link2;
hideElements('.only' + cookie2);
showElements('.only' + cookie1);
} else {
preferredExpansion = cookie2;
switchExpansion.textContent = 'Switch to ' + link1;
hideElements('.only' + cookie1);
showElements('.only' + cookie2);
}
var CookieDate = new Date();
CookieDate.setFullYear(CookieDate.getFullYear() + 1);
document.cookie = cookieName + '=' + preference + '; expires=' + CookieDate.toUTCString() + ';';
}
function initPreference(linkSelector, cookieName, cookie1, cookie2, link1, link2, toggleFunc) {
var userlink = document.querySelector('#pt-anonuserpage');
if (!userlink) {
userlink = document.querySelector('#pt-userpage');
}
if (userlink) {
var switchExpansion = document.querySelector('#' + linkSelector);
if (!switchExpansion) {
switchExpansion = document.createElement('li');
switchExpansion.style = 'cursor: pointer; color: rgb(102, 177, 250);';
switchExpansion.id = 'switchExpansion';
switchExpansion.addEventListener('click', toggleFunc);
userlink.parentElement.insertBefore(switchExpansion, null);
var elems = document.querySelectorAll('.' + linkSelector);
for (var i = 0; i < elems.length; i++) {
elems[i].addEventListener('click', toggleFunc);
elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);';
}
}
if (getCookie(cookieName) == cookie2) {
switchExpansion.textContent = 'Switch to ' + link1;
hideElements('.only' + cookie1);
showElements('.only' + cookie2);
} else {
switchExpansion.textContent = 'Switch to ' + link2;
hideElements('.only' + cookie2);
showElements('.only' + cookie1);
}
}
}
window.addEventListener('readystatechange', function() {
initPreference('switchExpansion', 'preferredExpansion', 'sod', 'hota', 'Shadow of Death', 'Horn of the Abyss', togglePreferredExpansion);
initPreference('switchView', 'preferredView', 'modern', 'legacy', 'Modern', 'Legacy', togglePreferredView);
});
window.addEventListener('DOMContentLoaded', function() {
initPreferredExpansion();
});
window.addEventListener('load', function() {
initPreferredExpansion();
});
initPreferredExpansion();
})();