User:PexEric/Assess a DYK.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
$(function() {
console.log('脚本初始化 - 当前页面:', mw.config.get('wgPageName'));
const currentPage = mw.config.get('wgPageName');
const isHomePage = currentPage === 'Wikipedia:首页';
const isNewArticleCandidate = currentPage === 'Wikipedia:新条目推荐/候选';
const isNewArticlesPage = /^Wikipedia:新条目推荐(\/|$)/.test(currentPage);
if (!isHomePage && !isNewArticleCandidate && !isNewArticlesPage) {
console.log('页面不符合条件,退出执行');
return;
}
let articleLinks = [];
try {
if (isHomePage) {
const dykSection = document.querySelector('#column-dyk');
if (!dykSection) throw new Error('未找到DYK容器');
articleLinks = Array.from(dykSection.querySelectorAll('b > a[title]'));
console.log('首页检测到条目:', articleLinks.map(a => a.title));
} else if (isNewArticleCandidate) {
articleLinks = Array.from(document.querySelectorAll('span.dykarticle > a[title]'));
console.log('候选页检测到条目:', articleLinks.map(a => a.title));
} else {
articleLinks = Array.from(document.querySelectorAll('b > a[title], b > a[href]'));
console.log('新条目页检测到条目:', articleLinks.map(a => a.title || a.href));
}
if (!articleLinks.length) throw new Error('未找到条目链接');
} catch (e) {
console.error('元素检测失败:', e);
return;
}
console.groupCollapsed(`开始处理 ${articleLinks.length} 个条目`);
articleLinks.forEach((link, index) => {
const process = async () => {
try {
console.group(`处理条目 ${index + 1}: ${link.title || link.textContent}`);
let articleName;
if (isHomePage || isNewArticleCandidate) {
articleName = link.title;
} else {
articleName = link.title || decodeURIComponent(link.href.match(/\/wiki\/([^?]+)/)[1]);
}
console.log('解析条目名称:', articleName);
const tempMarker = document.createElement('span');
tempMarker.textContent = ' [检查中...]';
tempMarker.style.color = '#666';
link.parentNode.insertBefore(tempMarker, link.nextSibling);
// 获取讨论页内容 [[1]]
const api = new mw.Api();
const params = {
action: 'query',
prop: 'revisions',
titles: `Talk:${articleName}`,
rvprop: 'content',
formatversion: 2
};
console.log('发送API请求:', params);
const result = await api.get(params);
console.log('API响应数据:', result);
const pages = result.query?.pages || [];
const pageData = pages[0] || {};
if (pageData.missing) {
console.log('讨论页不存在');
const annotation = createAnnotation({
rating: '未评级',
count: 0
});
tempMarker.replaceWith(annotation);
return;
}
// 解析讨论页内容 [[2]]
const content = pageData.revisions[0]?.content || '';
const banners = content.match(/{{\s*专题\s*\|.*?等级\s*=\s*(\w+)/gi) || [];
const ratings = banners.map(b =>
b.match(/等级\s*=\s*(\w+)/i)?.[1]?.trim() || ''
).filter(r => r);
const projectCount = banners.length;
let finalRating = '未评级';
if (ratings.length) {
// 定义评级优先级排序 [[3]]
const ratingOrder = ['甲级', '乙级', '丙级', '丁级'];
finalRating = ratings
.map(r => r.replace('级', ''))
.sort((a, b) =>
ratingOrder.indexOf(a) - ratingOrder.indexOf(b)
)[0] + '级';
}
const annotation = createAnnotation({
rating: finalRating,
count: projectCount
});
tempMarker.replaceWith(annotation);
console.log('标注添加成功:', annotation.outerHTML);
} catch (e) {
console.error('处理失败:', e);
tempMarker.textContent = ' [检查失败]';
tempMarker.style.color = '#cc0000';
} finally {
console.groupEnd();
}
};
process();
});
});
function createAnnotation({ rating, count }) {
const annotation = document.createElement('small');
annotation.style.marginLeft = '0.3em';
annotation.textContent = rating === '未评级'
? '(未评级)'
: `(${rating}:${count}个专题)`;
return annotation;
}