|
225 | 225 | n.id = "tcomment"; |
226 | 226 | a.appendTo(a.find(".content"), n); |
227 | 227 | }) |
| 228 | + |
| 229 | + // twikoo 初始化控制变量 |
| 230 | + var twikooTimer = null; |
| 231 | + var lastPath = null; |
| 232 | + |
228 | 233 | // 调用 twikoo 评论,正则 path,刷新当前评论 |
229 | 234 | hook.doneEach(function () { |
230 | | - twikoo.init({ |
231 | | - envId: 'https://twikooeo.sicnuwiki.com', |
232 | | - el: '#tcomment', |
233 | | - lang: 'zh-CN', |
234 | | - // path: location.pathname+location.hash.replace(/\?.*/,''), |
235 | | - onCommentLoaded: function () { |
236 | | - window.ViewImage && ViewImage.init('.tk-content img:not(.avatar,.tk-avatar-img,.tk-owo-emotion),.ebook img,a.zoom'); |
237 | | - } |
238 | | - }).then(function () { |
239 | | - console.log('Twikoo 加载完成'); |
240 | | - setTimeout(function() { |
241 | | - // 通过点击刷新按钮,获取当前页评论 |
242 | | - let tk_icon = document.getElementsByClassName("tk-icon")[0]; |
243 | | - tk_icon ? tk_icon.click() : undefined; |
244 | | - },700) |
245 | | - }); |
| 235 | + // 获取当前 docsify 页面路径作为评论区分标识 |
| 236 | + var currentPath = location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/'; |
| 237 | + |
| 238 | + // 如果路径相同,不重复初始化 |
| 239 | + if (lastPath === currentPath) { |
| 240 | + return; |
| 241 | + } |
| 242 | + lastPath = currentPath; |
| 243 | + |
| 244 | + // 取消之前的延迟初始化(防止快速切换页面导致叠加) |
| 245 | + if (twikooTimer) { |
| 246 | + clearTimeout(twikooTimer); |
| 247 | + twikooTimer = null; |
| 248 | + } |
| 249 | + |
| 250 | + // 先清空评论容器,避免叠加 |
| 251 | + var tcomment = document.getElementById('tcomment'); |
| 252 | + if (tcomment) { |
| 253 | + tcomment.innerHTML = ''; |
| 254 | + } |
| 255 | + |
| 256 | + // 延迟加载 twikoo,避免影响页面滑动性能 |
| 257 | + twikooTimer = setTimeout(function() { |
| 258 | + // 再次检查路径是否仍然匹配(用户可能已切换) |
| 259 | + var nowPath = location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/'; |
| 260 | + if (nowPath !== currentPath) { |
| 261 | + return; |
| 262 | + } |
| 263 | + |
| 264 | + twikoo.init({ |
| 265 | + envId: 'https://twikooeo.sicnuwiki.com', |
| 266 | + el: '#tcomment', |
| 267 | + lang: 'zh-CN', |
| 268 | + path: currentPath, |
| 269 | + onCommentLoaded: function () { |
| 270 | + window.ViewImage && ViewImage.init('.tk-content img:not(.avatar,.tk-avatar-img,.tk-owo-emotion),.ebook img,a.zoom'); |
| 271 | + } |
| 272 | + }); |
| 273 | + }, 300); |
246 | 274 | }) |
247 | 275 | } |
248 | 276 | ] |
|
0 commit comments