一起泡书吧
首页 分类 书架
'; fetch(document.querySelector('.reader-home').getAttribute('href')) .then(function(r){ return r.text(); }) .then(function(html){ var doc=new DOMParser().parseFromString(html,'text/html'); var ul=doc.querySelector('#chapterlist'); if(!ul){ tocBody.innerHTML='
未找到章节列表
'; return; } var clone=ul.cloneNode(true); var items=clone.querySelectorAll('li'); for(var i=0;i-1) links[j].classList.add('active'); } tocBody.scrollTop=0; tocLoaded=true; }) .catch(function(){ tocBody.innerHTML='
目录加载失败,请刷新重试
'; }); } $('tocFilter').oninput=function(){ var q=this.value.trim(); var items=tocBody.querySelectorAll('.chapter-list a'); for(var i=0;i-1)?'':'none'; } }; /* 目录内点击:跳转前关闭抽屉(保留位置由目标页自行处理) */ tocBody.addEventListener('click',function(e){ var a=e.target.closest? e.target.closest('a'):null; if(a) closeDrawer(); }); /* ---------- 书签 ---------- */ var BMKEY='yg_bm_'+art.dataset.oid; function getBm(){ try{ return JSON.parse(localStorage.getItem(BMKEY))||[]; }catch(e){ return []; } } function setBm(a){ try{ localStorage.setItem(BMKEY,JSON.stringify(a)); }catch(e){} } function isBookmarked(){ var a=getBm(); for(var i=0;i-1){ a.splice(i,1); setBm(a); $('btn-bm').classList.remove('active'); updateBmBadge(); toast('已取消书签'); } else{ a.unshift({url:location.href,title:document.querySelector('.reader-chapter').textContent}); setBm(a); $('btn-bm').classList.add('active'); updateBmBadge(); toast('已收藏当前章节'); } }; if(isBookmarked()) $('btn-bm').classList.add('active'); updateBmBadge(); /* ---------- 设置面板 ---------- */ var sheet=$('sheet'), sheetMask=$('sheetMask'); function openSheet(){ sheet.classList.add('open'); sheetMask.classList.add('open'); } function closeSheet(){ sheet.classList.remove('open'); sheetMask.classList.remove('open'); } $('btn-aa').onclick=openSheet; $('sheetClose').onclick=closeSheet; sheetMask.onclick=closeSheet; /* ---------- toast ---------- */ var toastTimer=null; function toast(msg){ var t=$('toast'); t.textContent=msg; t.classList.add('show'); clearTimeout(toastTimer); toastTimer=setTimeout(function(){ t.classList.remove('show'); },1600); } /* ---------- 自动连读 ---------- */ var loadingNext=false; var autoNextTriggered=false; function getNextHref(){ var el=$('nextLink'); return el?el.getAttribute('href'):''; } function maybeAutoNext(){ if(loadingNext || autoNextTriggered) return; var href=getNextHref(); if(!href) return; var doc=document.documentElement, vh=window.innerHeight; var scrolled=doc.scrollTop+vh; var total=doc.scrollHeight; /* 只有滚动到距离底部50像素以内才触发 */ if(scrolled >= total - 50){ autoNextTriggered=true; loadingNext=true; toast('正在加载下一章…'); try{ localStorage.setItem(POSKEY,'0'); }catch(e){} setTimeout(function(){ location.href=href; }, 300); } } $('autoread').onchange=function(){ PREF.autoread=this.checked; savePref(); autoNextTriggered=false; if(this.checked){ toast('自动连读已开启,滚到底部自动跳转下一章'); } }; /* ---------- 内容加载(ajaxchapter) ---------- */ function loadChapter(id, cid, eKey, nextpage, pagehash, append, done){ var fd='id='+encodeURIComponent(id)+'&cid='+encodeURIComponent(cid)+'&eKey='+encodeURIComponent(eKey); if(nextpage){ fd+='&nextpage='+encodeURIComponent(nextpage)+'&pagehash='+encodeURIComponent(pagehash); } fetch(art.dataset.api,{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:fd}) .then(function(r){ return r.json(); }) .then(function(d){ if(d && d.status==='success' && d.info && d.info.content && d.info.content.length>3){ if(append){ art.insertAdjacentHTML('beforeend', d.info.content); } else { art.className='reader-article'; art.innerHTML=d.info.content; if(PREF.readmode==='page') buildPages(); restorePos(); } if(d.nextpage){ loadChapter(id, cid, eKey, d.nextpage, d.hash, true, done); } else if(done){ done(); } } else if(!append){ art.className='reader-article'; art.innerHTML='

本章内容暂未获取到(采集源未返回内容)。

'; if(done) done(); } }) .catch(function(){ if(!append){ art.className='reader-article'; art.innerHTML='

内容加载失败,请刷新重试。

'; } if(done) done(); }); } if(art && art.textContent.indexOf('加载中')!==-1){ art.className='reader-article reader-loading'; loadChapter(art.dataset.oid, art.dataset.ocid, art.dataset.ekey, '', '', false); } /* ---------- 初始化 ---------- */ applyPref(); $('autoread').checked=PREF.autoread; if(PREF.autoread) maybeAutoNext(); /* 上一章/下一章链接:原始章节行无 rewriteurl,用 canonicalurl + cid 还原 */ (function(){ var base=art.dataset.canon||''; function chapUrl(c){ c=parseInt(c,10); if(isNaN(c)||c<0) return ''; return base.replace(/\/(\d+)\.html$/, '/'+c+'.html'); } var pl=$('prevLink'), nl=$('nextLink'); if(pl) pl.href=chapUrl(pl.dataset.cid); if(nl) nl.href=chapUrl(nl.dataset.cid); })(); setTimeout(function(){ restorePos(); if(PREF.readmode==='page') buildPages(); }, 80); window.addEventListener('resize',function(){ applyPref(); if(PREF.readmode==='page'){ teardownPages(); buildPages(); } }); })();