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

177 lines
5.5 KiB
Plaintext
Raw Normal View History

2024-01-04 13:39:50 +08:00
<%
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;
}
%>
2021-02-21 17:27:48 +08:00
<script type="text/javascript">
2022-12-11 16:38:36 +08:00
const stellar = {
2021-03-13 23:31:52 +08:00
// 懒加载 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) => {
2021-06-26 23:37:30 +08:00
var script = document.createElement('script');
if (src.startsWith('/')){
src = stellar.config.root + src.substring(1);
}
2021-06-26 23:37:30 +08:00
script.src = src;
2021-03-13 23:31:52 +08:00
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()
}
}
};
2022-10-29 17:23:32 +08:00
stellar.version = '<%- theme.stellar.version %>';
2021-03-06 00:01:41 +08:00
stellar.github = 'https://github.com/xaoxuu/hexo-theme-stellar/tree/<%- theme.stellar.version %>';
2021-02-21 17:27:48 +08:00
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 %>',
2021-02-21 17:27:48 +08:00
};
// required plugins (only load if needs)
stellar.plugins = {
2022-12-20 00:09:34 +08:00
jQuery: '<%- url_for(theme.plugins.jquery || "https://gcore.jsdelivr.net/npm/jquery@3.6.2/dist/jquery.min.js") %>'
2021-02-21 17:27:48 +08:00
};
2022-11-27 17:16:38 +08:00
if ('<%- theme.search.service %>') {
stellar.search = {};
stellar.search.service = '<%- theme.search.service %>';
if (stellar.search.service == 'local_search') {
2022-11-28 23:07:50 +08:00
let service_obj = Object.assign({}, <%- JSON.stringify(theme.search.local_search) %>);
2022-11-27 17:16:38 +08:00
stellar.search[stellar.search.service] = service_obj;
}
}
2022-10-07 02:38:51 +08:00
// stellar js
stellar.plugins.stellar = Object.assign(<%- JSON.stringify(theme.plugins.stellar) %>);
stellar.plugins.marked = Object.assign(<%- JSON.stringify(theme.plugins.marked) %>);
2021-02-21 17:27:48 +08:00
// 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) %>);
}
2022-01-21 11:24:32 +08:00
if ('<%- theme.plugins.fancybox.enable %>' == 'true') {
stellar.plugins.fancybox = Object.assign(<%- JSON.stringify(theme.plugins.fancybox) %>);
}
2022-03-02 14:50:45 +08:00
if ('<%- theme.plugins.heti.enable %>' == 'true') {
stellar.plugins.heti = Object.assign(<%- JSON.stringify(theme.plugins.heti) %>);
}
2023-05-09 12:54:34 +08:00
if ('<%- theme.plugins.copycode.enable %>' == 'true') {
stellar.plugins.copycode = Object.assign(<%- JSON.stringify(theme.plugins.copycode) %>);
}
2021-02-21 17:27:48 +08:00
</script>
2021-02-19 23:33:19 +08:00
<!-- required -->
2024-01-22 13:22:40 +08:00
<script src="<%- `${theme.stellar.main_js}?v=${stellar_info('version')}` %>" async></script>
2021-02-20 13:09:41 +08:00
2021-02-19 23:33:19 +08:00
<!-- optional -->
<%- partial('../plugins/comments/script') %>
2024-01-04 10:54:18 +08:00
<%- partial('../plugins/parser/script') %>
2021-02-19 23:33:19 +08:00
<!-- inject -->
2024-01-04 13:39:50 +08:00
<%- custom_inject() %>