mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-19 18:42:34 +00:00
parent
eefc25de1a
commit
e30159875a
@ -337,53 +337,66 @@ NexT.utils = {
|
||||
}
|
||||
},
|
||||
|
||||
getScript: (url, {
|
||||
condition = false,
|
||||
attributes: {
|
||||
id = '',
|
||||
async = false,
|
||||
defer = false,
|
||||
crossOrigin = '',
|
||||
dataset = {},
|
||||
...otherAttributes
|
||||
} = {},
|
||||
parentNode = null
|
||||
} = {}) => new Promise((resolve, reject) => {
|
||||
if (condition) {
|
||||
resolve();
|
||||
} else {
|
||||
const script = document.createElement('script');
|
||||
|
||||
if (id) script.id = id;
|
||||
if (crossOrigin) script.crossOrigin = crossOrigin;
|
||||
script.async = async;
|
||||
script.defer = defer;
|
||||
Object.assign(script.dataset, dataset);
|
||||
Object.entries(otherAttributes).forEach(([name, value]) => {
|
||||
script.setAttribute(name, String(value));
|
||||
});
|
||||
|
||||
script.onload = resolve;
|
||||
script.onerror = reject;
|
||||
|
||||
script.src = url;
|
||||
(parentNode || document.head).appendChild(script);
|
||||
getScript: function(url, options = {}, legacyCondition) {
|
||||
if (typeof options === 'function') {
|
||||
return this.getScript(url, {
|
||||
condition: legacyCondition
|
||||
}).then(options);
|
||||
}
|
||||
}),
|
||||
const {
|
||||
condition = false,
|
||||
attributes: {
|
||||
id = '',
|
||||
async = false,
|
||||
defer = false,
|
||||
crossOrigin = '',
|
||||
dataset = {},
|
||||
...otherAttributes
|
||||
} = {},
|
||||
parentNode = null
|
||||
} = options;
|
||||
return new Promise((resolve, reject) => {
|
||||
if (condition) {
|
||||
resolve();
|
||||
} else {
|
||||
const script = document.createElement('script');
|
||||
|
||||
loadComments: (selector) => new Promise((resolve) => {
|
||||
const element = document.querySelector(selector);
|
||||
if (!CONFIG.comments.lazyload || !element) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
const intersectionObserver = new IntersectionObserver((entries, observer) => {
|
||||
const entry = entries[0];
|
||||
if (!entry.isIntersecting) return;
|
||||
if (id) script.id = id;
|
||||
if (crossOrigin) script.crossOrigin = crossOrigin;
|
||||
script.async = async;
|
||||
script.defer = defer;
|
||||
Object.assign(script.dataset, dataset);
|
||||
Object.entries(otherAttributes).forEach(([name, value]) => {
|
||||
script.setAttribute(name, String(value));
|
||||
});
|
||||
|
||||
resolve();
|
||||
observer.disconnect();
|
||||
script.onload = resolve;
|
||||
script.onerror = reject;
|
||||
|
||||
script.src = url;
|
||||
(parentNode || document.head).appendChild(script);
|
||||
}
|
||||
});
|
||||
intersectionObserver.observe(element);
|
||||
})
|
||||
},
|
||||
|
||||
loadComments: function(selector, legacyCallback) {
|
||||
if (legacyCallback) {
|
||||
return this.loadComments(selector).then(legacyCallback);
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const element = document.querySelector(selector);
|
||||
if (!CONFIG.comments.lazyload || !element) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
const intersectionObserver = new IntersectionObserver((entries, observer) => {
|
||||
const entry = entries[0];
|
||||
if (!entry.isIntersecting) return;
|
||||
|
||||
resolve();
|
||||
observer.disconnect();
|
||||
});
|
||||
intersectionObserver.observe(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user