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

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
无编辑摘要
无编辑摘要
第22行: 第22行:


//页面摘要功能
//页面摘要功能
function fillExcerpts() {
function fillPageCards() {
     document.querySelectorAll('.page-excerpt').forEach(div => {
     document.querySelectorAll('.page-card').forEach(card => {
        if (div.dataset.filled) return; // 避免重复填充
         const title = card.dataset.title;
         const title = div.dataset.title;
         if (!title || card.dataset.filled) return;
         const length = parseInt(div.dataset.length) || 200;


         const url = '/api.php?action=query&prop=extracts&exchars=' + length +
         const excerptDiv = card.querySelector('.page-excerpt');
                    '&titles=' + encodeURIComponent(title) + '&format=json';
        const imgTag = card.querySelector('.page-card-image img');


         fetch(url)
        // 1️⃣ 获取摘要
        const excerptUrl = '/api.php?action=query&prop=extracts&exchars=200&titles=' +
                          encodeURIComponent(title) + '&format=json';
         fetch(excerptUrl)
             .then(resp => resp.json())
             .then(resp => resp.json())
             .then(data => {
             .then(data => {
第43行: 第45行:
                 }
                 }


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


                 // 删除除了 <a> 的所有标签
                 // 保留 <a>,删除其他标签
                 const nodes = temp.querySelectorAll('*');
                 temp.querySelectorAll('*').forEach(node => {
                nodes.forEach(node => {
                     if (node.tagName !== 'A') {
                     if (node.tagName !== 'A') {
                         const textNode = document.createTextNode(node.textContent);
                         node.replaceWith(document.createTextNode(node.textContent));
                        node.replaceWith(textNode);
                     }
                     }
                 });
                 });


                // 再把换行、回车替换为空格
                 let textWithLinks = temp.innerHTML.replace(/[\r\n]+/g, ' ');
                 let textWithLinks = temp.innerHTML.replace(/[\r\n]+/g, ' ');
                excerptDiv.innerHTML = textWithLinks;
            })
            .catch(err => console.error('Failed to fetch excerpt:', title, err));


                 div.innerHTML = textWithLinks;
        // 2️⃣ 获取第一张图片
                div.dataset.filled = '1';
        const imageUrl = '/api.php?action=query&titles=' + encodeURIComponent(title) +
                        '&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
                            const imageInfoUrl = '/api.php?action=query&titles=' +
                                                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;
                    }
                }
             })
             })
             .catch(err => {
             .catch(err => console.error('Failed to fetch image:', title, err));
                console.error('Failed to fetch excerpt for', title, err);
 
                div.textContent = '';
        card.dataset.filled = '1';
            });
     });
     });
}
}


// 立即执行一次
// 立即执行
fillExcerpts();
fillPageCards();


// 可选:监听后续 DOM 变化
// 可选:监听后续 DOM 变化
const observer = new MutationObserver(fillExcerpts);
const observer = new MutationObserver(fillPageCards);
observer.observe(document.body, { childList: true, subtree: true });
observer.observe(document.body, { childList: true, subtree: true });