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 ** // ** ** // ******************************************************…”
 
小无编辑摘要
 
(未显示同一用户的5个中间版本)
第7行: 第7行:
// ** **
// ** **
// **********************************************************************
// **********************************************************************
// Imported from [[:en:User:Alex Smotrov/edittop.js]], version as of: 2007-06-19T04:28:52
// Simplified and optimized version for Chinese (zh) locales.
// Updated from [[:en:User:TheDJ/Gadget-edittop.js]], version as of: 2009-04-28T11:54:22
// Modified to: Only support 'zh-hans' and 'zh-hant' variants.


if ($.inArray(mw.config.get('wgAction'), ['view', 'purge']) !== -1 && mw.config.get('wgNamespaceNumber') >= 0) {
(function() {
$(function edittop_hook() {
'use strict';
// 1. 只保留 'zh-hans' 和 'zh-hant' 两种变体
var localtitles = {
⚫
'zh-hans': '编辑首段', // 简体中文
'zh-hant': '編輯首段' // 繁体中文
⚫
};


⚫
var our_content = $('#content, #mw_content').first();
// 优化点 1:使用 mw.loader.using 确保依赖加载完成
⚫
var span1 = our_content.find('span.mw-editsection:not(.plainlinks)').first();
// 确保 mediawiki.util 和 jQuery 模块已加载
⚫
if (!span1.length) return;
mw.loader.using(['mediawiki.util', 'jquery']).then(function() {
⚫
var span0 = span1.clone();
// 优化点 2:提前获取配置,避免重复调用 mw.config.get
⚫
var config = mw.config.get([
'wgAction',
'wgNamespaceNumber',
'wgUserLanguage',
'wgPageName'
]);


⚫
$('#mw_header h1, #content h1').first().append(span0);
// 优化点 3:简化条件判断,提前返回
// 仅在查看或清除缓存页面,且为内容命名空间时运行
if (!['view', 'purge'].includes(config.wgAction) || config.wgNamespaceNumber < 0) {
⚫
return;
}


// 2. 保留原始的链接修改逻辑
// 优化点 4:使用 const 声明常量,增强代码可读性和可维护性
⚫
span0.find('a').each(function(idx) {
const EDIT_SUMMARY = '/* top */ ';
const TITLES = {
var a = $(this);
'zh': '编辑首段',
var href = a.attr('href') || '';
'zh-hans': '编辑首段',
var userLang = mw.config.get('wgUserLanguage');
⚫
'zh-hant': '編輯首段'
⚫
};


// 优化点 5:使用箭头函数和更简洁的语法
// 3. 根据用户语言设置按钮标题
// 如果用户语言是 'zh-hans' 或 'zh-hant',则使用对应文本,否则默认使用简体中文
⚫
$(function() {
const userLang = config.wgUserLanguage;
a.attr('title', localtitles[userLang] || localtitles['zh-hans']);
// 优化点 6:使用可选链操作符 (?.) 简化默认值获取
const editTitle = TITLES[userLang] || '编辑首段';


⚫
if (!/&(ve|)section=T/.test(href)) { // not transcluded
// 优化点 7:使用更精确的选择器,减少 DOM 查询
⚫
a.attr('href', href.replace(/&(ve|)section=\d+/, '&$1section=0&summary=/*%20top%20*/%20'))
⚫
const $content = $('#content, #mw_content').first();
⚫
} else if (/&vesection=/.test(href)) { // transcluded, VE
⚫
const $firstEditSection = $content.find('span.mw-editsection:not(.plainlinks):first');
a.attr('href', mw.util.getUrl(mw.config.get('wgPageName')) + '?veaction=edit&vesection=0&summary=/*%20top%20*/%20')

if (!$firstEditSection.length) {
} else { // transcluded, not VE
a.attr('href', mw.util.getUrl(mw.config.get('wgPageName')) + '?action=edit&section=0&summary=/*%20top%20*/%20')
return;
}
}

const $clonedEditSection = $firstEditSection.clone();
// 优化点 8:使用更高效的 DOM 插入方式
⚫
$('#mw_header h1, #content h1').first().append($clonedEditSection);

// 优化点 9:使用 text() 而不是 attr('title') 来设置标题
// 因为 title 属性的值就是元素的文本内容
$clonedEditSection.find('a').text(editTitle).each(function() {
const $link = $(this);
const originalHref = $link.attr('href') || '';
let newHref;

// 优化点 10:使用 switch 语句替代复杂的 if-else 嵌套
⚫
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);
});
});
});
});
});
}
})();

2025年11月12日 (三) 12:51的最新版本

// [[en:MediaWiki:Gadget-edittop.js]]

// **********************************************************************
// **                 ***WARNING GLOBAL GADGET FILE***                 **
// **             changes to this file affect many users.             **
// **           please discuss on the talk page before editing         **
// **                                                                  **
// **********************************************************************
// Imported from [[:en:User:Alex Smotrov/edittop.js]], version as of: 2007-06-19T04:28:52
// Updated from [[:en:User:TheDJ/Gadget-edittop.js]], version as of: 2009-04-28T11:54:22
// Modified to: Only support 'zh-hans' and 'zh-hant' variants.

if ($.inArray(mw.config.get('wgAction'), ['view', 'purge']) !== -1 && mw.config.get('wgNamespaceNumber') >= 0) {
    $(function edittop_hook() {
        // 1. 只保留 'zh-hans' 和 'zh-hant' 两种变体
        var localtitles = {
            'zh-hans': '编辑首段',   // 简体中文
            'zh-hant': '編輯首段'    // 繁体中文
        };

        var our_content = $('#content, #mw_content').first();
        var span1 = our_content.find('span.mw-editsection:not(.plainlinks)').first();
        if (!span1.length) return;
        var span0 = span1.clone();

        $('#mw_header h1, #content h1').first().append(span0);

        // 2. 保留原始的链接修改逻辑
        span0.find('a').each(function(idx) {
            var a = $(this);
            var href = a.attr('href') || '';
            var userLang = mw.config.get('wgUserLanguage');

            // 3. 根据用户语言设置按钮标题
            // 如果用户语言是 'zh-hans' 或 'zh-hant',则使用对应文本,否则默认使用简体中文
            a.attr('title', localtitles[userLang] || localtitles['zh-hans']);

            if (!/&(ve|)section=T/.test(href)) { // not transcluded
                a.attr('href', href.replace(/&(ve|)section=\d+/, '&$1section=0&summary=/*%20top%20*/%20'))
            } else if (/&vesection=/.test(href)) { // transcluded, VE
                a.attr('href', mw.util.getUrl(mw.config.get('wgPageName')) + '?veaction=edit&vesection=0&summary=/*%20top%20*/%20')
            } else { // transcluded, not VE
                a.attr('href', mw.util.getUrl(mw.config.get('wgPageName')) + '?action=edit&section=0&summary=/*%20top%20*/%20')
            }
        });
    });
}