User:Willy1018/test.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// Special:SpecialPages 分類 + 搜尋優化
mw.loader.using(['mediawiki.util'], function () {
if (mw.config.get('wgCanonicalSpecialPageName') == 'Specialpages') return;
const groupMap = {
'維護與錯誤檢查': ['Lint錯誤', '雙重的重新導向', '損壞的重新導向', '消歧義頁面', '無連結頁面', '未使用的模板'],
'分類與檔案': ['待分類頁面', '未使用的分類', '檔案清單', '搜尋重複檔案', '未分類的模板'],
'使用者與帳號管理': ['使用者清單', '使用者權限', '全域帳號清單', '建立帳號', '已封鎖的使用者'],
'統計與報表': ['最早頁面', '最少修訂的頁面', '最多修訂的條目', '統計'],
'最近活動': ['近期變更', '新頁面', '新建檔案', '日誌'],
'工具與API': ['API 沙盒', 'REST沙盒', '模板沙盒', '小工具統計'],
};
// 建立搜尋欄
const $searchBox = $('<input>')
.attr('placeholder', '🔍 搜尋特殊頁面...')
.css({ width: '100%', padding: '0.4em', margin: '1em 0' });
$('#mw-specialpagesbody').before($searchBox);
// 建立分組
const $container = $('<div>');
const $originalList = $('#mw-specialpagesbody > h4, #mw-specialpagesbody > ul');
const pageMap = new Map();
let currentHeader = null;
$originalList.each(function () {
if (this.tagName === 'H4') {
currentHeader = $(this).text().trim();
} else if (this.tagName === 'UL') {
$(this).find('li').each(function () {
const name = $(this).text().trim();
pageMap.set(name, $(this));
});
}
});
for (const [groupName, pages] of Object.entries(groupMap)) {
const $section = $('<details open>').css('margin-bottom', '1em');
const $summary = $('<summary>').text(groupName).css('font-weight', 'bold');
const $ul = $('<ul>');
for (const pageName of pages) {
const $li = pageMap.get(pageName);
if ($li) $ul.append($li);
}
$section.append($summary, $ul);
$container.append($section);
}
// 剩餘未歸類頁面放入「其他」
const remaining = Array.from(pageMap.entries()).filter(([name, $li]) => {
return !Object.values(groupMap).flat().includes(name);
});
if (remaining.length > 0) {
const $section = $('<details>').css('margin-bottom', '1em');
const $summary = $('<summary>').text('其他').css('font-weight', 'bold');
const $ul = $('<ul>');
for (const [name, $li] of remaining) {
$ul.append($li);
}
$section.append($summary, $ul);
$container.append($section);
}
$('#mw-specialpagesbody').empty().append($container);
// 搜尋過濾功能
$searchBox.on('input', function () {
const val = $(this).val().toLowerCase();
$container.find('li').each(function () {
const txt = $(this).text().toLowerCase();
$(this).toggle(txt.includes(val));
});
});
});