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

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
无编辑摘要
无编辑摘要
 
(未显示同一用户的29个中间版本)
第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');
         const title = card.dataset.title;
         if (!$el.length) return;
         if (!title || card.dataset.filled) return;


         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=200&titles=' +
         var observer = new IntersectionObserver(function(entries) {
                          encodeURIComponent(title) + '&format=json';
            entries.forEach(function(entry) {
        fetch(excerptUrl)
                if (entry.isIntersecting && !triggered) {
             .then(resp => resp.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 || '';
            root: null,      // 视口为浏览器窗口
                        break;
            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;
                 }
                 }


                 const temp = document.createElement('div');
                 // 缓动函数(前快后慢)
                 temp.innerHTML = html;
                function easeOutQuad(t) {
                    return t * (2 - t);
                 }


                 // 保留 <a>,删除其他标签
                 var duration = 2000;
                temp.querySelectorAll('*').forEach(node => {
                 var startTime = null;
                    if (node.tagName !== 'A') {
                        node.replaceWith(document.createTextNode(node.textContent));
                    }
                 });


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


        // 2️⃣ 获取第一张图片
                    var currentVal = Math.floor(eased * total);
        const imageUrl = '/api.php?action=query&titles=' + encodeURIComponent(title) +
                    $el.text(currentVal.toLocaleString());
                        '&prop=images&format=json';
        fetch(imageUrl)
            .then(resp => resp.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(resp => resp.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:', title, err));
             }).fail(function () {
 
                console.error('字数统计 API 请求失败');
         card.dataset.filled = '1';
            });
         }
     });
     });
}
});
 
// 立即执行
fillPageCards();
 
// 可选:监听后续 DOM 变化
const observer = new MutationObserver(fillPageCards);
observer.observe(document.body, { childList: true, subtree: true });