init...
This commit is contained in:
64
themes/butterfly/layout/includes/third-party/comments/disqusjs.pug
vendored
Normal file
64
themes/butterfly/layout/includes/third-party/comments/disqusjs.pug
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
- 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 getCSS('!{url_for(theme.asset.disqusjs_css)}')
|
||||
await 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
|
||||
}
|
||||
})()
|
||||
Reference in New Issue
Block a user