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

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
无编辑摘要
无编辑摘要
 
(未显示同一用户的38个中间版本)
第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 () {
function fillExcerptsPopupLogic() {
    $(function () {
    document.querySelectorAll('.page-excerpt-popup').forEach(div => {
        var $el = $('#wiki-wordcount-number');
        let title = div.dataset.title;
         if (!$el.length) return;
         let length = parseInt(div.dataset.length) || 200;


         // 调用 MediaWiki REST API page/summary
         // 标记是否已触发过动画
         fetch('/api/rest_v1/page/summary/' + encodeURIComponent(title))
         var triggered = false;
            .then(resp => resp.json())
            .then(data => {
                let text = data.extract || '';


                // 去掉换行和多余空格
        // IntersectionObserver 配置
                 text = text.replace(/\s+/g, ' ').trim();
        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% 可见才触发
        });


                 // 截取长度
        // 观察目标元素
                 if (text.length > length) {
        observer.observe(document.getElementById('wiki-wordcount-number'));
                     text = text.slice(0, length) + '…';
 
        // 真正执行字数滚动动画
        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;
                 }
                 }


                 div.textContent = text;
                 // 缓动函数(前快后慢)
            })
                function easeOutQuad(t) {
             .catch(err => {
                    return t * (2 - t);
                 console.error('Failed to fetch excerpt:', err);
                }
                div.textContent = '';
 
                var duration = 2000;
                var startTime = null;
 
                function animate(timestamp) {
                    if (!startTime) startTime = timestamp;
                    var run = timestamp - startTime;
                    var progress = Math.min(run / duration, 1);
                    var eased = easeOutQuad(progress);
 
                    var currentVal = Math.floor(eased * total);
                    $el.text(currentVal.toLocaleString());
 
                    if (progress < 1) {
                        requestAnimationFrame(animate);
                    }
                }
                requestAnimationFrame(animate);
             }).fail(function () {
                 console.error('字数统计 API 请求失败');
             });
             });
        }
     });
     });
}
});
document.addEventListener('DOMContentLoaded', fillExcerptsPopupLogic);