hexo-theme-stellar/layout/_partial/scripts/index.ejs

177 lines
5.5 KiB
Plaintext

<%
function custom_inject() {
var el = '';
for (let item of (config.inject?.script || [])) {
el += item;
}
for (let item of (theme.inject?.script || [])) {
el += item;
}
for (let item of (page.inject?.script || [])) {
el += item;
}
return el;
}
%>
<script type="text/javascript">
const stellar = {
// 懒加载 css https://github.com/filamentgroup/loadCSS
loadCSS: (href, before, media, attributes) => {
var doc = window.document;
var ss = doc.createElement("link");
var ref;
if (before) {
ref = before;
} else {
var refs = (doc.body || doc.getElementsByTagName("head")[0]).childNodes;
ref = refs[refs.length - 1];
}
var sheets = doc.styleSheets;
if (attributes) {
for (var attributeName in attributes) {
if (attributes.hasOwnProperty(attributeName)) {
ss.setAttribute(attributeName, attributes[attributeName]);
}
}
}
ss.rel = "stylesheet";
ss.href = href;
ss.media = "only x";
function ready(cb) {
if (doc.body) {
return cb();
}
setTimeout(function () {
ready(cb);
});
}
ready(function () {
ref.parentNode.insertBefore(ss, before ? ref : ref.nextSibling);
});
var onloadcssdefined = function (cb) {
var resolvedHref = ss.href;
var i = sheets.length;
while (i--) {
if (sheets[i].href === resolvedHref) {
return cb();
}
}
setTimeout(function () {
onloadcssdefined(cb);
});
};
function loadCB() {
if (ss.addEventListener) {
ss.removeEventListener("load", loadCB);
}
ss.media = media || "all";
}
if (ss.addEventListener) {
ss.addEventListener("load", loadCB);
}
ss.onloadcssdefined = onloadcssdefined;
onloadcssdefined(loadCB);
return ss;
},
// 从 butterfly 和 volantis 获得灵感
loadScript: (src, opt) => new Promise((resolve, reject) => {
var script = document.createElement('script');
if (src.startsWith('/')){
src = stellar.config.root + src.substring(1);
}
script.src = src;
if (opt) {
for (let key of Object.keys(opt)) {
script[key] = opt[key]
}
} else {
// 默认异步,如果需要同步,第二个参数传入 {} 即可
script.async = true
}
script.onerror = reject
script.onload = script.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
script.onload = script.onreadystatechange = null
resolve()
}
document.head.appendChild(script)
}),
// https://github.com/jerryc127/hexo-theme-butterfly
jQuery: (fn) => {
if (typeof jQuery === 'undefined') {
stellar.loadScript(stellar.plugins.jQuery).then(fn)
} else {
fn()
}
}
};
stellar.version = '<%- theme.stellar.version %>';
stellar.github = 'https://github.com/xaoxuu/hexo-theme-stellar/tree/<%- theme.stellar.version %>';
stellar.config = {
date_suffix: {
just: '<%- __('meta.date_suffix.just') %>',
min: '<%- __('meta.date_suffix.min') %>',
hour: '<%- __('meta.date_suffix.hour') %>',
day: '<%- __('meta.date_suffix.day') %>',
month: '<%- __('meta.date_suffix.month') %>',
},
root : '<%- config.root %>',
};
// required plugins (only load if needs)
stellar.plugins = {
jQuery: '<%- url_for(theme.plugins.jquery || "https://gcore.jsdelivr.net/npm/jquery@3.6.2/dist/jquery.min.js") %>'
};
if ('<%- theme.search.service %>') {
stellar.search = {};
stellar.search.service = '<%- theme.search.service %>';
if (stellar.search.service == 'local_search') {
let service_obj = Object.assign({}, <%- JSON.stringify(theme.search.local_search) %>);
stellar.search[stellar.search.service] = service_obj;
}
}
// stellar js
stellar.plugins.stellar = Object.assign(<%- JSON.stringify(theme.plugins.stellar) %>);
stellar.plugins.marked = Object.assign(<%- JSON.stringify(theme.plugins.marked) %>);
// optional plugins
if ('<%- theme.plugins.lazyload.enable %>' == 'true') {
stellar.plugins.lazyload = Object.assign(<%- JSON.stringify(theme.plugins.lazyload) %>);
}
if ('<%- theme.plugins.swiper.enable %>' == 'true') {
stellar.plugins.swiper = Object.assign(<%- JSON.stringify(theme.plugins.swiper) %>);
}
if ('<%- theme.plugins.scrollreveal.enable %>' == 'true') {
stellar.plugins.scrollreveal = Object.assign(<%- JSON.stringify(theme.plugins.scrollreveal) %>);
}
if ('<%- theme.plugins.preload.enable %>' == 'true') {
stellar.plugins.preload = Object.assign(<%- JSON.stringify(theme.plugins.preload) %>);
}
if ('<%- theme.plugins.fancybox.enable %>' == 'true') {
stellar.plugins.fancybox = Object.assign(<%- JSON.stringify(theme.plugins.fancybox) %>);
}
if ('<%- theme.plugins.heti.enable %>' == 'true') {
stellar.plugins.heti = Object.assign(<%- JSON.stringify(theme.plugins.heti) %>);
}
if ('<%- theme.plugins.copycode.enable %>' == 'true') {
stellar.plugins.copycode = Object.assign(<%- JSON.stringify(theme.plugins.copycode) %>);
}
</script>
<!-- required -->
<script src="<%- `${theme.stellar.main_js}?v=${stellar_info('version')}` %>" async></script>
<!-- optional -->
<%- partial('../plugins/comments/script') %>
<%- partial('../plugins/parser/script') %>
<!-- inject -->
<%- custom_inject() %>