“MediaWiki:Common.js”的版本间差异
跳转到导航
跳转到搜索
| 第1行: | 第1行: | ||
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */ | /* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */ | ||
| − | // | + | // 显示最近更新的页面列表(去重版) |
setTimeout(function() { | setTimeout(function() { | ||
| − | console.log(' | + | console.log('开始加载最近更新列表(去重版)'); |
// 1. 定位侧边栏中的“最近更新”容器 | // 1. 定位侧边栏中的“最近更新”容器 | ||
| 第15行: | 第15行: | ||
var items = sidebar.getElementsByTagName('li'); | var items = sidebar.getElementsByTagName('li'); | ||
for (var i = 0; i < items.length; i++) { | for (var i = 0; i < items.length; i++) { | ||
| − | + | if (items[i].textContent.includes('最近更新:') || items[i].textContent === '123') { | |
| − | if (items[i].textContent.includes(' | ||
targetItem = items[i]; | targetItem = items[i]; | ||
break; | break; | ||
| 第28行: | 第27行: | ||
// 2. 清空容器,显示“加载中...” | // 2. 清空容器,显示“加载中...” | ||
| − | targetItem.textContent = ''; | + | targetItem.textContent = ''; |
| − | targetItem.innerHTML = '最近更新:<br>加载中...'; | + | targetItem.innerHTML = '最近更新:<br>加载中...'; |
| − | // 3. | + | // 3. 调用API(适当增大rclimit,避免去重后数量不足) |
var apiUrl = mw.util.wikiScript('api') + '?'; | var apiUrl = mw.util.wikiScript('api') + '?'; | ||
apiUrl += 'action=query'; | apiUrl += 'action=query'; | ||
apiUrl += '&list=recentchanges'; | apiUrl += '&list=recentchanges'; | ||
| − | apiUrl += '&rcprop=title'; | + | apiUrl += '&rcprop=title'; |
| − | apiUrl += '&rclimit= | + | apiUrl += '&rclimit=10'; // 先获取10条(去重后可能少于10) |
| − | apiUrl += '&rcdaylimit=30'; | + | apiUrl += '&rcdaylimit=30'; |
apiUrl += '&format=json'; | apiUrl += '&format=json'; | ||
| − | // 4. | + | // 4. 发送请求并处理数据(添加去重逻辑) |
fetch(apiUrl) | fetch(apiUrl) | ||
.then(function(response) { | .then(function(response) { | ||
| − | if (!response.ok) | + | if (!response.ok) throw new Error('API请求失败:' + response.status); |
| − | |||
| − | |||
return response.json(); | return response.json(); | ||
}) | }) | ||
.then(function(data) { | .then(function(data) { | ||
| − | |||
targetItem.innerHTML = '最近更新:<br>'; | targetItem.innerHTML = '最近更新:<br>'; | ||
| − | |||
if (!data.query || !data.query.recentchanges || data.query.recentchanges.length === 0) { | if (!data.query || !data.query.recentchanges || data.query.recentchanges.length === 0) { | ||
targetItem.innerHTML += '暂无更新'; | targetItem.innerHTML += '暂无更新'; | ||
| 第58行: | 第53行: | ||
} | } | ||
| − | // | + | // 去重逻辑:用Set记录已出现的标题,只保留首次出现的(即最新的) |
| − | var | + | var seenTitles = new Set(); // 存储已出现的标题 |
| − | + | var uniqueUpdates = []; // 去重后的结果 | |
| − | |||
| − | |||
for (var i = 0; i < data.query.recentchanges.length; i++) { | for (var i = 0; i < data.query.recentchanges.length; i++) { | ||
var page = data.query.recentchanges[i]; | var page = data.query.recentchanges[i]; | ||
| + | var title = page.title; | ||
| + | |||
| + | if (!seenTitles.has(title)) { // 如果标题未出现过 | ||
| + | seenTitles.add(title); // 记录到Set | ||
| + | uniqueUpdates.push(page); // 加入去重结果 | ||
| + | |||
| + | // 最多保留5个不重复的页面(可调整) | ||
| + | if (uniqueUpdates.length >= 5) break; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // 生成去重后的列表 | ||
| + | var list = document.createElement('ul'); | ||
| + | list.style.listStyle = 'none'; | ||
| + | list.style.paddingLeft = '10px'; | ||
| + | list.style.margin = '5px 0'; | ||
| + | |||
| + | if (uniqueUpdates.length === 0) { | ||
| + | targetItem.innerHTML += '暂无更新'; | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | uniqueUpdates.forEach(function(page) { | ||
var listItem = document.createElement('li'); | var listItem = document.createElement('li'); | ||
var link = document.createElement('a'); | var link = document.createElement('a'); | ||
| − | + | link.href = mw.util.getUrl(page.title); | |
| − | |||
| − | link.href = mw.util.getUrl(page.title); | ||
link.textContent = page.title; | link.textContent = page.title; | ||
| − | link.style.color = '#0645ad'; | + | link.style.color = '#0645ad'; |
| − | |||
listItem.appendChild(link); | listItem.appendChild(link); | ||
list.appendChild(listItem); | list.appendChild(listItem); | ||
| − | } | + | }); |
targetItem.appendChild(list); | targetItem.appendChild(list); | ||
| − | console.log(' | + | console.log('去重后显示 ' + uniqueUpdates.length + ' 个页面'); |
}) | }) | ||
.catch(function(error) { | .catch(function(error) { | ||
| 第86行: | 第99行: | ||
}); | }); | ||
| − | }, 500); | + | }, 500); |
2025年10月8日 (三) 18:16:55的版本
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
// 显示最近更新的页面列表(去重版)
setTimeout(function() {
console.log('开始加载最近更新列表(去重版)');
// 1. 定位侧边栏中的“最近更新”容器
var sidebar = document.getElementById('mw-panel');
if (!sidebar) {
console.log('未找到侧边栏');
return;
}
var targetItem = null;
var items = sidebar.getElementsByTagName('li');
for (var i = 0; i < items.length; i++) {
if (items[i].textContent.includes('最近更新:') || items[i].textContent === '123') {
targetItem = items[i];
break;
}
}
if (!targetItem) {
console.log('未找到目标容器');
return;
}
// 2. 清空容器,显示“加载中...”
targetItem.textContent = '';
targetItem.innerHTML = '最近更新:<br>加载中...';
// 3. 调用API(适当增大rclimit,避免去重后数量不足)
var apiUrl = mw.util.wikiScript('api') + '?';
apiUrl += 'action=query';
apiUrl += '&list=recentchanges';
apiUrl += '&rcprop=title';
apiUrl += '&rclimit=10'; // 先获取10条(去重后可能少于10)
apiUrl += '&rcdaylimit=30';
apiUrl += '&format=json';
// 4. 发送请求并处理数据(添加去重逻辑)
fetch(apiUrl)
.then(function(response) {
if (!response.ok) throw new Error('API请求失败:' + response.status);
return response.json();
})
.then(function(data) {
targetItem.innerHTML = '最近更新:<br>';
if (!data.query || !data.query.recentchanges || data.query.recentchanges.length === 0) {
targetItem.innerHTML += '暂无更新';
return;
}
// 去重逻辑:用Set记录已出现的标题,只保留首次出现的(即最新的)
var seenTitles = new Set(); // 存储已出现的标题
var uniqueUpdates = []; // 去重后的结果
for (var i = 0; i < data.query.recentchanges.length; i++) {
var page = data.query.recentchanges[i];
var title = page.title;
if (!seenTitles.has(title)) { // 如果标题未出现过
seenTitles.add(title); // 记录到Set
uniqueUpdates.push(page); // 加入去重结果
// 最多保留5个不重复的页面(可调整)
if (uniqueUpdates.length >= 5) break;
}
}
// 生成去重后的列表
var list = document.createElement('ul');
list.style.listStyle = 'none';
list.style.paddingLeft = '10px';
list.style.margin = '5px 0';
if (uniqueUpdates.length === 0) {
targetItem.innerHTML += '暂无更新';
return;
}
uniqueUpdates.forEach(function(page) {
var listItem = document.createElement('li');
var link = document.createElement('a');
link.href = mw.util.getUrl(page.title);
link.textContent = page.title;
link.style.color = '#0645ad';
listItem.appendChild(link);
list.appendChild(listItem);
});
targetItem.appendChild(list);
console.log('去重后显示 ' + uniqueUpdates.length + ' 个页面');
})
.catch(function(error) {
console.error('加载失败:', error);
targetItem.innerHTML = '最近更新:<br>加载失败';
});
}, 500);