打开/关闭菜单
60
73
29
1.1K
武外梗百科
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
无编辑摘要
标签已被回退
无编辑摘要
 
(未显示同一用户的16个中间版本)
第19行: 第19行:
     })(window, document, "clarity", "script", "uewoetn0au");
     })(window, document, "clarity", "script", "uewoetn0au");
</script>
</script>
`);
document.head.insertAdjacentHTML('beforeend',`
<meta name="baidu-site-verification" content="codeva-38vNyP0io6" />
`);
`);


//页面摘要功能
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
/* ========= Page Card 摘要 + 概述图(保留内部链接) ========= */
    $(function () {
        var $el = $('#wiki-wordcount-number');
        if (!$el.length) return;


console.log('COMMON.JS LOADED');
        // 标记是否已触发过动画
        var triggered = false;


mw.hook('wikipage.content').add(function () {
        // IntersectionObserver 配置
    console.log('wikipage.content triggered');
        var observer = new IntersectionObserver(function(entries) {
            entries.forEach(function(entry) {
                if (entry.isIntersecting && !triggered) {
                    triggered = true;
                    startCountAnimation();
                    observer.unobserve(entry.target);
                }
            });
        }, {
            root: null,      // 视口为浏览器窗口
            rootMargin: "0px",
            threshold: 0.3  // 至少 30% 可见才触发
        });


    var cards = document.querySelectorAll('.page-card');
        // 观察目标元素
        observer.observe(document.getElementById('wiki-wordcount-number'));


    cards.forEach(function (card) {
        // 真正执行字数滚动动画
        if (card.dataset.done === '1') return;
        function startCountAnimation() {
            new mw.Api().get({
                action: 'wordcounter',
                prop: 'totals',
                format: 'json'
            }).done(function (data) {
                // --- 修改点:适配您的 API 返回结构 ---
                var total = 0;
                if (data && data.wordcounter && data.wordcounter.totals) {
                    total = data.wordcounter.totals.totalWords || 0;
                }


        var title = card.dataset.title;
                // 缓动函数(前快后慢)
        if (!title) return;
                function easeOutQuad(t) {
                    return t * (2 - t);
                }


        var excerptDiv = card.querySelector('.page-excerpt');
                var duration = 2000;
        var imageDiv  = card.querySelector('.page-card-image');
                var startTime = null;


        if (!excerptDiv) return;
                function animate(timestamp) {
                    if (!startTime) startTime = timestamp;
                    var run = timestamp - startTime;
                    var progress = Math.min(run / duration, 1);
                    var eased = easeOutQuad(progress);


        // API 地址(extracts + pageimages),保留 HTML
                    var currentVal = Math.floor(eased * total);
        var url = '/api.php?action=query' +
                    $el.text(currentVal.toLocaleString());
                  '&titles=' + encodeURIComponent(title) +
                  '&prop=extracts|pageimages' +
                  '&exchars=' + (card.dataset.length || 200) +
                  '&exlimit=1' +
                  '&exsectionformat=plain' +
                  '&exintro=1' +
                  '&format=json' +
                  '&explaintext=0'; // 保留 HTML


        fetch(url)
                     if (progress < 1) {
            .then(function (res) {
                         requestAnimationFrame(animate);
                if (!res.ok) throw new Error('HTTP ' + res.status + ': ' + res.statusText);
                return res.json();
            })
            .then(function (data) {
                if (!data.query || !data.query.pages) throw new Error('No pages in response');
 
                for (var id in data.query.pages) {
                     if (data.query.pages.hasOwnProperty(id)) {
                        var page = data.query.pages[id];
 
                        // 摘要 HTML
                        if (page.extract) {
                            // 去掉换行符,保留 <a>
                            var html = page.extract.replace(/[\r\n]+/g, ' ');
                            excerptDiv.innerHTML = html;
                        } else {
                            excerptDiv.textContent = '(无摘要)';
                        }
 
                        // 概述图
                         if (imageDiv && page.thumbnail && page.thumbnail.source) {
                            imageDiv.innerHTML = '<img src="' + page.thumbnail.source + '" alt="概述图">';
                        }
 
                        break; // 只取第一个页面
                     }
                     }
                 }
                 }
             })
                requestAnimationFrame(animate);
            .catch(function (err) {
             }).fail(function () {
                 console.error('Failed to fetch summary for', title, err);
                 console.error('字数统计 API 请求失败');
                excerptDiv.textContent = '(加载失败)';
             });
             });
 
         }
         card.dataset.done = '1';
     });
     });
});
});