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

MediaWiki:Common.js:修订间差异

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


         const url = '/api.php?action=query&prop=extracts&exchars=' + length +
         // 标记是否已触发过动画
                    '&titles=' + encodeURIComponent(title) + '&format=json';
        var triggered = false;


         fetch(url)
         // IntersectionObserver 配置
             .then(resp => resp.json())
        var observer = new IntersectionObserver(function(entries) {
             .then(data => {
             entries.forEach(function(entry) {
                const pages = data.query.pages;
                if (entry.isIntersecting && !triggered) {
                 let html = '';
                    triggered = true;
                 for (let pageId in pages) {
                    startCountAnimation();
                    if (pages.hasOwnProperty(pageId)) {
                    observer.unobserve(entry.target);
                        html = pages[pageId].extract || '';
                }
                        break;
            });
                     }
        }, {
            root: null,      // 视口为浏览器窗口
            rootMargin: "0px",
             threshold: 0.3  // 至少 30% 可见才触发
        });
 
        // 观察目标元素
        observer.observe(document.getElementById('wiki-wordcount-number'));
 
        // 真正执行字数滚动动画
        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;
                }
 
                // 缓动函数(前快后慢)
                function easeOutQuad(t) {
                     return t * (2 - t);
                 }
                 }


                 // 保留 <a>,去掉其他 HTML 标签(标题、段落、换行等)
                 var duration = 2000;
                // 先临时生成 DOM
                 var startTime = null;
                const temp = document.createElement('div');
                 temp.innerHTML = html;


                 // 删除除了 <a> 的所有标签
                 function animate(timestamp) {
                const nodes = temp.querySelectorAll('*');
                     if (!startTime) startTime = timestamp;
                nodes.forEach(node => {
                    var run = timestamp - startTime;
                     if (node.tagName !== 'A') {
                    var progress = Math.min(run / duration, 1);
                        const textNode = document.createTextNode(node.textContent);
                     var eased = easeOutQuad(progress);
                        node.replaceWith(textNode);
                     }
                });


                // 再把换行、回车替换为空格
                    var currentVal = Math.floor(eased * total);
                let textWithLinks = temp.innerHTML.replace(/[\r\n]+/g, ' ');
                    $el.text(currentVal.toLocaleString());


                 div.innerHTML = textWithLinks;
                    if (progress < 1) {
                 div.dataset.filled = '1';
                        requestAnimationFrame(animate);
             })
                    }
            .catch(err => {
                 }
                 console.error('Failed to fetch excerpt for', title, err);
                 requestAnimationFrame(animate);
                div.textContent = '';
             }).fail(function () {
                 console.error('字数统计 API 请求失败');
             });
             });
        }
     });
     });
}
});
 
// 立即执行一次
fillExcerpts();
 
// 可选:监听后续 DOM 变化
const observer = new MutationObserver(fillExcerpts);
observer.observe(document.body, { childList: true, subtree: true });