User:Kurgenera/link.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// =====================================================================
// ShowDomains.js - 外部链接域名显示工具 (带恢复和标记版)
// 功能: 按钮触发,将链接文本替换为域名并加标记,并提供“恢复”功能。
// =====================================================================
(function(mw, $) {
// --- 1. 触发条件检查 ---
const namespace = mw.config.get('wgNamespaceNumber');
const allowedNamespaces = [0, 2, 118];
if (allowedNamespaces.indexOf(namespace) === -1 || mw.config.get('wgIsRedirect')) {
return;
}
// --- 2. 配置 ---
const DOMAIN_EMOJI = '🔗'; // 用于标记域名的表情符号
// 全局变量来引用侧边栏的两个按钮
let $showButtonLink;
let $restoreButtonLink;
/**
* 从完整的 URL 中提取域名
* @param {string} url - 完整的 URL 字符串
* @returns {string|null} 提取出的域名
*/
function extractDomain(url) {
try {
const parsedUrl = new URL(url);
let hostname = parsedUrl.hostname;
if (hostname.startsWith('www.')) {
hostname = hostname.substring(4);
}
return hostname;
} catch (e) {
return null;
}
}
// --- 3. 核心功能:替换并标记 ---
/**
* 扫描页面并替换外部链接文本为域名,并添加 Emoji 标记。
*/
function replaceLinkTextWithDomain() {
/**
// 1. 通知用户开始
mw.notify('正在替换外部链接文本为域名并添加标记...', { tag: 'show-domains-loading', type: 'info', timeout: 2000 });
*/
let count = 0;
// 确保工具在运行前清理旧的标记(防止重复运行时的视觉错误)
$('.domain-tag').remove();
// 2. 扫描和替换
$('#mw-content-text a.external').each(function() {
const $link = $(this);
const url = $link.attr('href');
if (!url) {
return;
}
const domain = extractDomain(url);
if (domain) {
const currentText = $link.text().trim();
// 仅当链接文本与域名不一致时才进行替换
if ($link.data('original-text') || currentText !== domain) {
// 存储原始文本 (如果尚未存储)
if (!$link.data('original-text')) {
$link.data('original-text', currentText);
}
// 替换链接文本
$link.text(domain);
// 添加 Emoji 标记
const $tag = $('<span>')
.text(DOMAIN_EMOJI)
.addClass('domain-tag')
.css({
'margin-left': '4px',
'cursor': 'pointer', // 可点击样式
'color': '#0052D9' // 蓝色突出显示
});
$link.after($tag);
count++;
}
}
});
// 3. 切换按钮状态
if (count > 0) {
$showButtonLink.hide();
$restoreButtonLink.show();
}
// 4. 完成通知
mw.notify(`域名显示工具完成。已修改 ${count} 个外部链接的文本并添加标记。`, { tag: 'show-domains-complete', type: 'success', timeout: 4000 });
}
// --- 4. 核心功能:恢复原始文本 ---
/**
* 恢复所有被修改的外部链接为原始文本,并移除标记。
*/
function restoreOriginalText() {
/**
// 1. 通知用户开始
mw.notify('正在恢复外部链接的原始文本...', { tag: 'restore-loading', type: 'info', timeout: 2000 });
*/
let count = 0;
// 2. 扫描和恢复
$('#mw-content-text a.external').each(function() {
const $link = $(this);
const originalText = $link.data('original-text');
if (originalText) {
// 恢复链接文本
$link.text(originalText);
$link.removeData('original-text'); // 清除存储的数据
count++;
}
});
// 3. 移除所有标记
$('.domain-tag').remove();
// 4. 切换按钮状态
if (count > 0) {
$restoreButtonLink.hide();
$showButtonLink.show();
}
// 5. 完成通知
mw.notify(`域名显示工具恢复完成。已恢复 ${count} 个链接的文本。`, { tag: 'restore-complete', type: 'success', timeout: 4000 });
}
// --- 5. 触发工具的按钮 (侧边栏) ---
function initDomainButtons() {
// Helper function to create the sidebar link element
const createLink = (id, label, action) => {
const link = mw.util.addPortletLink(
'p-tb',
'#',
label,
id,
label,
null,
'#t-print'
);
if (link) {
$(link).find('a').on('click', function(e) {
e.preventDefault();
if (typeof URL === 'undefined') {
mw.notify('浏览器不支持 URL API,无法运行此工具。', { type: 'error' });
return;
}
action();
});
}
return $(link);
};
// A. 创建 '显示域名' 按钮
$showButtonLink = createLink('t-show-domains', '显示域名(Kurwa__Tools)', replaceLinkTextWithDomain);
// B. 创建 '恢复文本' 按钮 (默认隐藏)
$restoreButtonLink = createLink('t-restore-domains', '恢复原始文本(Kurwa__Tools)', restoreOriginalText);
$restoreButtonLink.hide(); // 初始隐藏
}
// DOM加载完成后初始化按钮
$(function() {
if (typeof $ === 'undefined' || typeof mw.notify !== 'function') {
return;
}
initDomainButtons();
});
})(mediaWiki, jQuery);