MediaWiki:Gadget-edit0.js
外观
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
// [[en:MediaWiki:Gadget-edittop.js]]
// Optimized for MediaWiki 1.43+ and modern skins (Vector 2022), without redundant i18n
// Improved by: [Your Name/ID]
(function($, mw) {
'use strict';
// 1. 基础校验:仅在允许的操作和非负命名空间生效
const ALLOWED_ACTIONS = ['view', 'purge'];
const currentAction = mw.config.get('wgAction');
const currentNamespace = mw.config.get('wgNamespaceNumber');
if (!ALLOWED_ACTIONS.includes(currentAction) || currentNamespace < 0) {
return;
}
// 2. 页面加载完成后执行(确保DOM已渲染)
$(function() {
// 核心配置:简化版多语言标题(保留主流语种,按需扩展)
const BUTTON_TITLES = {
en: 'Edit lead section',
zh: '编辑简介',
'zh-hans': '编辑简介',
'zh-cn': '编辑简介',
'zh-hant': '編輯首段',
'zh-tw': '編輯首段',
de: 'Einleitungsabschnitt bearbeiten',
es: 'Editar sección introductoria',
fr: 'Modifier le résumé introductif',
ja: '導入部を編集',
ru: 'Редактировать вступление'
};
// 3. 确定按钮标题(优先用户语言,兜底英文)
const userLang = mw.config.get('wgUserLanguage');
const buttonTitle = BUTTON_TITLES[userLang] ?? BUTTON_TITLES.en;
// 4. 定位内容容器(兼容Vector 2022/旧版/自定义皮肤)
const $contentContainer = $('#content, #mw_content, .mw-content-container').first();
if (!$contentContainer.length) {
console.warn('Edittop Gadget: Content container not found, exiting.');
return;
}
// 5. 克隆现有编辑分段按钮(确保有可复用的按钮模板)
const $originalEditSection = $contentContainer.find('.mw-editsection:not(.plainlinks)').first();
if (!$originalEditSection.length) {
console.warn('Edittop Gadget: Original edit section button not found, exiting.');
return;
}
const $clonedButton = $originalEditSection.clone();
// 6. 清除克隆按钮的重复ID(避免DOM ID冲突)
$clonedButton.removeAttr('id').find('[id]').removeAttr('id');
// 7. 处理编辑链接(区分可视化编辑器VE和传统编辑器)
const $editLink = $clonedButton.find('a').first();
const isVE = $editLink.attr('href')?.includes('veaction=edit') ?? false;
// 用mw.util.getUrl生成标准链接,自动处理编码和参数格式
const editParams = {
[isVE ? 'veaction' : 'action']: 'edit',
section: 0, // 0代表页面首段(引言部分)
summary: '/* top */' // 预填编辑摘要,标识“编辑首段”操作
};
const editUrl = mw.util.getUrl(mw.config.get('wgPageName'), editParams);
// 8. 更新克隆按钮的属性(链接+标题)
$editLink.attr({
'href': editUrl,
'title': buttonTitle
});
// 9. 定位标题容器并添加按钮(适配所有主流皮肤标题结构)
const $titleContainer = $('#mw-page-title-main, #firstHeading, #content h1, #mw_header h1').first();
if ($titleContainer.length) {
$titleContainer.append($clonedButton);
} else {
console.warn('Edittop Gadget: Title container not found, button not added.');
}
});
})(jQuery, mw);