Hexo个人博客
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.8 KiB

7 months ago
- const disqusPageTitle = page.title.replace(/'/ig,"\\'")
- const { shortname, apikey } = theme.disqus
- const { use, lazyload, count } = theme.comments
script.
(() => {
const disqus_config = function () {
this.page.url = '!{ page.permalink }'
this.page.identifier = '!{ url_for(page.path) }'
this.page.title = '!{ disqusPageTitle }'
}
const disqusReset = () => {
window.DISQUS && window.DISQUS.reset({
reload: true,
config: disqus_config
})
}
btf.addGlobalFn('themeChange', disqusReset, 'disqus')
const loadDisqus = () =>{
if (window.DISQUS) disqusReset()
else {
const script = document.createElement('script')
script.src = 'https://!{shortname}.disqus.com/embed.js'
script.setAttribute('data-timestamp', +new Date())
document.head.appendChild(script)
}
}
const getCount = async() => {
try {
const eleGroup = document.querySelector('#post-meta .disqus-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=!{shortname}&api_key=!{apikey}&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 ('!{use[0]}' === 'Disqus' || !!{lazyload}) {
if (!{lazyload}) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
else {
loadDisqus()
!{ count ? 'GLOBAL_CONFIG_SITE.isPost && getCount()' : '' }
}
} else {
window.loadOtherComment = loadDisqus
}
})()