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

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
无编辑摘要
无编辑摘要
第25行: 第25行:
     document.querySelectorAll('.page-card').forEach(card => {
     document.querySelectorAll('.page-card').forEach(card => {
         if (card.dataset.filled) return;
         if (card.dataset.filled) return;
         const title = card.dataset.title;
 
         const title = card.dataset.title?.trim();
        if (!title) return;
 
         const length = parseInt(card.dataset.length) || 200;
         const length = parseInt(card.dataset.length) || 200;
         const excerptDiv = card.querySelector('.page-excerpt');
         const excerptDiv = card.querySelector('.page-excerpt');
         const imgDiv = card.querySelector('.page-card-image');
         const imgDiv = card.querySelector('.page-card-image');


         // --- 1. 获取摘要 ---
         // API 根路径
         const excerptUrl = '/api.php?action=query&prop=extracts&exchars=' + length +
         const apiRoot = 'https://wflswiki.zelda-110.link/api.php';
                          '&titles=' + encodeURIComponent(title) + '&format=json&explaintext=0';
 
        fetch(excerptUrl)
        // 1️⃣ 摘要
        fetch(`${apiRoot}?action=query&prop=extracts&exchars=${length}&titles=${encodeURIComponent(title)}&format=json&explaintext=0`)
             .then(r => r.json())
             .then(r => r.json())
             .then(data => {
             .then(data => {
第48行: 第51行:
                 const temp = document.createElement('div');
                 const temp = document.createElement('div');
                 temp.innerHTML = html;
                 temp.innerHTML = html;
                // 保留 <a>,删除其他标签
                 temp.querySelectorAll('*').forEach(node => {
                 temp.querySelectorAll('*').forEach(node => {
                     if (node.tagName !== 'A') {
                     if (node.tagName !== 'A') {
第56行: 第57行:
                 });
                 });


                // 去掉换行
                 excerptDiv.innerHTML = temp.innerHTML.replace(/[\r\n]+/g, ' ');
                 excerptDiv.innerHTML = temp.innerHTML.replace(/[\r\n]+/g, ' ');
             })
             })
             .catch(err => console.error('Failed to fetch excerpt for', title, err));
             .catch(err => console.error('摘要加载失败', title, err));


         // --- 2. 获取第一张图片 ---
         // 2️⃣ 第一张图片
         const imagesUrl = '/api.php?action=query&titles=' + encodeURIComponent(title) +
         fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(title)}&prop=images&format=json`)
                          '&prop=images&format=json';
        fetch(imagesUrl)
             .then(r => r.json())
             .then(r => r.json())
             .then(data => {
             .then(data => {
第72行: 第70行:
                         const images = pages[pageId].images || [];
                         const images = pages[pageId].images || [];
                         if (images.length > 0) {
                         if (images.length > 0) {
                             const firstImageTitle = images[0].title; // "File:xxx.jpg"
                             const firstImage = images[0].title;


                             // 二次请求获取 URL
                             fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(firstImage)}&prop=imageinfo&iiprop=url&format=json`)
                            const imageInfoUrl = '/api.php?action=query&titles=' +
                                                encodeURIComponent(firstImageTitle) +
                                                '&prop=imageinfo&iiprop=url&format=json';
                            fetch(imageInfoUrl)
                                 .then(r => r.json())
                                 .then(r => r.json())
                                 .then(imgData => {
                                 .then(imgData => {
第99行: 第93行:
                 }
                 }
             })
             })
             .catch(err => console.error('Failed to fetch image for', title, err));
             .catch(err => console.error('图片加载失败', title, err));


         card.dataset.filled = '1';
         card.dataset.filled = '1';
第105行: 第99行:
}
}


// 立即执行
// DOM 完全加载后再执行
fillPageCards();
document.addEventListener('DOMContentLoaded', fillPageCards);
 
// 监听 DOM 变化(可选)
const observer = new MutationObserver(fillPageCards);
observer.observe(document.body, { childList: true, subtree: true });

2025年12月14日 (日) 18:01的版本

//Microsoft 和 Google 分析
document.head.insertAdjacentHTML('beforeend',`
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-GVZPTSXG1H"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-GVZPTSXG1H');
</script>
`);
document.head.insertAdjacentHTML('beforeend',`a
<script type="text/javascript">
    (function(c,l,a,r,i,t,y){
        c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
        t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
        y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
    })(window, document, "clarity", "script", "uewoetn0au");
</script>
`);

//页面摘要功能
function fillPageCards() {
    document.querySelectorAll('.page-card').forEach(card => {
        if (card.dataset.filled) return;

        const title = card.dataset.title?.trim();
        if (!title) return;

        const length = parseInt(card.dataset.length) || 200;
        const excerptDiv = card.querySelector('.page-excerpt');
        const imgDiv = card.querySelector('.page-card-image');

        // API 根路径
        const apiRoot = 'https://wflswiki.zelda-110.link/api.php';

        // 1️⃣ 摘要
        fetch(`${apiRoot}?action=query&prop=extracts&exchars=${length}&titles=${encodeURIComponent(title)}&format=json&explaintext=0`)
            .then(r => r.json())
            .then(data => {
                const pages = data.query.pages;
                let html = '';
                for (let pageId in pages) {
                    if (pages.hasOwnProperty(pageId)) {
                        html = pages[pageId].extract || '';
                        break;
                    }
                }

                const temp = document.createElement('div');
                temp.innerHTML = html;
                temp.querySelectorAll('*').forEach(node => {
                    if (node.tagName !== 'A') {
                        node.replaceWith(document.createTextNode(node.textContent));
                    }
                });

                excerptDiv.innerHTML = temp.innerHTML.replace(/[\r\n]+/g, ' ');
            })
            .catch(err => console.error('摘要加载失败', title, err));

        // 2️⃣ 第一张图片
        fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(title)}&prop=images&format=json`)
            .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 firstImage = images[0].title;

                            fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(firstImage)}&prop=imageinfo&iiprop=url&format=json`)
                                .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) {
                                                const imgEl = document.createElement('img');
                                                imgEl.src = url;
                                                imgEl.alt = "概述图";
                                                imgDiv.appendChild(imgEl);
                                            }
                                        }
                                    }
                                });
                        }
                        break;
                    }
                }
            })
            .catch(err => console.error('图片加载失败', title, err));

        card.dataset.filled = '1';
    });
}

// DOM 完全加载后再执行
document.addEventListener('DOMContentLoaded', fillPageCards);