mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-20 19:02:33 +00:00
parent
eefc25de1a
commit
e30159875a
@ -337,53 +337,66 @@ NexT.utils = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getScript: (url, {
|
getScript: function(url, options = {}, legacyCondition) {
|
||||||
condition = false,
|
if (typeof options === 'function') {
|
||||||
attributes: {
|
return this.getScript(url, {
|
||||||
id = '',
|
condition: legacyCondition
|
||||||
async = false,
|
}).then(options);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}),
|
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) => {
|
if (id) script.id = id;
|
||||||
const element = document.querySelector(selector);
|
if (crossOrigin) script.crossOrigin = crossOrigin;
|
||||||
if (!CONFIG.comments.lazyload || !element) {
|
script.async = async;
|
||||||
resolve();
|
script.defer = defer;
|
||||||
return;
|
Object.assign(script.dataset, dataset);
|
||||||
}
|
Object.entries(otherAttributes).forEach(([name, value]) => {
|
||||||
const intersectionObserver = new IntersectionObserver((entries, observer) => {
|
script.setAttribute(name, String(value));
|
||||||
const entry = entries[0];
|
});
|
||||||
if (!entry.isIntersecting) return;
|
|
||||||
|
|
||||||
resolve();
|
script.onload = resolve;
|
||||||
observer.disconnect();
|
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