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'); | ||
// | // API 根路径 | ||
const | 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(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; | ||
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(' | .catch(err => console.error('摘要加载失败', title, err)); | ||
// | // 2️⃣ 第一张图片 | ||
fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(title)}&prop=images&format=json`) | |||
.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 | const firstImage = images[0].title; | ||
fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(firstImage)}&prop=imageinfo&iiprop=url&format=json`) | |||
.then(r => r.json()) | .then(r => r.json()) | ||
.then(imgData => { | .then(imgData => { | ||
| 第99行: | 第93行: | ||
} | } | ||
}) | }) | ||
.catch(err => console.error(' | .catch(err => console.error('图片加载失败', title, err)); | ||
card.dataset.filled = '1'; | card.dataset.filled = '1'; | ||
| 第105行: | 第99行: | ||
} | } | ||
// | // DOM 完全加载后再执行 | ||
document.addEventListener('DOMContentLoaded', fillPageCards); | |||