User:Kurgenera/black.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// 用户名黑块隐藏脚本
$(function() {
if (!window.mw) {
console.error("MediaWiki environment not detected.");
return;
}
// 排除:0=条目, 6=文件, 8=系统界面, 10=模板, 12=帮助, 14=分类
// 排除所有扩展名空间,只专注于站务、讨论、用户和特殊页。
/*
const EXCLUDED_NAMESPACES = [
-2, 0, 6, 12, 14, 100, 126, 710, 828, 1728, 2600
];
if (EXCLUDED_NAMESPACES.includes(currentNamespace)) {
return;
}
*/
// *****************************************************************
// 1. 在此数组中填入您希望隐藏的用户名列表 (确保名称准确)
var targetUsers = ["Example"];
// *****************************************************************
// 2. 注入必要的 CSS 样式
var blackoutCSS =
// 定义黑色字块的样式
'.hidden-user-block { ' +
'background-color: black !important; ' + // 黑色背景
'color: black !important; ' + // 黑色文字(实现隐藏)
'padding: 2px 4px !important; ' + // 增加一些内边距,使其更像块状
'border-radius: 2px !important; ' + // 轻微圆角
'text-decoration: none !important; ' + // 移除下划线
'cursor: default !important; ' + // 鼠标悬停时不改变光标
'}' +
// 定义鼠标悬停时的样式(防止用户通过高亮选中来阅读内容)
'.hidden-user-block:hover { ' +
'background-color: #333 !important; ' + // 悬停时颜色略微变浅,但仍保持不可读
'color: #333 !important; ' +
'}';
mw.util.addCSS(blackoutCSS);
// 3. 查找并替换链接
targetUsers.forEach(function(username) {
// 使用 jQuery 的选择器逃逸函数,以防用户名中包含特殊字符
var escapedName = $.escapeSelector(username);
// 构建选择器:查找 href 属性中包含 User:Name 或 User_talk:Name 的链接
var userSelector = 'a[href*="/wiki/User:' + escapedName + '"], a[href*="/wiki/User_talk:' + escapedName + '"], a[href*="/wiki/Special:Contributions/' + escapedName + '"]';
$(userSelector).each(function() {
var $link = $(this);
// 避免重复处理或处理隐藏元素
if ($link.is(':visible') && !$link.hasClass('hidden-user-block')) {
$link
.addClass('hidden-user-block') // 应用黑色块样式
.text(' [隐藏] ') // 将链接文字替换为占位符
.attr('title', '该用户已被您的自定义脚本隐藏。') // 替换工具提示
.attr('href', 'javascript:void(0)'); // 阻止链接点击(可选)
}
});
});
});