无编辑摘要 |
无编辑摘要 |
||
第22行: | 第22行: | ||
var hotaElements = document.querySelectorAll('.onlyhota'); | var hotaElements = document.querySelectorAll('.onlyhota'); | ||
var sodElements = document.querySelectorAll('.onlysod'); | 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) { | |||
var hotaElements = document.querySelectorAll('.mw-customcollapsible-hota'); | |||
var sodElements = document.querySelectorAll('.mw-customcollapsible-sod'); | |||
for (var i = 0; i < hotaElements.length; i++) { | for (var i = 0; i < hotaElements.length; i++) { | ||
第63行: | 第76行: | ||
isHOTA = !isHOTA; | isHOTA = !isHOTA; | ||
toggleClasses(isHOTA); | toggleClasses(isHOTA); | ||
toggleTemplate(isHOTA); | |||
updateButtonText(); | updateButtonText(); | ||
setCookie('isHOTA', isHOTA, 30); | setCookie('isHOTA', isHOTA, 30); | ||
第75行: | 第89行: | ||
// 初始化 | // 初始化 | ||
toggleClasses(isHOTA); | toggleClasses(isHOTA); | ||
toggleTemplate(isHOTA); | |||
updateButtonText(); | updateButtonText(); | ||
console.log('切换按钮初始化完成'); | console.log('切换按钮初始化完成'); |
2024年9月24日 (二) 08:31的版本
// 添加新按钮 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) { var hotaElements = document.querySelectorAll('.mw-customcollapsible-hota'); var sodElements = document.querySelectorAll('.mw-customcollapsible-sod'); 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' : ''; } } // 设置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'; function updateButtonText() { button.querySelector('.hide-title').textContent = isHOTA ? '切换至死亡阴影' : '切换至深渊号角'; } function toggleVersion() { isHOTA = !isHOTA; toggleClasses(isHOTA); toggleTemplate(isHOTA); updateButtonText(); setCookie('isHOTA', isHOTA, 30); 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;