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

MediaWiki:Common.js:修订间差异

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


         const excerptDiv = card.querySelector('.page-excerpt');
         // 标记是否已触发过动画
         const imgTag = card.querySelector('.page-card-image img');
         var triggered = false;


         // --- 1. 获取摘要 ---
         // IntersectionObserver 配置
         const excerptUrl = '/api.php?action=query&prop=extracts&exchars=' + length +
         var observer = new IntersectionObserver(function(entries) {
                          '&titles=' + encodeURIComponent(title) + '&format=json&explaintext=0';
             entries.forEach(function(entry) {
        fetch(excerptUrl)
                 if (entry.isIntersecting && !triggered) {
             .then(r => r.json())
                    triggered = true;
            .then(data => {
                    startCountAnimation();
                 const pages = data.query.pages;
                     observer.unobserve(entry.target);
                let html = '';
                for (let pageId in pages) {
                     if (pages.hasOwnProperty(pageId)) {
                        html = pages[pageId].extract || '';
                        break;
                    }
                 }
                 }
            });
        }, {
            root: null,      // 视口为浏览器窗口
            rootMargin: "0px",
            threshold: 0.3  // 至少 30% 可见才触发
        });


                const temp = document.createElement('div');
        // 观察目标元素
                temp.innerHTML = html;
        observer.observe(document.getElementById('wiki-wordcount-number'));


                // 保留 <a>,去掉其他标签
        // 真正执行字数滚动动画
                temp.querySelectorAll('*').forEach(node => {
        function startCountAnimation() {
                    if (node.tagName !== 'A') {
            new mw.Api().get({
                        node.replaceWith(document.createTextNode(node.textContent));
                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);
                }
 
                 var duration = 2000;
                var startTime = null;


                 // 去掉换行
                 function animate(timestamp) {
                excerptDiv.innerHTML = temp.innerHTML.replace(/[\r\n]+/g, ' ');
                    if (!startTime) startTime = timestamp;
            })
                    var run = timestamp - startTime;
            .catch(err => console.error('Failed to fetch excerpt for', title, err));
                    var progress = Math.min(run / duration, 1);
                    var eased = easeOutQuad(progress);


        // --- 2. 获取第一张图片 ---
                    var currentVal = Math.floor(eased * total);
        const imagesUrl = '/api.php?action=query&titles=' + encodeURIComponent(title) +
                    $el.text(currentVal.toLocaleString());
                          '&prop=images&format=json';
        fetch(imagesUrl)
            .then(r => r.json())
            .then(data => {
                const pages = data.query.pages;
                for (let pageId in pages) {
                    if (pages.hasOwnProperty(pageId)) {
                        const images = pages[pageId].images || [];
                        if (images.length > 0) {
                            const firstImageTitle = images[0].title; // "File:xxx.jpg"


                            // 二次请求获取 URL
                    if (progress < 1) {
                            const imageInfoUrl = '/api.php?action=query&titles=' +
                        requestAnimationFrame(animate);
                                                encodeURIComponent(firstImageTitle) +
                                                '&prop=imageinfo&iiprop=url&format=json';
                            fetch(imageInfoUrl)
                                .then(r => r.json())
                                .then(imgData => {
                                    const imgPages = imgData.query.pages;
                                    for (let imgId in imgPages) {
                                        if (imgPages.hasOwnProperty(imgId)) {
                                            const url = imgPages[imgId].imageinfo?.[0]?.url;
                                            if (url) imgTag.src = url;
                                        }
                                    }
                                });
                        }
                        break;
                     }
                     }
                 }
                 }
             })
                requestAnimationFrame(animate);
            .catch(err => console.error('Failed to fetch image for', title, err));
             }).fail(function () {
 
                console.error('字数统计 API 请求失败');
         card.dataset.filled = '1';
            });
         }
     });
     });
}
});
 
// 立即执行
fillPageCards();
const observer = new MutationObserver(fillPageCards);
observer.observe(document.body, { childList: true, subtree: true });