User:PexEric/IPE-CatEdit.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
* @name IPE-category-quickedit
* @author PexEric
* @description InPageEdit插件:在分类页面的每个条目链接后添加快速编辑按钮。兼容CatListMainTalkLinks.js。
* @version 0.0.1
*/
mw.hook('InPageEdit').add(({ quickEdit }) => {
// 1. 本地国际化 (i18n) 定义
const langPack = {
'zh-hans': {
ipe_cat_edit_tooltip: '快速编辑 “$1”',
ipe_cat_edit_button_text: '[编]',
},
'zh-hant': {
ipe_cat_edit_tooltip: '快速編輯 「$1」',
ipe_cat_edit_button_text: '[編]',
},
en: {
ipe_cat_edit_tooltip: 'Quick edit "$1"',
ipe_cat_edit_button_text: '[E]',
}
};
// 本地消息查找函数 (与之前解决方案中相同)
const getLocalMsg = (key, ...params) => {
const userLang = mw.config.get('wgUserLanguage');
let translations = langPack[userLang];
if (!translations && userLang.includes('-')) {
const baseLang = userLang.split('-')[0];
translations = langPack[baseLang];
}
if (!translations) {
translations = langPack['zh-hans'] || langPack['en']; // 指定后备语言
}
let message = translations[key] || `(${key})`; // 未找到则返回带括号的 key
if (params.length > 0 && typeof message === 'string') {
params.forEach((param, index) => {
const paramStr = (typeof param === 'string' || typeof param === 'number') ? param : '';
message = message.replace(new RegExp(`\\$${index + 1}`, 'g'), paramStr);
});
}
return message;
};
// --- 不再需要合并到全局 ipe.i18n ---
// 2. 添加按钮样式 (CSS) - 保持不变
mw.util.addCSS(`
.ipe-category-edit-button {
margin-left: 4px;
font-size: 90%;
cursor: pointer;
color: #0645ad;
user-select: none;
display: inline-block;
vertical-align: middle;
}
.ipe-category-edit-button:hover {
text-decoration: underline;
color: #0b0080;
}
`);
// 3. 核心逻辑 (DOM Ready 回调内)
$(function() {
if (mw.config.get('wgNamespaceNumber') !== 14) return;
const categoryContentArea = document.getElementById('mw-pages') || document.querySelector('.mw-category-generated');
if (!categoryContentArea) return;
// 函数:添加编辑按钮
const addEditButtons = () => {
$(categoryContentArea).find('li a:not([data-ipe-processed="true"])').each(function() {
const $link = $(this);
const pageTitle = $link.attr('title');
// 标记为已处理
$link.data('ipe-processed', true);
if (!pageTitle || $link.hasClass('new')) {
return; // 跳过无标题或红链
}
// 创建按钮 - 使用 getLocalMsg 获取文本
const $editButton = $('<span>', {
class: 'ipe-category-edit-button',
text: getLocalMsg('ipe_cat_edit_button_text'), // 使用本地 i18n
title: getLocalMsg('ipe_cat_edit_tooltip', pageTitle) // 使用本地 i18n
});
// 绑定点击事件 (使用 quickEdit)
$editButton.on('click', function(e) {
e.preventDefault(); e.stopPropagation();
const titleToEdit = $link.attr('title');
if (titleToEdit) {
// 优先使用钩子传入的 quickEdit,提供后备以防万一
const doEdit = quickEdit || (window.InPageEdit && window.InPageEdit.quickEdit);
if (doEdit) {
doEdit({ page: titleToEdit, reload: false });
} else {
console.warn('[IPE-category-quickedit v1.2.0] InPageEdit.quickEdit not found!');
mw.notify('无法启动 InPageEdit。', { type: 'error' });
}
} else {
console.warn('[IPE-category-quickedit v1.2.0] Could not get page title on click from link:', $link);
mw.notify('无法获取页面标题以进行编辑。', { type: 'warn' });
}
});
// 插入按钮
$link.after($editButton);
});
};
// 首次运行
addEditButtons();
// --- MutationObserver 逻辑保持不变 (用于兼容 CatListMainTalkLinks.js 等) ---
const observerCallback = function(mutationsList) {
let needsUpdate = false;
for(const mutation of mutationsList) {
// 检查是否有未处理的链接被添加
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
$(mutation.addedNodes).each(function() {
const $node = $(this);
if ($node.is('li') && $node.find('a:not([data-ipe-processed="true"])').length > 0) {
needsUpdate = true; return false; // break $.each
} else if ($node.is('a') && !$node.data('ipe-processed') && $node.closest('li').length > 0) {
needsUpdate = true; return false;
} else if ($node.find('li a:not([data-ipe-processed="true"])').length > 0) {
needsUpdate = true; return false;
}
});
}
if (needsUpdate) break; // break mutationsList loop
}
if (needsUpdate) {
clearTimeout(this.debounceTimer); // 使用 this 访问 observer 实例属性
this.debounceTimer = setTimeout(addEditButtons, 150);
}
};
const observer = new MutationObserver(observerCallback);
const config = { childList: true, subtree: true };
observer.observe(categoryContentArea, config);
// mw.hook('wikipage.unload').add(() => observer.disconnect()); // 可选
}); // 结束 DOM Ready
}); // 结束 mw.hook('InPageEdit').add