MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
function fillPageCards() { | function fillPageCards() { | ||
document.querySelectorAll('.page-card').forEach(card => { | document.querySelectorAll('.page-card').forEach(card => { | ||
| 第33行: | 第10行: | ||
const imgDiv = card.querySelector('.page-card-image'); | const imgDiv = card.querySelector('.page-card-image'); | ||
// API 根路径 | // MediaWiki API 根路径 | ||
const apiRoot = 'https://wflswiki.zelda-110.link/api.php'; | const apiRoot = 'https://wflswiki.zelda-110.link/api.php'; | ||
// 1️⃣ | // 1️⃣ 异步获取摘要 | ||
fetch(`${apiRoot}?action=query&prop=extracts&exchars=${length}&titles=${encodeURIComponent(title)}&format=json&explaintext=0`) | fetch(`${apiRoot}?action=query&prop=extracts&exchars=${length}&titles=${encodeURIComponent(title)}&format=json&explaintext=0`) | ||
.then(r => r.json()) | .then(r => r.json()) | ||
| 第51行: | 第28行: | ||
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') { | ||
| 第61行: | 第40行: | ||
.catch(err => console.error('摘要加载失败', title, err)); | .catch(err => console.error('摘要加载失败', title, err)); | ||
// 2️⃣ | // 2️⃣ 异步获取第一张图片 | ||
fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(title)}&prop=images&format=json`) | fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(title)}&prop=images&format=json`) | ||
.then(r => r.json()) | .then(r => r.json()) | ||
| 第72行: | 第51行: | ||
const firstImage = images[0].title; | const firstImage = images[0].title; | ||
// 获取真实 URL | |||
fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(firstImage)}&prop=imageinfo&iiprop=url&format=json`) | fetch(`${apiRoot}?action=query&titles=${encodeURIComponent(firstImage)}&prop=imageinfo&iiprop=url&format=json`) | ||
.then(r => r.json()) | .then(r => r.json()) | ||
| 第99行: | 第79行: | ||
} | } | ||
// DOM | // 监听 DOMContentLoaded,保证 DOM 已渲染 | ||
document.addEventListener('DOMContentLoaded', fillPageCards); | document.addEventListener('DOMContentLoaded', fillPageCards); | ||
// 支持动态生成内容,例如 #ask 异步插入条目 | |||
const observer = new MutationObserver(fillPageCards); | |||
observer.observe(document.body, { childList: true, subtree: true }); | |||