Improved the comments plug-in load function, close #12

This commit is contained in:
凡梦星尘
2022-07-13 21:25:15 +08:00
parent bf55ada93c
commit 81cff8a5c2
50 changed files with 405 additions and 326 deletions

View File

@@ -0,0 +1,36 @@
document.addEventListener('DOMContentLoaded', () => {
const element = '.artalk-container';
if (!NexT.CONFIG.comments.enable
|| !NexT.CONFIG.artalk
|| !NexT.utils.checkDOMExist(element)) return;
const artalk_css = NexT.utils.getCDNResource(NexT.CONFIG.artalk.css);
NexT.utils.getStyle(artalk_css, null);
const artalk_js = NexT.utils.getCDNResource(NexT.CONFIG.artalk.js);
const {
site,
placeholder,
server,
} = NexT.CONFIG.artalk.config;
NexT.utils.loadComments(element)
.then(() => NexT.utils.getScript(artalk_js, {
}))
.then(() => {
new Artalk({
el : element,
pageKey : NexT.CONFIG.permalink,
pageTitle : NexT.CONFIG.title,
server : server,
site : site,
locale : NexT.CONFIG.lang,
placeholder : placeholder,
darkMode : 'auto'
});
NexT.utils.hiddeLodingCmp(element);
});
});

View File

@@ -1,39 +0,0 @@
/* global NexT, CONFIG */
document.addEventListener('page:loaded', () => {
const { appid, appkey } = CONFIG.changyan;
const mainJs = 'https://cy-cdn.kuaizhan.com/upload/changyan.js';
const countJs = `https://cy-cdn.kuaizhan.com/upload/plugins/plugins.list.count.js?clientId=${appid}`;
// Get the number of comments
setTimeout(() => {
return NexT.utils.getScript(countJs, {
attributes: {
async: true,
id : 'cy_cmt_num'
}
});
}, 0);
// When scroll to comment section
if (CONFIG.page.comments && !CONFIG.page.isHome) {
NexT.utils.loadComments('#SOHUCS')
.then(() => {
return NexT.utils.getScript(mainJs, {
attributes: {
async: true
}
});
})
.then(() => {
window.changyan.api.config({
appid,
conf: appkey
});
})
.catch(error => {
// eslint-disable-next-line no-console
console.error('Failed to load Changyan', error);
});
}
});

View File

@@ -1,41 +0,0 @@
/* global NexT, CONFIG, DISQUS */
document.addEventListener('page:loaded', () => {
if (CONFIG.disqus.count) {
const loadCount = () => {
NexT.utils.getScript(`https://${CONFIG.disqus.shortname}.disqus.com/count.js`, {
attributes: { id: 'dsq-count-scr' }
});
};
// defer loading until the whole page loading is completed
window.addEventListener('load', loadCount, false);
}
if (CONFIG.page.comments) {
// `disqus_config` should be a global variable
// See https://help.disqus.com/en/articles/1717084-javascript-configuration-variables
window.disqus_config = function() {
this.page.url = CONFIG.page.permalink;
this.page.identifier = CONFIG.page.path;
this.page.title = CONFIG.page.title;
if (CONFIG.disqus.i18n.disqus !== 'disqus') {
this.language = CONFIG.disqus.i18n.disqus;
}
};
NexT.utils.loadComments('#disqus_thread').then(() => {
if (window.DISQUS) {
DISQUS.reset({
reload: true,
config: window.disqus_config
});
} else {
NexT.utils.getScript(`https://${CONFIG.disqus.shortname}.disqus.com/embed.js`, {
attributes: { dataset: { timestamp: '' + +new Date() } }
});
}
});
}
});

View File

@@ -1,18 +0,0 @@
/* global NexT, CONFIG, DisqusJS */
document.addEventListener('page:loaded', () => {
if (!CONFIG.page.comments) return;
NexT.utils.loadComments('#disqus_thread')
.then(() => NexT.utils.getScript(CONFIG.disqusjs.js, { condition: window.DisqusJS }))
.then(() => {
window.dsqjs = new DisqusJS({
api : CONFIG.disqusjs.api || 'https://disqus.com/api/',
apikey : CONFIG.disqusjs.apikey,
shortname : CONFIG.disqusjs.shortname,
url : CONFIG.page.permalink,
identifier: CONFIG.page.path,
title : CONFIG.page.title
});
});
});

View File

@@ -0,0 +1,41 @@
document.addEventListener('DOMContentLoaded', () => {
const element = '.giscus-container';
if (!NexT.CONFIG.comments.enable
|| !NexT.CONFIG.giscus
|| !NexT.utils.checkDOMExist(element)) return;
const {
category,
categoryid,
emit,
inputposition,
mapping,
reactions,
repo,
repoid,
theme } = NexT.CONFIG.giscus.config;
NexT.utils.loadComments(element)
.then(() => NexT.utils.getScript(NexT.CONFIG.giscus.js, {
attributes: {
'async' : true,
'crossorigin' : 'anonymous',
'data-repo' : repo,
'data-repo-id' : repoid,
'data-category' : category,
'data-category-id' : categoryid,
'data-mapping' : mapping,
'data-reactions-enabled' : reactions ? 1:0,
'data-emit-metadata' : emit ? 1:0,
'data-input-position' : inputposition,
'data-theme' : theme,
'data-lang' : NexT.CONFIG.lang,
'data-loading' : 'lazy'
},
parentNode: document.querySelector(element)
}));
NexT.utils.hiddeLodingCmp(element);
});

View File

@@ -1,24 +0,0 @@
/* global NexT, CONFIG, Gitalk */
document.addEventListener('page:loaded', () => {
if (!CONFIG.page.comments) return;
NexT.utils.loadComments('.gitalk-container')
.then(() => NexT.utils.getScript(CONFIG.gitalk.js, {
condition: window.Gitalk
}))
.then(() => {
const gitalk = new Gitalk({
clientID : CONFIG.gitalk.client_id,
clientSecret : CONFIG.gitalk.client_secret,
repo : CONFIG.gitalk.repo,
owner : CONFIG.gitalk.github_id,
admin : [CONFIG.gitalk.admin_user],
id : CONFIG.gitalk.path_md5,
proxy : CONFIG.gitalk.proxy,
language : CONFIG.gitalk.language || window.navigator.language,
distractionFreeMode: CONFIG.gitalk.distraction_free_mode
});
gitalk.render(document.querySelector('.gitalk-container'));
});
});

View File

@@ -1,15 +0,0 @@
/* global NexT, CONFIG */
document.addEventListener('page:loaded', () => {
if (!CONFIG.page.comments) return;
NexT.utils.loadComments('#isso-thread')
.then(() => NexT.utils.getScript(`${CONFIG.isso}js/embed.min.js`, {
attributes: {
dataset: {
isso: `${CONFIG.isso}`
}
},
parentNode: document.querySelector('#isso-thread')
}));
});

View File

@@ -1,19 +1,18 @@
/* global NexT, CONFIG, LivereTower */
document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('page:loaded', () => {
if (!CONFIG.page.comments) return;
const element = '#lv-container';
if (!NexT.CONFIG.comments.enable
|| !NexT.CONFIG.livere
|| !NexT.utils.checkDOMExist(element)) return;
NexT.utils.loadComments('#lv-container').then(() => {
window.livereOptions = {
refer: CONFIG.page.path.replace(/index\.html$/, '')
};
if (typeof LivereTower === 'function') return;
NexT.utils.getScript('https://cdn-city.livere.com/js/embed.dist.js', {
NexT.utils.loadComments(element).then(() => {
NexT.utils.getScript(NexT.CONFIG.livere.js, {
attributes: {
async: true
}
},
parentNode: document.querySelector(element)
});
NexT.utils.hiddeLodingCmp(element);
});
});

View File

@@ -1,17 +1,27 @@
/* global NexT, CONFIG */
document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('page:loaded', () => {
if (!CONFIG.page.comments) return;
const element = '.utterances-container';
if (!NexT.CONFIG.comments.enable
|| !NexT.CONFIG.utterances
|| !NexT.utils.checkDOMExist(element)) return;
NexT.utils.loadComments('.utterances-container')
.then(() => NexT.utils.getScript('https://utteranc.es/client.js', {
const {
repo,
issueterm,
label,
theme } = NexT.CONFIG.utterances.config;
NexT.utils.loadComments(element)
.then(() => NexT.utils.getScript(NexT.CONFIG.utterances.js, {
attributes: {
async : true,
crossOrigin : 'anonymous',
'repo' : CONFIG.utterances.repo,
'issue-term': CONFIG.utterances.issue_term,
'theme' : CONFIG.utterances.theme
'async' : true,
'crossorigin' : 'anonymous',
'repo' : repo,
'issue-term' : issueterm,
'theme' : theme
},
parentNode: document.querySelector('.utterances-container')
parentNode: document.querySelector(element)
}));
NexT.utils.hiddeLodingCmp(element);
});

View File

@@ -0,0 +1,48 @@
document.addEventListener('DOMContentLoaded', () => {
const element = '.waline-container';
if (!NexT.CONFIG.comments.enable
|| !NexT.CONFIG.waline
|| !NexT.utils.checkDOMExist(element)) return;
const {
emoji,
imguploader,
pageview,
placeholder,
requiredmeta,
serverurl,
wordlimit
} = NexT.CONFIG.waline.config;
const waline_css = NexT.utils.getCDNResource(NexT.CONFIG.waline.css);
NexT.utils.getStyle(waline_css, null);
const waline_js = NexT.utils.getCDNResource(NexT.CONFIG.waline.js);
const locale = {
placeholder: placeholder
};
NexT.utils.loadComments(element)
.then(() => NexT.utils.getScript(waline_js, {
}))
.then(() => {
Waline.init({
locale,
el : element,
pageview : pageview,
emoji : emoji,
imageUploader : imguploader,
wordLimit : wordlimit,
requiredMeta : requiredmeta,
serverURL : serverurl,
lang : NexT.CONFIG.lang,
dark : "auto"
});
NexT.utils.hiddeLodingCmp(element);
});
});