PageSourceSearch

https://mayx.pages.dev/assets/js/pjax.js

js mayx.pages.dev collected 2026-10-03 05:25:37 UTC 5,874 bytes, 153 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.