window.WebAssembly = undefined; const url = new URL(window.location.href); const pathParts = url.pathname.split('/'); const UUID = pathParts.pop() || pathParts.pop(); // DOM元素 const sidebar = document.getElementById('sidebar'); const sidebarToggle = document.getElementById('sidebar-toggle'); const overlay = document.getElementById('overlay'); const sidebarSearch = document.getElementById('sidebar-search'); const themeToggle = document.getElementById('theme-toggle'); const articleContainer = document.getElementById('article-container'); const sidebarNav = document.getElementById('sidebar-nav'); const globalSearch = document.getElementById('global-search'); const searchButton = document.getElementById('search-button'); const headerTitle = document.getElementById('header-title'); // 初始化Marked.js if (window.marked) marked.setOptions({ highlight: function (code, lang) { if (lang && hljs.getLanguage(lang)) { return hljs.highlight(code, { language: lang }).value; } return hljs.highlightAuto(code).value; }, breaks: true, }); // 渲染侧边栏导航 function renderSidebar() { sidebarNav.innerHTML = ''; tableOfContents.chapters.forEach(chapter => { const chapterEl = document.createElement('div'); chapterEl.className = 'mb-1'; const chapterHeader = document.createElement('div'); chapterHeader.className = 'flex items-center justify-between p-2 rounded-lg hover:bg-gray-100 cursor-pointer transition-all duration-300'; chapterHeader.innerHTML = ` ${chapter.title} `; const chapterContent = document.createElement('div'); chapterContent.className = 'pl-4 mt-1 space-y-1 overflow-hidden max-h-0 transition-all duration-300'; // 添加章节链接 const chapterLink = document.createElement('a'); chapterLink.href = `#${chapter.id}`; chapterLink.className = 'block p-2 rounded-lg hover:bg-gray-100 text-sm transition-all duration-300'; chapterLink.textContent = chapter.title; chapterLink.addEventListener('click', (e) => { e.preventDefault(); loadArticleAsync(chapter.id); closeSidebar(); highlightActiveLink(chapter.id); }); chapterContent.appendChild(chapterLink); // 添加子章节链接 if (chapter.sections && chapter.sections.length > 0) { chapter.sections.forEach(section => { const sectionLink = document.createElement('a'); sectionLink.href = `#${section.id}`; sectionLink.className = 'block p-2 rounded-lg hover:bg-gray-100 text-sm transition-all duration-300'; sectionLink.innerHTML = ` ${section.title} `; sectionLink.addEventListener('click', (e) => { e.preventDefault(); loadArticleAsync(section.id); closeSidebar(); highlightActiveLink(section.id); }); chapterContent.appendChild(sectionLink); }); } chapterHeader.addEventListener('click', () => { const isExpanded = chapterContent.classList.contains('expanded'); if (isExpanded) { chapterContent.classList.remove('expanded'); chapterContent.style.maxHeight = '0'; chapterHeader.querySelector('.fa-chevron-down').style.transform = 'rotate(0deg)'; } else { chapterContent.classList.add('expanded'); chapterContent.style.maxHeight = chapterContent.scrollHeight + 'px'; chapterHeader.querySelector('.fa-chevron-down').style.transform = 'rotate(180deg)'; } }); chapterEl.appendChild(chapterHeader); chapterEl.appendChild(chapterContent); sidebarNav.appendChild(chapterEl); }); } // 异步加载文章内容 async function loadArticleAsync(id, password = null) { // 显示现代化加载动画 showLoadingAnimation(); // 找到侧边栏中对应的链接并模拟点击 const link = document.querySelector(`#sidebar-nav a[href="#${id}"]`); let apiUrl = `/API/GetDoc/${id}`; if (password) { apiUrl += `?Pwd=${password}`; } try { // 使用fetch API进行异步请求 const response = await fetch(apiUrl); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const content = await response.json(); if (content.DocDecrypt === false) { showPasswordPrompt(content.DocContent, id); } else if (content) { renderArticle(content); } else { showError("未找到文章内容"); } } catch (error) { console.error('Fetch error:', error); showError("加载文章失败,请稍后再试"); } } // 显示加载动画 function showLoadingAnimation() { articleContainer.innerHTML = `
正在加载文章内容...
请尝试刷新页面或联系管理员