|
|
| 第22行: |
第22行: |
|
| |
|
| //页面摘要功能 | | //页面摘要功能 |
| function fillPageCards() {
| | console.log('COMMON.JS LOADED'); |
| document.querySelectorAll('.page-card').forEach(card => {
| |
| if (card.dataset.filled) return;
| |
| const title = card.dataset.title;
| |
| const length = parseInt(card.dataset.length) || 200;
| |
|
| |
|
| const excerptDiv = card.querySelector('.page-excerpt');
| | mw.hook('wikipage.content').add(function () { |
| const imgDiv = card.querySelector('.page-card-image');
| | console.log('wikipage.content triggered'); |
| | |
| // --- 1. 获取摘要 ---
| |
| const excerptUrl = '/api.php?action=query&prop=extracts&exchars=' + length +
| |
| '&titles=' + encodeURIComponent(title) + '&format=json&explaintext=0';
| |
| fetch(excerptUrl)
| |
| .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');
| | var cards = document.querySelectorAll('.page-card'); |
| temp.innerHTML = html;
| | for (var i = 0; i < cards.length; i++) { |
| | var card = cards[i]; |
| | if (card.getAttribute('data-done')) continue; |
|
| |
|
| // 保留 <a>,删除其他标签
| | var title = card.getAttribute('data-title'); |
| temp.querySelectorAll('*').forEach(node => {
| | var div = card.querySelector('.page-excerpt'); |
| if (node.tagName !== 'A') {
| | if (!div || !title) continue; |
| node.replaceWith(document.createTextNode(node.textContent));
| |
| }
| |
| });
| |
|
| |
|
| // 去掉换行
| | var url = '/api.php?action=query&prop=extracts&exchars=200&explaintext=1' |
| excerptDiv.innerHTML = temp.innerHTML.replace(/[\r\n]+/g, ' '); | | + '&titles=' + encodeURIComponent(title) |
| })
| | + '&format=json'; |
| .catch(err => console.error('Failed to fetch excerpt for', title, err));
| |
|
| |
|
| // --- 2. 获取第一张图片 ---
| | fetch(url) |
| const imagesUrl = '/api.php?action=query&titles=' + encodeURIComponent(title) +
| | .then(function (r) { return r.json(); }) |
| '&prop=images&format=json';
| | .then(function (data) { |
| fetch(imagesUrl) | | if (!data.query || !data.query.pages) return; |
| .then(r => r.json()) | | for (var id in data.query.pages) { |
| .then(data => { | | if (data.query.pages.hasOwnProperty(id)) { |
| const pages = data.query.pages; | | div.textContent = data.query.pages[id].extract || '(无摘要)'; |
| 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(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; | | break; |
| } | | } |
| } | | } |
| }) | | }); |
| .catch(err => console.error('Failed to fetch image for', title, err));
| |
|
| |
|
| card.dataset.filled = '1'; | | card.setAttribute('data-done', '1'); |
| }); | | } |
| } | | }); |
| | |
| // 立即执行
| |
| fillPageCards();
| |
| | |
| // 监听 DOM 变化(可选)
| |
| const observer = new MutationObserver(fillPageCards);
| |
| observer.observe(document.body, { childList: true, subtree: true });
| |
| | |
| console.log('COMMON.JS LOADED');
| |