MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第24行: | 第24行: | ||
function fillExcerpts() { | function fillExcerpts() { | ||
document.querySelectorAll('.page-excerpt').forEach(div => { | document.querySelectorAll('.page-excerpt').forEach(div => { | ||
if (div.dataset.filled) return; | if (div.dataset.filled) return; // 避免重复填充 | ||
const title = div.dataset.title; | const title = div.dataset.title; | ||
const length = parseInt(div.dataset.length) || 200; | const length = parseInt(div.dataset.length) || 200; | ||
const url = '/api.php?action=query&prop=extracts&exchars=' + length + | const url = '/api.php?action=query&prop=extracts&exchars=' + length + | ||
'&titles=' + encodeURIComponent(title) + '&format=json'; | '&titles=' + encodeURIComponent(title) + '&format=json'; | ||
| 第35行: | 第35行: | ||
.then(data => { | .then(data => { | ||
const pages = data.query.pages; | const pages = data.query.pages; | ||
let | let html = ''; | ||
for (let pageId in pages) { | for (let pageId in pages) { | ||
if (pages.hasOwnProperty(pageId)) { | if (pages.hasOwnProperty(pageId)) { | ||
html = pages[pageId].extract || ''; | |||
break; | break; | ||
} | } | ||
} | } | ||
div.innerHTML = | // 保留 <a>,去掉其他 HTML 标签(标题、段落、换行等) | ||
// 先临时生成 DOM | |||
const temp = document.createElement('div'); | |||
temp.innerHTML = html; | |||
// 删除除了 <a> 的所有标签 | |||
const nodes = temp.querySelectorAll('*'); | |||
nodes.forEach(node => { | |||
if (node.tagName !== 'A') { | |||
const textNode = document.createTextNode(node.textContent); | |||
node.replaceWith(textNode); | |||
} | |||
}); | |||
// 再把换行、回车替换为空格 | |||
let textWithLinks = temp.innerHTML.replace(/[\r\n]+/g, ' '); | |||
div.innerHTML = textWithLinks; | |||
div.dataset.filled = '1'; | div.dataset.filled = '1'; | ||
}) | }) | ||
| 第56行: | 第73行: | ||
fillExcerpts(); | fillExcerpts(); | ||
// | // 可选:监听后续 DOM 变化 | ||
const observer = new MutationObserver(fillExcerpts); | const observer = new MutationObserver(fillExcerpts); | ||
observer.observe(document.body, { childList: true, subtree: true }); | observer.observe(document.body, { childList: true, subtree: true }); | ||
2025年12月14日 (日) 17:50的版本
//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>
`);
//页面摘要功能
function fillExcerpts() {
document.querySelectorAll('.page-excerpt').forEach(div => {
if (div.dataset.filled) return; // 避免重复填充
const title = div.dataset.title;
const length = parseInt(div.dataset.length) || 200;
const url = '/api.php?action=query&prop=extracts&exchars=' + length +
'&titles=' + encodeURIComponent(title) + '&format=json';
fetch(url)
.then(resp => resp.json())
.then(data => {
const pages = data.query.pages;
let html = '';
for (let pageId in pages) {
if (pages.hasOwnProperty(pageId)) {
html = pages[pageId].extract || '';
break;
}
}
// 保留 <a>,去掉其他 HTML 标签(标题、段落、换行等)
// 先临时生成 DOM
const temp = document.createElement('div');
temp.innerHTML = html;
// 删除除了 <a> 的所有标签
const nodes = temp.querySelectorAll('*');
nodes.forEach(node => {
if (node.tagName !== 'A') {
const textNode = document.createTextNode(node.textContent);
node.replaceWith(textNode);
}
});
// 再把换行、回车替换为空格
let textWithLinks = temp.innerHTML.replace(/[\r\n]+/g, ' ');
div.innerHTML = textWithLinks;
div.dataset.filled = '1';
})
.catch(err => {
console.error('Failed to fetch excerpt for', title, err);
div.textContent = '';
});
});
}
// 立即执行一次
fillExcerpts();
// 可选:监听后续 DOM 变化
const observer = new MutationObserver(fillExcerpts);
observer.observe(document.body, { childList: true, subtree: true });