mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-10 21:17:07 +08:00
feat: 適配 hexo-blog-encrypt fix: 修復 hexo 新版本下,prismjs 的問題 improvement: 在 pjax 關閉的情況下。減少不必要的全局變量 improvement: 更新依賴版本 improvement: 頁面進入效果優化 improvement: 添加平滑滾動 improvement: 兼容性優化 improvement: 優化 lighthouse 分數 improvement: 優化代碼
64 lines
2.1 KiB
Plaintext
64 lines
2.1 KiB
Plaintext
- let disqusjsPageTitle = page.title.replace(/'/ig,"\\'")
|
|
- const { shortname:dqShortname, apikey:dqApikey, option:dqOption } = theme.disqusjs
|
|
|
|
script.
|
|
(() => {
|
|
const initDisqusjs = () => {
|
|
window.disqusjs = null
|
|
disqusjs = new DisqusJS(Object.assign({
|
|
shortname: '!{dqShortname}',
|
|
identifier: '!{ url_for(page.path) }',
|
|
url: '!{ page.permalink }',
|
|
title: '!{ disqusjsPageTitle }',
|
|
apikey: '!{dqApikey}',
|
|
},!{JSON.stringify(dqOption)}))
|
|
|
|
disqusjs.render(document.getElementById('disqusjs-wrap'))
|
|
}
|
|
|
|
const themeChange = () => {
|
|
const ele = document.getElementById('disqus_thread')
|
|
if(!ele) return
|
|
disqusjs.destroy()
|
|
initDisqusjs()
|
|
}
|
|
|
|
btf.addGlobalFn('themeChange', themeChange, 'disqusjs')
|
|
|
|
const loadDisqusjs = async() => {
|
|
if (window.disqusJsLoad) initDisqusjs()
|
|
else {
|
|
await btf.getCSS('!{url_for(theme.asset.disqusjs_css)}')
|
|
await btf.getScript('!{url_for(theme.asset.disqusjs)}')
|
|
initDisqusjs()
|
|
window.disqusJsLoad = true
|
|
}
|
|
}
|
|
|
|
const getCount = async() => {
|
|
try {
|
|
const eleGroup = document.querySelector('#post-meta .disqusjs-comment-count')
|
|
if (!eleGroup) return
|
|
const cleanedLinks = eleGroup.href.replace(/#post-comment$/, '')
|
|
|
|
const res = await fetch(`https://disqus.com/api/3.0/threads/set.json?forum=!{dqShortname}&api_key=!{dqApikey}&thread:link=${cleanedLinks}`,{
|
|
method: 'GET'
|
|
})
|
|
const result = await res.json()
|
|
const count = result.response.length ? result.response[0].posts : 0
|
|
eleGroup.textContent = count
|
|
} catch (err) {
|
|
console.error(err)
|
|
}
|
|
}
|
|
|
|
if ('!{theme.comments.use[0]}' === 'Disqusjs' || !!{theme.comments.lazyload}) {
|
|
if (!{theme.comments.lazyload}) btf.loadComment(document.getElementById('disqusjs-wrap'), loadDisqusjs)
|
|
else {
|
|
loadDisqusjs()
|
|
!{ theme.comments.count ? 'GLOBAL_CONFIG_SITE.isPost && getCount()' : '' }
|
|
}
|
|
} else {
|
|
window.loadOtherComment = loadDisqusjs
|
|
}
|
|
})() |