Upgrade third party js logic improve load speed.

This commit is contained in:
凡梦星尘 2022-10-26 22:37:03 +08:00
parent 90df1e1247
commit 525c8a7cf5
10 changed files with 58 additions and 27 deletions

View File

@ -1,14 +1,42 @@
/* global NexT, boot, CONFIG */
window.NexT = {};
NexT.boot = {};
NexT.comments = {};
NexT.plugins = {};
NexT.comments.register = function() {
// Defined comment component & add register event
NexT.plugins.comments = {};
NexT.plugins.comments.register = function() {
if (!NexT.CONFIG.page.comments) return;
for(var c in NexT.comments) {
for(var c in NexT.plugins.comments) {
if (c === 'register') continue;
eval('NexT.comments.'+c)();
eval('NexT.plugins.comments.'+c)();
};
}
// Defined search engine & add register event
NexT.plugins.search = {}
NexT.plugins.search.register = function() {
for(var s in NexT.plugins.search) {
if (s === 'register') continue;
eval('NexT.plugins.search.'+s)();
};
}
// Defined share plugin & add register event
NexT.plugins.share = {}
NexT.plugins.share.register = function() {
for(var s in NexT.plugins.share) {
if (s === 'register') continue;
eval('NexT.plugins.share.'+s)();
};
}
// Add event to register all third party plugins
NexT.plugins.register = function() {
for(var p in NexT.plugins) {
if (p === 'register') continue;
eval('NexT.plugins.'+p+'.register')();
}
};
(function() {

View File

@ -21,7 +21,7 @@ NexT.boot.registerEvents = function() {
// NexT.utils.registerCanIUseTag();
NexT.utils.registerToolButtons();
// Register comment's components
NexT.comments.register();
NexT.plugins.register();
// Mobile top menu bar.
document.querySelector('.site-nav-toggle .toggle').addEventListener('click', event => {

View File

@ -1,4 +1,5 @@
NexT.comments.giscus = function() {
/* Artalk comment plugin */
NexT.plugins.comments.artalk = function() {
const element = '.artalk-container';
if (!NexT.CONFIG.artalk
|| !NexT.utils.checkDOMExist(element)) return;

View File

@ -1,4 +1,5 @@
NexT.comments.giscus = function() {
/* Giscus comment plugin */
NexT.plugins.comments.giscus = function() {
const element = '.giscus-container';
if (!NexT.CONFIG.page.comments
|| !NexT.CONFIG.giscus

View File

@ -1,4 +1,5 @@
NexT.comments.giscus = function() {
/* LiveRe comment plugin */
NexT.plugins.comments.livere = function() {
const element = '#lv-container';
if (!NexT.CONFIG.livere
|| !NexT.utils.checkDOMExist(element)) return;

View File

@ -1,4 +1,5 @@
NexT.comments.giscus = function() {
/* Utterances comment plugin */
NexT.plugins.comments.utterances = function() {
const element = '.utterances-container';
if (!NexT.CONFIG.utterances
|| !NexT.utils.checkDOMExist(element)) return;

View File

@ -1,4 +1,5 @@
NexT.comments.waline = function() {
/* Waline comment plugin */
NexT.plugins.comments.waline = function() {
const element = '.waline-container';
if (!NexT.CONFIG.waline
|| !NexT.utils.checkDOMExist(element)) return;

View File

@ -1,12 +1,11 @@
/* global instantsearch, algoliasearch, CONFIG, pjax */
/* Algolia search engine */
NexT.plugins.search.algolia = function() {
document.addEventListener('DOMContentLoaded', () => {
const algoiajs = NexT.utils.getCDNResource(NexT.CONFIG.algolia.js);
const algoliajs = NexT.utils.getCDNResource(NexT.CONFIG.algolia.js);
const instantschjs = NexT.utils.getCDNResource(NexT.CONFIG.algolia.instantjs);
NexT.utils.getScript(algoiajs, {});
NexT.utils.getScript(instantschjs, {}).then(() => {
NexT.utils.getScript(algoliajs);
NexT.utils.getScript(instantschjs, function() {
const { indexname, appid, apikey, hits } = NexT.CONFIG.algolia.cfg;
const indexName = indexname;
@ -138,7 +137,6 @@ document.addEventListener('DOMContentLoaded', () => {
onPopupClose();
}
});
});;
});
});
}

View File

@ -1,4 +1,4 @@
/* global CONFIG, pjax, LocalSearch */
/* LocalSearch engine */
class LocalSearch {
constructor({
path = '',
@ -227,7 +227,7 @@ class LocalSearch {
}
}
document.addEventListener('DOMContentLoaded', () => {
NexT.plugins.search.localsearch = function() {
if (! NexT.CONFIG.localSearch.path) {
// Search DB path
console.warn('`search indexes file` is not configurate!');
@ -323,4 +323,4 @@ document.addEventListener('DOMContentLoaded', () => {
onPopupClose();
}
});
});
}

View File

@ -1,16 +1,16 @@
document.addEventListener('DOMContentLoaded', () => {
/* AddThis share plugin */
NexT.plugins.share.addthis = function() {
const element = '.addthis_inline_share_toolbox';
if (!NexT.CONFIG.addthis || !NexT.utils.checkDOMExist(element)) return;
const addthis_js = NexT.CONFIG.addthis.js + '?pubid=' + NexT.CONFIG.addthis.cfg.pubid;
NexT.utils.loadComments(element).then(() => {
NexT.utils.lazyLoadComponent(element, function() {
NexT.utils.getScript(addthis_js, {
attributes: {
async: true
async: false
},
parentNode: document.querySelector(element)
});
});
});
}