注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
// 添加新按钮 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);