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

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
无编辑摘要
无编辑摘要
 
(未显示同一用户的21个中间版本)
第11行: 第11行:
</script>
</script>
`);
`);
document.head.insertAdjacentHTML('beforeend',`a
document.head.insertAdjacentHTML('beforeend',`
<script type="text/javascript">
<script type="text/javascript">
     (function(c,l,a,r,i,t,y){
     (function(c,l,a,r,i,t,y){
第20行: 第20行:
</script>
</script>
`);
`);
document.head.insertAdjacentHTML('beforeend',`
<meta name="baidu-site-verification" content="codeva-38vNyP0io6" />
`);
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
    $(function () {
        var $el = $('#wiki-wordcount-number');
        if (!$el.length) return;
        // 标记是否已触发过动画
        var triggered = false;
        // IntersectionObserver 配置
        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% 可见才触发
        });


//页面摘要功能
        // 观察目标元素
console.log('COMMON.JS LOADED');
        observer.observe(document.getElementById('wiki-wordcount-number'));


mw.hook('wikipage.content').add(function () {
        // 真正执行字数滚动动画
    console.log('wikipage.content triggered');
        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 cards = document.querySelectorAll('.page-card');
                // 缓动函数(前快后慢)
    for (var i = 0; i < cards.length; i++) {
                function easeOutQuad(t) {
        var card = cards[i];
                    return t * (2 - t);
        if (card.getAttribute('data-done')) continue;
                }


        var title = card.getAttribute('data-title');
                var duration = 2000;
        var div = card.querySelector('.page-excerpt');
                var startTime = null;
        if (!div || !title) continue;


        var url = '/api.php?action=query&prop=extracts&exchars=200&explaintext=1'
                function animate(timestamp) {
                + '&titles=' + encodeURIComponent(title)
                    if (!startTime) startTime = timestamp;
                + '&format=json';
                    var run = timestamp - startTime;
                    var progress = Math.min(run / duration, 1);
                    var eased = easeOutQuad(progress);


        fetch(url)
                    var currentVal = Math.floor(eased * total);
            .then(function (r) { return r.json(); })
                    $el.text(currentVal.toLocaleString());
            .then(function (data) {
 
                if (!data.query || !data.query.pages) return;
                     if (progress < 1) {
                for (var id in data.query.pages) {
                         requestAnimationFrame(animate);
                     if (data.query.pages.hasOwnProperty(id)) {
                         div.textContent = data.query.pages[id].extract || '(无摘要)';
                        break;
                     }
                     }
                 }
                 }
                requestAnimationFrame(animate);
            }).fail(function () {
                console.error('字数统计 API 请求失败');
             });
             });
 
         }
         card.setAttribute('data-done', '1');
     });
     }
});
});