1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| let article, Interval = null, hoverBarrage = false; const comment = { initBarrage(data) { if (!data.length) return; let Index = 0, box = [], dom = document.querySelector('#comment-barrage');
clearInterval(Interval); Interval = null;
Interval = setInterval(() => { if (box.length >= 1 && !hoverBarrage) { removeBarrage(box.shift()); } if(!hoverBarrage){ BarrageBox(data[Index]); Index += 1; Index %= data.length; } }, 5000); $("#menu-commentBarrage span").text("关闭热评"); $("#comment-barrage").hover(function() { hoverBarrage = true; console.log("热评悬浮"); }, function() { hoverBarrage = false; console.log("停止悬浮"); });
async function BarrageBox(data){ const time = await meuicat.changeTime([data.created]); let barrage = document.createElement('div'); barrage.className = 'comment-barrage-item' barrage.innerHTML = ` <div class="barrageHead"> <img class="barrageAvatar" src="${data.avatar}"/> <div class="barrageNick">${data.nick}</div> <div class="barrageTime">评论于${time[0].timeString}</div> <a class="comment-barrage-close" href="javascript:rm.switchCommentBarrage()"><i class="iconfont icat-close"></i></a> </div> <a class="barrageContent" href="javascript:void(0);" onclick="meuicat.scrollTo('${data.id}');">${data.comment}</a> ` box.push(barrage); dom.append(barrage); } function removeBarrage(barrage){ barrage.className = 'comment-barrage-item out'; setTimeout(()=>{ dom.removeChild(barrage); },1000) }
const scrollBarrage = btf.throttle(function(){ let visibleBottom = window.scrollY + document.documentElement.clientHeight, comment = document.getElementById('post-comment'), centerY = comment.offsetTop+(comment.offsetHeight/2);
if(document.body.clientWidth > 768){ if(centerY > visibleBottom){ dom.style.bottom = '40px'; } else { dom.style.bottom = '-200px'; } } }, 200)
document.addEventListener('scroll', scrollBarrage); document.addEventListener('pjax:send', function(){ clearInterval(Interval); document.removeEventListener('scroll', scrollBarrage); }); }, fetchData(options, type, exclude) { fetch('{envId}', { method: "POST", body: JSON.stringify({ "event": "GET_RECENT_COMMENTS", "accessToken": "{YOUR_TOKEN}", "includeReply": true, ...options }), headers: { 'Content-Type': 'application/json' } }).then(res => res.json()).then(response => { let html = '', data = response.data;
if (type === 'total') { const totalBox = document.querySelectorAll('.length-num.comment-total, .N_comments'); totalBox.forEach(element => { if (element.classList.contains('N_comments')) { element.innerText = data.length + '条评论'; } else { element.innerText = data.length; } }); return }; if (exclude) { switch (type) { case 'visitor': data = data.filter(item => item.mailMd5 === exclude); break; case 'v-shield': data = data.filter(item => item.mailMd5 !== exclude); break; case 'a-shield': data = data.filter(item => item.url !== exclude); break; default: break; } }
if (type === 'barrage') return this.initBarrage(data); data.forEach(item => { const createdDate = new Date(item.created); const formattedDate = `${createdDate.getFullYear().toString().slice(-2)}-${createdDate.getMonth() + 1}-${createdDate.getDate()} ${createdDate.getHours()}:${createdDate.getMinutes()}:${createdDate.getSeconds()}`; html += `<div class="comment-card"> <div class="comment-info"> <img src="${item.avatar}" class="nolazyload"> <div class="comment-information"> <span class="${['亦封', '亦小封'].includes(item.nick) ? 'comment-author' : ''} comment-user" data-mailMd5="${item.mailMd5}">${item.nick}</span> <span class="comment-time">${formattedDate}</span> </div> </div> <div class="comment-content">${item.commentText.replaceAll('<', '<').replaceAll('>', '>')}</div> <div class="comment-more"> <div class="comment-title"> <span class="comment-link" title="查看此文章" onclick="pjax.loadUrl('${item.url}')"> <i class="iconfont icat-read"></i> ${article[item.url]} </span> <a onclick="pjax.loadUrl('${item.url}#${item.id}')">查看评论</a> </div> <div class="comment-tool">` let a = `<a href="javascript:void(0)" onclick="comment.article(event)" title="显示此文章所有评论">查看更多</a>`, b = `<a href="javascript:void(0)" onclick="comment.article(event, true)" title="不显示此文章的评论">屏蔽文章</a>`, c = `<a href="javascript:void(0)" onclick="comment.visitor(event, true)" title="不显示该访客的评论">屏蔽Ta</a>`, d = `<a href="javascript:void(0)" onclick="comment.visitor(event)" title="显示该访客的所有评论">查看Ta更多评论</a>` e = `<a href="javascript:void(0)" onclick="comment.data()" title="查看本站最新评论">返回评论</a>`; switch (type) { case 'article': html += e + c + d; break; case 'visitor': html += e + a + b; break; case 'v-shield': case 'a-shield': html += a + b + c + d + e; break; default: if (!type) html += a + b + c + d; break; }
html += `</div> </div> </div>` }); document.getElementById('comments-page').innerHTML = html; }); }, visitor(event, shield) { const spanElement = event.target.closest('.comment-card').querySelector('.comment-user'); const mail = spanElement.getAttribute('data-mailMd5'); if (shield) { this.fetchData({ "pageSize": -1 }, 'v-shield', mail); return } this.fetchData({ "pageSize": -1 }, 'visitor', mail); }, article(event, shield) { const spanElement = event.target.closest('.comment-card').querySelector('.comment-link'); const url = spanElement.getAttribute('onclick').match(/'(\/.*?)'/)[1]; if (shield) { this.fetchData({ "pageSize": -1 }, 'a-shield', url); return } this.fetchData({ "urls": [url] }, 'article'); }, data() { if (!article) { fetch('/articles.json').then(res => res.json()).then(data => { article = {}; [...data.post, ...data.page].forEach(item => { article[item.link] = item.title; }); }); } this.fetchData({ "pageSize": 100 }); }, };
|