/**
* PJAX 初始化与页面切换重绑定脚本
* 依赖:jQuery, jquery.pjax.min.js
* 加载顺序:在 jquery.pjax.min.js 之后,body 末尾
*/
(function ($) {
// ========== 常量 ==========
var CONTAINER = '#pjax-container';
var PJAX_OPTS = {
container: CONTAINER,
fragment: CONTAINER,
timeout: 8000,
scrollTo: false
};
// ========== 工具函数 ==========
var _loadedScripts = {};
/** 动态加载外部 CSS(避免重复加载) */
function loadCSS(href) {
if ($('link[href="' + href + '"]').length) return;
$('').appendTo('head');
}
/** 动态加载外部 JS(去重) */
function loadScript(src, callback) {
if (_loadedScripts[src]) {
if (typeof callback === 'function') callback();
return;
}
_loadedScripts[src] = true;
var s = document.createElement('script');
s.src = src;
s.onload = callback || null;
document.body.appendChild(s);
}
// ========== 页面类型判断 ==========
/** 是否为文章页(非首页/分页) */
function isPostPage(pathname) {
return !/^(\/(index\.html)?|\/page\d+(\/index\.html)?)$/.test(pathname || window.location.pathname);
}
/** 是否为真正的文章页(用 DOM 特征判断,仅 post 布局才有这些元素) */
function isRealPostPage() {
return $(CONTAINER + ' #gitalk-container').length > 0;
}
// ========== 欢迎语生成 ==========
/**
* 根据当前时间和页面生成 Live2D 欢迎语
* 此函数暴露到 window._live2d.getWelcomeText,供 message.js 首次加载时复用
* @param {string} [pathname] - 页面路径,默认当前路径
* @param {string} [title] - 页面标题,默认从 document.title 提取
* @returns {string} 欢迎语 HTML
*/
function getWelcomeText(pathname, title) {
pathname = pathname || window.location.pathname;
title = title || document.title.split(' | ')[0];
if (pathname === '/' || pathname === '/index.html') {
var now = (new Date()).getHours();
if (now > 23 || now <= 5) return '你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?';
if (now > 5 && now <= 7) return '早上好!一日之计在于晨,美好的一天就要开始了!';
if (now > 7 && now <= 11) return '上午好!工作顺利嘛,不要久坐,多起来走动走动哦!';
if (now > 11 && now <= 14) return '中午了,工作了一个上午,现在是午餐时间!';
if (now > 14 && now <= 17) return '午后很容易犯困呢,今天的运动目标完成了吗?';
if (now > 17 && now <= 19) return '傍晚了!窗外夕阳的景色很美丽呢,最美不过夕阳红~~';
if (now > 19 && now <= 21) return '晚上好,今天过得怎么样?';
if (now > 21 && now <= 23) return '已经这么晚了呀,早点休息吧,晚安~~';
return '嗨~ 快来逗我玩吧!';
}
return '欢迎阅读「 ' + title + ' 」';
}
// ========== 各组件重初始化 ==========
/** 访问量统计 */
function reinitVisitors() {
if (typeof BlogAPI === 'undefined') return;
var apiBase = BlogAPI;
if ($('.visitors').length === 1) {
var $visitor = $('.visitors:first');
$.get(apiBase + '/count_click_add?id=' + $visitor.attr('id'), function (data) {
$visitor.text(Number(data));
});
} else if ($('.visitors-index').length > 0) {
$('.visitors-index').each(function () {
var $elem = $(this);
$.get(apiBase + '/count_click?id=' + $elem.attr('id'), function (data) {
$elem.text(Number(data));
});
});
}
}
/** AI 摘要(post.html 内联脚本,pjax 后触发) */
function reinitAISummary() {
if (typeof ai_gen === 'function' && $('#ai-output').length) {
try { ai_gen(); } catch (e) { /* ignore */ }
}
}
/** 代码块复制按钮 */
function reinitCopyButtons() {
$('.copy').remove();
$('div.highlight').each(function () {
var $block = $(this);
var $btn = $('