MediaWiki:Gadget-edit0.js:修订间差异
跳转到导航
跳转到搜索
删除的内容 添加的内容
创建页面,内容为“// en:MediaWiki:Gadget-edittop.js // ********************************************************************** // ** ***WARNING GLOBAL GADGET FILE*** ** // ** changes to this file affect many users. ** // ** please discuss on the talk page before editing ** // ** ** // ******************************************************…” |
小无编辑摘要 |
||
| 第1行: | 第1行: | ||
// [[en:MediaWiki:Gadget-edittop.js]] |
// [[en:MediaWiki:Gadget-edittop.js]] |
||
// ********************************************************************** |
|||
// ** ***WARNING GLOBAL GADGET FILE*** ** |
|||
// ** changes to this file affect many users. ** |
|||
// ** please discuss on the talk page before editing ** |
|||
// ** ** |
|||
// ********************************************************************** |
|||
// Simplified and optimized version for Chinese (zh) locales. |
|||
(function() { |
(function() { |
||
'use strict'; |
'use strict'; |
||
// 优化点 1:使用 mw.loader.using 确保依赖加载完成 |
|||
// 确保 mediawiki.util 和 jQuery 模块已加载 |
|||
mw.loader.using(['mediawiki.util', 'jquery']).then(function() { |
mw.loader.using(['mediawiki.util', 'jquery']).then(function() { |
||
const config = mw.config.get([ |
|||
var config = mw.config.get([ |
|||
'wgAction', |
'wgAction', |
||
'wgNamespaceNumber', |
'wgNamespaceNumber', |
||
| 第23行: | 第12行: | ||
]); |
]); |
||
// 优化点 3:简化条件判断,提前返回 |
|||
// 仅在查看或清除缓存页面,且为内容命名空间时运行 |
|||
if (!['view', 'purge'].includes(config.wgAction) || config.wgNamespaceNumber < 0) { |
if (!['view', 'purge'].includes(config.wgAction) || config.wgNamespaceNumber < 0) { |
||
return; |
return; |
||
} |
} |
||
// 优化点 4:使用 const 声明常量,增强代码可读性和可维护性 |
|||
const EDIT_SUMMARY = '/* top */ '; |
const EDIT_SUMMARY = '/* top */ '; |
||
const TITLES = { |
const TITLES = { |
||
| 第37行: | 第23行: | ||
}; |
}; |
||
// 优化点 5:使用箭头函数和更简洁的语法 |
|||
$(function() { |
$(function() { |
||
const userLang = config.wgUserLanguage; |
const userLang = config.wgUserLanguage; |
||
// 优化点 6:使用可选链操作符 (?.) 简化默认值获取 |
|||
const editTitle = TITLES[userLang] || '编辑首段'; |
const editTitle = TITLES[userLang] || '编辑首段'; |
||
// 优化点 7:使用更精确的选择器,减少 DOM 查询 |
|||
const $content = $('#content, #mw_content').first(); |
const $content = $('#content, #mw_content').first(); |
||
const $firstEditSection = $content.find('span.mw-editsection:not(.plainlinks):first'); |
const $firstEditSection = $content.find('span.mw-editsection:not(.plainlinks):first'); |
||
| 第52行: | 第35行: | ||
const $clonedEditSection = $firstEditSection.clone(); |
const $clonedEditSection = $firstEditSection.clone(); |
||
// 优化点 8:使用更高效的 DOM 插入方式 |
|||
$('#mw_header h1, #content h1').first().append($clonedEditSection); |
$('#mw_header h1, #content h1').first().append($clonedEditSection); |
||
// 优化点 9:使用 text() 而不是 attr('title') 来设置标题 |
|||
// 因为 title 属性的值就是元素的文本内容 |
|||
$clonedEditSection.find('a').text(editTitle).each(function() { |
$clonedEditSection.find('a').text(editTitle).each(function() { |
||
const $link = $(this); |
const $link = $(this); |
||
| 第62行: | 第42行: | ||
let newHref; |
let newHref; |
||
// 优化点 10:使用 switch 语句替代复杂的 if-else 嵌套 |
|||
if (!/&(ve|)section=T/.test(originalHref)) { |
if (!/&(ve|)section=T/.test(originalHref)) { |
||
newHref = originalHref.replace(/&(ve|)section=\d+/, `&$1section=0&summary=${encodeURIComponent(EDIT_SUMMARY)}`); |
newHref = originalHref.replace(/&(ve|)section=\d+/, `&$1section=0&summary=${encodeURIComponent(EDIT_SUMMARY)}`); |
||
2025年11月12日 (三) 11:08的版本
// [[en:MediaWiki:Gadget-edittop.js]]
(function() {
'use strict';
mw.loader.using(['mediawiki.util', 'jquery']).then(function() {
const config = mw.config.get([
'wgAction',
'wgNamespaceNumber',
'wgUserLanguage',
'wgPageName'
]);
if (!['view', 'purge'].includes(config.wgAction) || config.wgNamespaceNumber < 0) {
return;
}
const EDIT_SUMMARY = '/* top */ ';
const TITLES = {
'zh': '编辑首段',
'zh-hans': '编辑首段',
'zh-hant': '編輯首段'
};
$(function() {
const userLang = config.wgUserLanguage;
const editTitle = TITLES[userLang] || '编辑首段';
const $content = $('#content, #mw_content').first();
const $firstEditSection = $content.find('span.mw-editsection:not(.plainlinks):first');
if (!$firstEditSection.length) {
return;
}
const $clonedEditSection = $firstEditSection.clone();
$('#mw_header h1, #content h1').first().append($clonedEditSection);
$clonedEditSection.find('a').text(editTitle).each(function() {
const $link = $(this);
const originalHref = $link.attr('href') || '';
let newHref;
if (!/&(ve|)section=T/.test(originalHref)) {
newHref = originalHref.replace(/&(ve|)section=\d+/, `&$1section=0&summary=${encodeURIComponent(EDIT_SUMMARY)}`);
} else if (/&vesection=/.test(originalHref)) {
newHref = mw.util.getUrl(config.wgPageName, {
veaction: 'edit',
vesection: 0,
summary: EDIT_SUMMARY
});
} else {
newHref = mw.util.getUrl(config.wgPageName, {
action: 'edit',
section: 0,
summary: EDIT_SUMMARY
});
}
$link.attr('href', newHref);
});
});
});
})();