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://…”
 
无编辑摘要
 
(未显示同一用户的7个中间版本)
第1行: 第1行:
/* Any JavaScript here will be loaded for all users on every page load. */
// 添加新按钮
function addToggleButton() {
   var navbarNav = document.querySelector('ul.nav.navbar-nav');
   if (!navbarNav) {
     console.error('未找到 ul.nav.navbar-nav 元素');
     return null;
   }
   var 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>';
   navbarNav.appendChild(newButton);
   console.log('新按钮已添加到导航栏');
   return document.getElementById('toggleButton');
}


(function () {
// 切换显示/隐藏类
 
function toggleClasses(showHOTA) {
var heroesStyleSheet = new CSSStyleSheet();
   var hotaElements = document.querySelectorAll('.onlyhota');
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}');
   var sodElements = document.querySelectorAll('.onlysod');
 
  
function getCookie(cname) {
   for (var i = 0; i < hotaElements.length; i++) {
  var name = cname + '=';
     hotaElements[i].style.display = showHOTA ? '' : 'none';
  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) {
    for (var j = 0; j < sodElements.length; j++) {
    return c.substring(name.length, c.length);
     sodElements[j].style.display = showHOTA ? 'none' : '';
    }
    }
  }
  return '';
}
}


function showElements(desc, linkSelector) {
// 切换显示/隐藏类
  var elems = document.querySelectorAll(desc);
function toggleTemplate(showHOTA) {
  for (var i = 0; i < elems.length; i++) {
   // 获取 #mw-customcollapsible-hota 下的 .mw-collapsible-content 元素
    elems[i].style = '';
   var hotaElements = document.querySelectorAll('#mw-customcollapsible-hota .mw-collapsible-content');
  }
   // 获取 #mw-customcollapsible-sod 下的 .mw-collapsible-content 元素
  elems = document.querySelectorAll('.' + linkSelector + desc);
    var sodElements = document.querySelectorAll('#mw-customcollapsible-sod .mw-collapsible-content');
  for (i = 0; i < elems.length; i++) {
   elems[i].style = 'user-select:none; cursor: pointer; color: rgb(102, 177, 250);';
  }
}


function hideElements(desc) {
   // 切换 HOTA 模板内容的显示/隐藏
  var elems = document.querySelectorAll(desc);
   for (var i = 0; i < hotaElements.length; i++) {
  for (var i = 0; i < elems.length; i++) {
     hotaElements[i].style.display = showHOTA ? '' : 'none';
   elems[i].style = 'display:none;';
   }
  }
  elems = document.querySelectorAll('.initialOnly');
  for (i = 0; i < elems.length; i++) {
   elems[i].style = 'display:none;';
  }
}


function togglePreference(linkSelector, cookieName, cookie1, cookie2, link1, link2) {
   // 切换 SOD 模板内容的显示/隐藏
  var preference = getCookie(cookieName);
   for (var j = 0; j < sodElements.length; j++) {
  var switchView = document.querySelector('#' + linkSelector);
     sodElements[j].style.display = showHOTA ? 'none' : '';
  var switchView2 = document.querySelector('#' + linkSelector + '2');
    }
  if (preference == cookie2) {
   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() {
// 设置Cookie
  togglePreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', 'Horn of the Abyss');
function setCookie(name, value, days) {
   var expires = new Date(Date.now() + days * 864e5).toUTCString();
   document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=/';
}
}


function togglePreferredView() {
// 获取Cookie
  togglePreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy', 'Legacy/Modern');
function getCookie(name) {
   return document.cookie.split('; ').reduce(function(r, v) {
     var parts = v.split('=');
     return parts[0] === name ? decodeURIComponent(parts[1]) : r;
   }, '');
}
}


function toggleDoR() {
// 主函数
  togglePreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', 'Day of Reckoning');
function initToggleButton() {
}
   console.log(' 初始化切换按钮');
   var button = addToggleButton();
   if (!button) {
     console.error(' 添加按钮失败');
     return;
   }


function setHeroesStyle() {
   var isHOTA = getCookie('isHOTA') !== 'false'; // 修复:将字符串 'false' 转换为布尔值
  var preference = getCookie('heroesStyle');
  if (preference == 'heroesStyleEnabled') {
   document.adoptedStyleSheets.push(heroesStyleSheet);
  } else {
   while (document.adoptedStyleSheets.pop());
  }
}


function toggleHeroesStyle() {
   function updateButtonText() {
  togglePreference('switchHeroesStyle', 'heroesStyle', 'heroesStyleDisabled', 'heroesStyleEnabled', 'Disable H3CSS', 'Enable H3CSS', 'Heroes Style');
     button.querySelector('.hide-title').textContent = isHOTA ? ' 切换至死亡阴影' : ' 切换至深渊号角';
  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');
     toggleTemplate(isHOTA);
  }
     updateButtonText();
  if (userlink) {
     setCookie('isHOTA', isHOTA, 30); // 修复:将布尔值存储为字符串 'true' 或 'false'
   var switchView = document.querySelector('#' + linkSelector);
     console.log(' 版本已切换,当前状态:', isHOTA ? 'HOTA' : 'SOD');
   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', function(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++) {
   toggleTemplate(isHOTA);
    elems[i].title = '';
   updateButtonText();
  }
    console.log(' 切换按钮初始化完成');
}
}


function initCommon() {
// 确保DOM完全加载后执行初始化
  initPreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', 'Horn of the Abyss', togglePreferredExpansion);
if (document.readyState === 'loading') {
  initPreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', 'Day of Reckoning', toggleDoR);
    document.addEventListener('DOMContentLoaded', initToggleButton);
  var mainpage = document.querySelector('.mainpage.onlysod.onlyhota');
} else {
  if (mainpage) {
   initToggleButton();
    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();
// 添加一个全局函数用于手动触发初始化(以防其他方法失败)
 
window.manualInitToggleButton = initToggleButton;
})();

2024年9月24日 (二) 08:46的最新版本

// 添加新按钮
function addToggleButton() {
    var navbarNav = document.querySelector('ul.nav.navbar-nav');
    if (!navbarNav) {
        console.error('未找到 ul.nav.navbar-nav 元素');
        return null;
    }
    var 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>';
    navbarNav.appendChild(newButton);
    console.log('新按钮已添加到导航栏');
    return document.getElementById('toggleButton');
}

// 切换显示/隐藏类
function toggleClasses(showHOTA) {
    var hotaElements = document.querySelectorAll('.onlyhota');
    var sodElements = document.querySelectorAll('.onlysod');
    
    for (var i = 0; i < hotaElements.length; i++) {
        hotaElements[i].style.display = showHOTA ? '' : 'none';
    }
    for (var j = 0; j < sodElements.length; j++) {
        sodElements[j].style.display = showHOTA ? 'none' : '';
    }
}

// 切换显示/隐藏类
function toggleTemplate(showHOTA) {
    // 获取 #mw-customcollapsible-hota 下的 .mw-collapsible-content 元素
    var hotaElements = document.querySelectorAll('#mw-customcollapsible-hota .mw-collapsible-content');
    // 获取 #mw-customcollapsible-sod 下的 .mw-collapsible-content 元素
    var sodElements = document.querySelectorAll('#mw-customcollapsible-sod .mw-collapsible-content');

    // 切换 HOTA 模板内容的显示/隐藏
    for (var i = 0; i < hotaElements.length; i++) {
        hotaElements[i].style.display = showHOTA ? '' : 'none';
    }

    // 切换 SOD 模板内容的显示/隐藏
    for (var j = 0; j < sodElements.length; j++) {
        sodElements[j].style.display = showHOTA ? 'none' : '';
    }
}

// 设置Cookie
function setCookie(name, value, days) {
    var 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(function(r, v) {
        var parts = v.split('=');
        return parts[0] === name ? decodeURIComponent(parts[1]) : r;
    }, '');
}

// 主函数
function initToggleButton() {
    console.log('初始化切换按钮');
    var button = addToggleButton();
    if (!button) {
        console.error('添加按钮失败');
        return;
    }

    var isHOTA = getCookie('isHOTA') !== 'false'; // 修复:将字符串 'false' 转换为布尔值

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

    function toggleVersion() {
        isHOTA = !isHOTA;
        toggleClasses(isHOTA);
        toggleTemplate(isHOTA);
        updateButtonText();
        setCookie('isHOTA', isHOTA, 30); // 修复:将布尔值存储为字符串 'true' 或 'false'
        console.log('版本已切换,当前状态:', isHOTA ? 'HOTA' : 'SOD');
    }

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

    // 初始化
    toggleClasses(isHOTA);
    toggleTemplate(isHOTA);
    updateButtonText();
    console.log('切换按钮初始化完成');
}

// 确保DOM完全加载后执行初始化
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initToggleButton);
} else {
    initToggleButton();
}

// 添加一个全局函数用于手动触发初始化(以防其他方法失败)
window.manualInitToggleButton = initToggleButton;