`, "text/html").body;
const h2Elements = Array.from(decoded.querySelectorAll('h2'));
// 移除最后一个 h2 及其后面的所有内容
if (h2Elements.length > 0) {
const lastH2 = h2Elements[h2Elements.length - 1];
let nodeToRemove = lastH2;
while (nodeToRemove && nodeToRemove !== decoded.lastChild) {
const next = nodeToRemove.nextElementSibling;
if (next && next.tagName.toLowerCase() !== 'h2') {
next.remove();
} else {
break;
}
}
lastH2.remove();
h2Elements.pop(); // 同步更新数组
}
const result = [];
h2Elements.forEach((h2, index) => {
const currentTitle = h2.textContent.trim();
const currentContent = [];
let nextNode = h2.nextElementSibling;
while (nextNode && nextNode.tagName.toLowerCase() !== 'h2') {
if (nextNode.tagName.toLowerCase() === 'img') {
const imgSrc = nextNode.getAttribute('src');
if (imgSrc) {
nextNode.setAttribute('src', fixImagePath(imgSrc));
}
}
currentContent.push(nextNode.outerHTML.trim());
nextNode = nextNode.nextElementSibling;
}
const cleanedContent = currentContent.join('\n')
.replace(/
/gi, '/cheshenfangandechanpinyufuwu337/')
.replace(/
/gi, '') .trim(); result.push({ title: currentTitle, content: cleanedContent, id: `tab-pane-${index}` }); console.log(`目录 ${index + 1}:${currentTitle} =====`); console.log(cleanedContent); console.log('------------------------------------------'); }); function fixImagePath(src) { if (src.startsWith('/')) return window.location.origin + src; return src; } const navTabs = document.getElementById('myTab'); const tabContent = document.getElementById('tabContentContainer'); result.forEach((group, index) => { const tab = document.createElement('a'); tab.className = 'nav-link' + (index === 0 ? ' active' : ''); tab.href = `#${group.id}`; tab.innerText = group.title; navTabs.appendChild(tab); const pane = document.createElement('div'); pane.className = 'tab-pane' + (index === 0 ? ' active' : ''); pane.id = group.id; pane.innerHTML = group.content; tabContent.appendChild(pane); }); document.querySelectorAll('.tabbedSystem .nav-link').forEach(tab => { tab.addEventListener('click', (e) => { e.preventDefault(); document.querySelectorAll('.tabbedSystem .nav-link').forEach(t => t.classList.remove('active')); tab.classList.add('active'); const targetId = tab.getAttribute('href').replace('#', ''); document.querySelectorAll('#tabContentContainer .tab-pane').forEach(pane => { pane.classList.remove('active'); }); const activePane = document.getElementById(targetId); if (activePane) { activePane.classList.add('active'); } }); }); });
`, "text/html").body; const h2Elements = Array.from(decoded.querySelectorAll('h2')); if (h2Elements.length === 0) return; const lastH2 = h2Elements[h2Elements.length - 1]; const lastTitle = lastH2.textContent.trim(); const paragraphs = []; let nextNode = lastH2.nextElementSibling; while (nextNode && nextNode.tagName.toLowerCase() !== 'h2') { if (nextNode.tagName.toLowerCase() === 'p') { const cleanP = nextNode.cloneNode(true); cleanP.querySelectorAll('br, hr').forEach(tag => tag.remove()); paragraphs.push(cleanP); } nextNode = nextNode.nextElementSibling; } const container = document.getElementById("lastSectionContainer"); // 插入最上方的大标题 const titleElement = document.createElement('h2'); titleElement.className = 'main-title'; titleElement.textContent = lastTitle; container.appendChild(titleElement); paragraphs.forEach((p, idx) => { const paraText = p.innerHTML.trim(); // 删除
中的图片标签 const cleanParaText = removeImagesFromText(paraText); const imgTags = Array.from(p.querySelectorAll('img')); const imgSrcs = imgTags.map(img => fixImagePath(img.getAttribute('src'))); const sectionId = `dynamic-section-${idx}`; const wrapper = document.createElement('div'); wrapper.className = 'scroll-section'; wrapper.id = sectionId; wrapper.innerHTML = `
${cleanParaText}
`;
container.appendChild(wrapper);
});
function fixImagePath(src) {
if (!src) return '';
if (src.startsWith('/')) return window.location.origin + src;
return src;
}
function removeImagesFromText(text) {
const div = document.createElement('div');
div.innerHTML = text;
// 删除 div 中所有的 标签
const imgs = div.querySelectorAll('img');
imgs.forEach(img => img.remove());
return div.innerHTML; // 返回清理后的 HTML
}
// 滚动监听逻辑
window.addEventListener('scroll', () => {
document.querySelectorAll('.scroll-section').forEach(section => {
const rect = section.getBoundingClientRect();
const visibleRatio = Math.min(1, Math.max(0, (window.innerHeight - rect.top) / rect.height));
const imgs = section.querySelectorAll('.scroll-img');
const totalImgs = imgs.length;
if (totalImgs === 0) return;
const index = Math.min(totalImgs - 1, Math.floor(visibleRatio * totalImgs));
imgs.forEach((img, i) => {
img.classList.toggle('active', i === index);
});
});
});
});
留言板