MediaWiki:Common.js
MediaWiki界面页面
更多操作
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
//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',`
<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>
`);
//页面摘要功能
mw.hook('wikipage.content').add(function () {
console.log('wikipage.content triggered');
var cards = document.querySelectorAll('.page-card');
cards.forEach(function (card) {
if (card.dataset.done === '1') return;
var title = card.dataset.title;
if (!title) return;
var excerptDiv = card.querySelector('.page-excerpt');
var imageDiv = card.querySelector('.page-card-image');
if (!excerptDiv) return;
// API 地址(extracts + pageimages)
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)
.then(function (res) {
if (!res.ok) throw new Error('HTTP ' + res.status + ': ' + res.statusText);
return res.json();
})
.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) {
console.error('Failed to fetch summary for', title, err);
excerptDiv.textContent = '(加载失败)';
});
card.dataset.done = '1';
});
});