MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第22行: | 第22行: | ||
//页面摘要功能 | //页面摘要功能 | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
console.log('wikipage.content triggered'); | console.log('wikipage.content triggered'); | ||
| 第40行: | 第38行: | ||
if (!excerptDiv) return; | if (!excerptDiv) return; | ||
// | // API 地址(extracts + pageimages) | ||
var url = '/ | var url = '/api.php?action=query' + | ||
'&titles=' + encodeURIComponent(title) + | |||
'&prop=extracts|pageimages' + | |||
'&exchars=' + (card.dataset.length || 200) + | |||
'&explaintext=1' + | |||
'&piprop=thumbnail&pithumbsize=300&format=json'; | |||
fetch(url) | fetch(url) | ||
.then(function (res) { | .then(function (res) { | ||
if (!res.ok) throw new Error('HTTP ' + res.status); | if (!res.ok) throw new Error('HTTP ' + res.status + ': ' + res.statusText); | ||
return res.json(); | return res.json(); | ||
}) | }) | ||
.then(function (data) { | .then(function (data) { | ||
if (!data.query || !data.query.pages) throw new Error('No pages in response'); | |||
for (var id in data.query.pages) { | |||
if (data.query.pages.hasOwnProperty(id)) { | |||
var page = data.query.pages[id]; | |||
// 摘要 | |||
excerptDiv.textContent = page.extract || '(无摘要)'; | |||
// 概述图 | |||
if (imageDiv && page.thumbnail && page.thumbnail.source) { | |||
imageDiv.innerHTML = '<img src="' + page.thumbnail.source + '" alt="概述图">'; | |||
} | |||
break; // 只取第一个页面 | |||
} | |||
} | } | ||
}) | }) | ||
.catch(function (err) { | .catch(function (err) { | ||
console. | console.error('Failed to fetch summary for', title, err); | ||
excerptDiv.textContent = '(加载失败)'; | excerptDiv.textContent = '(加载失败)'; | ||
}); | }); | ||