Mayx's Home Page
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.2 KiB

  1. /**
  2. * PJAX 初始化与页面切换重绑定脚本
  3. * 依赖jQuery, jquery.pjax.min.js
  4. * 加载顺序 jquery.pjax.min.js 之后body 末尾
  5. */
  6. (function ($) {
  7. // ========== 常量 ==========
  8. var CONTAINER = '#pjax-container';
  9. var PJAX_OPTS = {
  10. container: CONTAINER,
  11. fragment: CONTAINER,
  12. timeout: 8000,
  13. scrollTo: false
  14. };
  15. // ========== 各组件重初始化 ==========
  16. /** AI 摘要(post.html 内联脚本,pjax 后由 executeScripts 触发) */
  17. function reinitAISummary() {
  18. if (typeof ai_gen === 'function' && $('#ai-output').length) {
  19. try { ai_gen(); } catch (e) { /* ignore */ }
  20. }
  21. }
  22. /** Google Analytics 页面浏览事件 */
  23. function trackPageView() {
  24. if (typeof gtag === 'function') {
  25. gtag('config', window._gaId || '', { page_path: window.location.pathname });
  26. }
  27. }
  28. /** Live2D 重初始化 */
  29. var _live2dSelectors = ['.post-link', '#search-input'];
  30. var _live2dDelegateBound = false;
  31. function reinitLive2d() {
  32. if (!window._live2d) return;
  33. var pathname = window.location.pathname;
  34. // 更新"想问这篇文章"相关状态(仅真正的文章页显示)
  35. $('#post_id').val(pathname);
  36. if ($(CONTAINER + ' #gitalk-container').length > 0) {
  37. $('.live_talk_input_name_body').show();
  38. } else {
  39. $('.live_talk_input_name_body').hide();
  40. $('#load_this').prop('checked', false);
  41. }
  42. // 音乐按钮:根据当前页面是否有 BGM 输入来显示/隐藏
  43. if (typeof window._live2d.initBGM === 'function') {
  44. window._live2d.initBGM();
  45. }
  46. // 事件委托绑定(只执行一次)
  47. if (!_live2dDelegateBound && typeof String.prototype.renderTip === 'function') {
  48. var selector = CONTAINER + ' ' + _live2dSelectors.join(', ' + CONTAINER + ' ');
  49. $(document).on('mouseover._live2d_pjax', selector, function (e) {
  50. var $el = $(e.currentTarget || e.target);
  51. if ($el.is('.post-link')) {
  52. window._live2d.showMessage('要看看 ' + $el.text() + ' 么?', 3000);
  53. } else if ($el.is('#search-input')) {
  54. window._live2d.showMessage('在找什么东西呢,需要帮忙吗?', 3000);
  55. }
  56. });
  57. $(document).on('mouseout._live2d_pjax', selector, function () {
  58. if (window._live2d.showHitokoto) window._live2d.showHitokoto();
  59. });
  60. _live2dDelegateBound = true;
  61. }
  62. // 欢迎语
  63. if (typeof window._live2d.showMessage === 'function') {
  64. window._live2d.showMessage(getWelcomeText(pathname), 6000);
  65. }
  66. }
  67. // ========== PJAX 导航 ==========
  68. /** PJAX 完成后的统一处理 */
  69. function doPjaxComplete() {
  70. $('body').removeClass('pjax-loading');
  71. // 清理可能残留的浮层(如推荐文章 tooltip,hover 后点击跳转时 mouseleave 来不及触发)
  72. $('.content-tooltip').remove();
  73. onPjaxComplete();
  74. }
  75. /** 暴露给模板内 onclick/onchange 调用的导航函数 */
  76. window.go = function (url) {
  77. $.pjax({ url: url, ...PJAX_OPTS });
  78. };
  79. // ========== 初始化 ==========
  80. /** pjax 完成后滚动到目标位置:有锚点则定位锚点,否则回到顶部 */
  81. function scrollToAnchor() {
  82. var hash = window.location.hash;
  83. if (hash) {
  84. // 中文等非 ASCII 字符在 URL 中会被编码,需先解码再匹配元素 id
  85. var id = hash.slice(1);
  86. try { id = decodeURIComponent(id); } catch (e) { /* 保持原值 */ }
  87. var target = document.getElementById(id) ||
  88. document.querySelector('a[name="' + id + '"]');
  89. if (target) {
  90. target.scrollIntoView({ behavior: 'smooth', block: 'start' });
  91. return;
  92. }
  93. }
  94. window.scrollTo(0, 0);
  95. }
  96. /** 每次 pjax 完成后执行所有重初始化 */
  97. function onPjaxComplete() {
  98. initVisitors();
  99. initCopyButtons();
  100. highlightKeyword();
  101. reinitAISummary();
  102. reinitLive2d();
  103. trackPageView();
  104. scrollToAnchor();
  105. }
  106. $(document).ready(function () {
  107. // 排除列表:外链、锚点、静态资源、Live2D 目录
  108. var exclude = ':not([target="_blank"]):not([href^="http"]):not([href^="//"])' +
  109. ':not([href^="mailto"]):not([href^="#"])' +
  110. ':not([href$=".xml"]):not([href$=".json"]):not([href$=".tgz"]):not([href$=".zip"])' +
  111. ':not([href^="/Live2dHistoire"])';
  112. $(document).pjax('a' + exclude, PJAX_OPTS.container, PJAX_OPTS);
  113. $(document).on('submit', 'form#search-input-all', function (e) {
  114. $.pjax.submit(e, PJAX_OPTS.container, PJAX_OPTS);
  115. });
  116. $(document).on('pjax:send', function () {
  117. $('body').addClass('pjax-loading');
  118. });
  119. $(document).on('pjax:complete', doPjaxComplete);
  120. $(document).on('pjax:error', function (xhr, textStatus, error) {
  121. console.warn('[pjax] error, fallback:', error);
  122. });
  123. $(document).on('pjax:end', function (event, xhr, options) {
  124. var $container = $(options.container || PJAX_OPTS.container);
  125. $container.find('script[type="module"]').each(function () {
  126. var oldScript = this;
  127. var newScript = document.createElement('script');
  128. newScript.type = 'module';
  129. // 如果是外链脚本 (<script src="..."></script>)
  130. if (oldScript.src) {
  131. newScript.src = oldScript.src;
  132. } else {
  133. // 如果是行内脚本 (<script>...code...</script>)
  134. newScript.textContent = oldScript.textContent;
  135. }
  136. // 插入到 body 中触发浏览器执行
  137. document.body.appendChild(newScript);
  138. // 运行完后建议移除,防止 DOM 变得混乱(不影响模块执行)
  139. newScript.remove();
  140. });
  141. });
  142. });
  143. })(jQuery);