MediaWiki:Common.js:修订间差异

(创建页面,内容为“→‎Any JavaScript here will be loaded for all users on every page load.:​ (function () { var heroesStyleSheet = new CSSStyleSheet(); heroesStyleSheet.replaceSync('#content,h1,h2,h3,h4{color:#fff}#p-views,.vector-menu-tabs-legacy li a{background:0 0;height:initial}body{background:url(https://heroes.thelazy.net/images/9/9f/Shroud.png)}#mw-head,#mw-head-base,#mw-page-base{background:0 0}div.mw-body-content.mw-content-ltr,table{color:#fff;background:url(https://…”
 
无编辑摘要
第1行: 第1行:
/* Any JavaScript here will be loaded for all users on every page load. */
// 添加新按钮
 
function addToggleButton() {
(function () {
   const navbar = document.querySelector('.navbar');
 
   const lastDropdown = navbar.querySelector('.dropdown:last-child');
var heroesStyleSheet = new CSSStyleSheet();
   const newButton = document.createElement('li');
heroesStyleSheet.replaceSync('#content,h1,h2,h3,h4{color:#fff}#p-views,.vector-menu-tabs-legacy li a{background:0 0;height:initial}body{background:url(https://heroes.thelazy.net/images/9/9f/Shroud.png)}#mw-head,#mw-head-base,#mw-page-base{background:0 0}div.mw-body-content.mw-content-ltr,table{color:#fff;background:url(https://heroes.thelazy.net/images/b/b4/Leather-extra-dark.png)}#mw-content-text,div.mw-body-content.mw-content-ltr{border-image-source:url(https://heroes.thelazy.net/images/c/c7/Border-gold.png);border-image-slice:8 9 8 9;border-image-width:8px 9px 8px 9px;border-image-outset:8px 9px 8px 9px;border-image-repeat:repeat repeat}#content,.vector-menu-content{border-image-slice:48 48 48 48;border-image-width:48px 48px 48px 48px;border-image-outset:0 0 0 0;border-image-repeat:repeat repeat}#content{border-image-source:url(https://heroes.thelazy.net/images/4/43/Border-blue.png);background:url(https://heroes.thelazy.net/images/3/3d/Leather.png);margin-top:4px;padding:48px}.vector-menu-content{border-image-source:url(https://heroes.thelazy.net/images/2/2a/Border-red.png)}.vector-legacy-sidebar .vector-menu-portal .vector-menu-content{padding:24px;margin:0;background:url(https://heroes.thelazy.net/images/8/82/Marble.png)}a,a:visited{color:#add8e6!important}.mw-redirectedfrom,.vector-menu-heading-label{color:#fff!important}#left-navigation .vector-menu-content-list,#left-navigation .vector-menu-tabs{border:2px solid #b8860b}.vector-menu-tabs .vector-menu-content{border:2px solid #000}li.mw-list-item{background:0 0!important;padding:.2em .6em}.vector-menu-tabs .vector-menu-content-list li:not(:last-child){border-right:2px solid #daa520}.vector-menu-tabs-legacy li a{padding:0;margin:0}#p-personal .vector-menu-content-list{border:none}#mw-content-text{padding:8px}#p-tb-label{display:none}');
   newButton.className = 'dropdown nav-item';
 
   newButton.innerHTML = `
function getCookie(cname) {
     <a class="nav-link" role="button" title="切换版本" href="#" id="toggleButton">
  var name = cname + '=';
       <span class="fa fa-exchange"></span>
  var decodedCookie = decodeURIComponent(document.cookie);
       <span class="hide-title">切换版本</span>
  var ca = decodedCookie.split(';');
     </a>
  for (var i = 0; i < ca.length; i++) {
    `;
    var c = ca[i];
    lastDropdown.insertAdjacentElement('afterend', newButton);
    while (c.charAt(0) == ' ') {
    return document.getElementById('toggleButton');
    c = c.substring(1);
    }
   if (c.indexOf(name) == 0) {
    return c.substring(name.length, c.length);
   }
  }
  return '';
}
}


function showElements(desc, linkSelector) {
// 切换显示/隐藏类
  var elems = document.querySelectorAll(desc);
function toggleClasses(showHOTA) {
  for (var i = 0; i < elems.length; i++) {
   const hotaElements = document.querySelectorAll('.onlyhota');
    elems[i].style = '';
    const sodElements = document.querySelectorAll('.onlysod');
  }
  
  elems = document.querySelectorAll('.' + linkSelector + desc);
   hotaElements.forEach(el => el.style.display = showHOTA ? '' : 'none');
  for (i = 0; i < elems.length; i++) {
    sodElements.forEach(el => el.style.display = showHOTA ? 'none' : '');
    elems[i].style = 'user-select:none; cursor: pointer; color: rgb(102, 177, 250);';
  }
}
}


function hideElements(desc) {
// 设置Cookie
  var elems = document.querySelectorAll(desc);
function setCookie(name, value, days) {
  for (var i = 0; i < elems.length; i++) {
   const expires = new Date(Date.now() + days * 864e5).toUTCString();
    elems[i].style = 'display:none;';
    document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=/';
  }
  elems = document.querySelectorAll('.initialOnly');
  for (i = 0; i < elems.length; i++) {
   elems[i].style = 'display:none;';
  }
}
}


function togglePreference(linkSelector, cookieName, cookie1, cookie2, link1, link2) {
// 获取Cookie
  var preference = getCookie(cookieName);
function getCookie(name) {
  var switchView = document.querySelector('#' + linkSelector);
   return document.cookie.split('; ').reduce((r, v) => {
  var switchView2 = document.querySelector('#' + linkSelector + '2');
     const parts = v.split('=');
  if (preference == cookie2) {
     return parts[0] === name ? decodeURIComponent(parts[1]) : r;
   preference = cookie1;
    }, '');
   switchView.textContent = link2;
   switchView2.textContent = link2;
   hideElements('.only' + cookie2);
   showElements('.only' + cookie1, linkSelector);
  } else {
   preference = cookie2;
   switchView.textContent = link1;
   switchView2.textContent = link1;
   hideElements('.only' + cookie1);
    showElements('.only' + cookie2, linkSelector);
  }
  var CookieDate = new Date();
  CookieDate.setFullYear(CookieDate.getFullYear() + 1);
  document.cookie = cookieName + '=' + preference + '; expires=' + CookieDate.toUTCString() + ';';
}
}


function togglePreferredExpansion() {
// 主函数
  togglePreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', 'Horn of the Abyss');
function initToggleButton() {
}
   const button = addToggleButton();
   let isHOTA = getCookie('isHOTA') !== 'false';


function togglePreferredView() {
   function updateButtonText() {
  togglePreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy', 'Legacy/Modern');
     button.querySelector('.hide-title').textContent = isHOTA ? ' 切换至死亡阴影' : ' 切换至深渊号角';
}
    }
 
function toggleDoR() {
  togglePreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', 'Day of Reckoning');
}
 
function setHeroesStyle() {
  var preference = getCookie('heroesStyle');
  if (preference == 'heroesStyleEnabled') {
   document.adoptedStyleSheets.push(heroesStyleSheet);
  } else {
    while (document.adoptedStyleSheets.pop());
  }
}
 
function toggleHeroesStyle() {
  togglePreference('switchHeroesStyle', 'heroesStyle', 'heroesStyleDisabled', 'heroesStyleEnabled', 'Disable H3CSS', 'Enable H3CSS', 'Heroes Style');
  setHeroesStyle();
}


function initPreference(linkSelector, cookieName, cookie1, cookie2, link1, link2, prefName, toggleFunc) {
   function toggleVersion() {
  var userlink = document.querySelector('#pt-anonuserpage');
     isHOTA = !isHOTA;
  if (!userlink) {
     toggleClasses(isHOTA);
   userlink = document.querySelector('#pt-userpage');
     updateButtonText();
  }
      setCookie('isHOTA', isHOTA, 30);
  if (userlink) {
   var switchView = document.querySelector('#' + linkSelector);
   if (!switchView) {
    switchView = document.createElement('li');
    switchView.style = 'user-select:none; cursor: pointer; color: rgb(102, 177, 250);';
    switchView.id = linkSelector;
    switchView.title = prefName + ' (toggle)';
    switchView.addEventListener('click', toggleFunc);
    userlink.parentElement.insertBefore(switchView, null);
    var elems = document.querySelectorAll('.' + linkSelector);
    for (var i = 0; i < elems.length; i++) {
      elems[i].addEventListener('click', toggleFunc);
     elems[i].style = 'user-select:none; cursor: pointer; color: rgb(102, 177, 250);';
    }
   }
   if (getCookie(cookieName) == cookie2) {
    switchView.textContent = link1;
    hideElements('.only' + cookie1);
    showElements('.only' + cookie2);
   } else {
    switchView.textContent = link2;
    hideElements('.only' + cookie2);
    showElements('.only' + cookie1);
    }
    }
  }
  var mwpanelul = document.querySelector('#mw-panel ul');
  if (mwpanelul) {
   var switchView2 = document.querySelector('#' + linkSelector + '2');
   if (!switchView2) {
    switchView2 = document.createElement('li');
    switchView2.style = 'user-select:none; cursor: pointer; color: rgb(102, 177, 250);';
    switchView2.id = linkSelector + '2';
    switchView2.title = prefName + ' (toggle)';
    switchView2.addEventListener('click', toggleFunc);
    mwpanelul.insertBefore(switchView2, null);
   }
   if (getCookie(cookieName) == cookie2) {
    switchView2.textContent = link1;
    hideElements('.only' + cookie1);
    showElements('.only' + cookie2);
   } else {
    switchView2.textContent = link2;
    hideElements('.only' + cookie2);
    showElements('.only' + cookie1);
   }
  }
}


function removeTooltips() {
   button.addEventListener('click', (e) => {
  var elems = document.querySelectorAll('.mainpage .tabs-content div div a');
     e.preventDefault();
  for (var i = 0; i < elems.length; i++) {
     toggleVersion();
    elems[i].title = '';
    });
  }
}


function removeUnwantedTitles() {
   // 初始化
  var elems = document.querySelectorAll('#p-logo a');
   toggleClasses(isHOTA);
  for (var i = 0; i < elems.length; i++) {
   updateButtonText();
   elems[i].title = '';
  }
}
}


function initCommon() {
// 当DOM加载完成后执行初始化
  initPreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', 'Horn of the Abyss', togglePreferredExpansion);
document.addEventListener('DOMContentLoaded', initToggleButton);
  initPreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', 'Day of Reckoning', toggleDoR);
  var mainpage = document.querySelector('.mainpage.onlysod.onlyhota');
  if (mainpage) {
   initPreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy', 'Legacy/Modern', togglePreferredView);
   removeTooltips();
  }
  removeUnwantedTitles();
  initPreference('switchHeroesStyle', 'heroesStyle', 'heroesStyleDisabled', 'heroesStyleEnabled',  'Disable H3CSS', 'Enable H3CSS', 'Heroes Style', toggleHeroesStyle);
  setHeroesStyle();
}
 
initCommon();
 
})();

2024年9月24日 (二) 07:46的版本

// 添加新按钮
function addToggleButton() {
    const navbar = document.querySelector('.navbar');
    const lastDropdown = navbar.querySelector('.dropdown:last-child');
    const newButton = document.createElement('li');
    newButton.className = 'dropdown nav-item';
    newButton.innerHTML = `
        <a class="nav-link" role="button" title="切换版本" href="#" id="toggleButton">
            <span class="fa fa-exchange"></span>
            <span class="hide-title">切换版本</span>
        </a>
    `;
    lastDropdown.insertAdjacentElement('afterend', newButton);
    return document.getElementById('toggleButton');
}

// 切换显示/隐藏类
function toggleClasses(showHOTA) {
    const hotaElements = document.querySelectorAll('.onlyhota');
    const sodElements = document.querySelectorAll('.onlysod');
    
    hotaElements.forEach(el => el.style.display = showHOTA ? '' : 'none');
    sodElements.forEach(el => el.style.display = showHOTA ? 'none' : '');
}

// 设置Cookie
function setCookie(name, value, days) {
    const expires = new Date(Date.now() + days * 864e5).toUTCString();
    document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=/';
}

// 获取Cookie
function getCookie(name) {
    return document.cookie.split('; ').reduce((r, v) => {
        const parts = v.split('=');
        return parts[0] === name ? decodeURIComponent(parts[1]) : r;
    }, '');
}

// 主函数
function initToggleButton() {
    const button = addToggleButton();
    let isHOTA = getCookie('isHOTA') !== 'false';

    function updateButtonText() {
        button.querySelector('.hide-title').textContent = isHOTA ? '切换至死亡阴影' : '切换至深渊号角';
    }

    function toggleVersion() {
        isHOTA = !isHOTA;
        toggleClasses(isHOTA);
        updateButtonText();
        setCookie('isHOTA', isHOTA, 30);
    }

    button.addEventListener('click', (e) => {
        e.preventDefault();
        toggleVersion();
    });

    // 初始化
    toggleClasses(isHOTA);
    updateButtonText();
}

// 当DOM加载完成后执行初始化
document.addEventListener('DOMContentLoaded', initToggleButton);