跳转到内容

MediaWiki:Gadget-edit0.js:修订间差异

勤求古训,博采众方
无编辑摘要
无编辑摘要
 
(未显示同一用户的2个中间版本)
第7行: 第7行:
// **                                                                  **
// **                                                                  **
// **********************************************************************
// **********************************************************************
// Optimized for MediaWiki configuration where $wgLanguageCode = 'zh'
// Imported from [[:en:User:Alex Smotrov/edittop.js]], version as of: 2007-06-19T04:28:52
// and $wgDefaultLanguageVariant = 'zh-hans'.
// 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) {
if ($.inArray(mw.config.get('wgAction'), ['view', 'purge']) !== -1 && mw.config.get('wgNamespaceNumber') >= 0) {
     $(function edittop_hook() {
     $(function edittop_hook() {
        'use strict';
         // 1. 只保留 'zh-hans' 和 'zh-hant' 两种变体
 
         var localtitles = {
         // 1. 语言标题配置
        // 键值对应 $wgVariants 的值
         var variantTitles = {
             'zh-hans': '编辑首段',  // 简体中文
             'zh-hans': '编辑首段',  // 简体中文
             'zh-hant': '編輯首段'    // 繁体中文
             'zh-hant': '編輯首段'    // 繁体中文
         };
         };
        var defaultTitle = '编辑首段'; // 最终回退
        // 2. 获取页面和用户配置
        var wgPageName = mw.config.get('wgPageName');
        var userVariant = mw.config.get('wgUserVariant'); // 优先使用用户变体
        // 3. 查找必要的 DOM 元素
        var $content = $('#content, #mw_content').first();
        var $firstEditSection = $content.find('span.mw-editsection:not(.plainlinks)').first();
        var $targetHeading = $('#mw_header h1, #content h1').first();


         // 4. 前置条件检查:如果找不到编辑区块或标题,则退出
         var our_content = $('#content, #mw_content').first();
        if (!$firstEditSection.length || !$targetHeading.length) {
         var span1 = our_content.find('span.mw-editsection:not(.plainlinks)').first();
            return;
        if (!span1.length) return;
         }
         var span0 = span1.clone();
       
        // 5. 关键修复:防止重复添加
        // 检查标题处是否已存在指向 section=0 的编辑链接
        if ($targetHeading.find('span.mw-editsection a[href*="section=0"]').length > 0) {
            return;
         }


         // 6. 克隆并添加编辑区块
         $('#mw_header h1, #content h1').first().append(span0);
        var $clonedEditSection = $firstEditSection.clone();
        $targetHeading.append($clonedEditSection);


         // 7. 确定并设置正确的标题文本
         // 2. 保留原始的链接修改逻辑
         var editTitle = variantTitles[userVariant] || defaultTitle;
         span0.find('a').each(function(idx) {
        $clonedEditSection.find('a').text(editTitle).attr('title', editTitle);
            var a = $(this);
            var href = a.attr('href') || '';
            var userLang = mw.config.get('wgUserLanguage');


        // 8. 修改链接地址
            // 3. 根据用户语言设置按钮标题
        $clonedEditSection.find('a').each(function() {
            // 如果用户语言是 'zh-hans' 或 'zh-hant',则使用对应文本,否则默认使用简体中文
             var $link = $(this);
             a.attr('title', localtitles[userLang] || localtitles['zh-hans']);
            var originalHref = $link.attr('href') || '';
            var newHref;
            var summary = '/* top */ ';


             if (!/&(ve|)section=T/.test(originalHref)) { // 非嵌入页面
             if (!/&(ve|)section=T/.test(href)) { // not transcluded
                 newHref = originalHref.replace(/&(ve|)section=\d+/, `&$1section=0&summary=${encodeURIComponent(summary)}`);
                 a.attr('href', href.replace(/&(ve|)section=\d+/, '&$1section=0&summary=/*%20top%20*/%20'))
             } else if (/&vesection=/.test(originalHref)) { // 嵌入页面,VE编辑器
             } else if (/&vesection=/.test(href)) { // transcluded, VE
                 newHref = mw.util.getUrl(wgPageName, {
                 a.attr('href', mw.util.getUrl(mw.config.get('wgPageName')) + '?veaction=edit&vesection=0&summary=/*%20top%20*/%20')
                    veaction: 'edit',
             } else { // transcluded, not VE
                    vesection: 0,
                 a.attr('href', mw.util.getUrl(mw.config.get('wgPageName')) + '?action=edit&section=0&summary=/*%20top%20*/%20')
                    summary: summary
                });
             } else { // 嵌入页面,传统编辑器
                 newHref = mw.util.getUrl(wgPageName, {
                    action: 'edit',
                    section: 0,
                    summary: 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')
            }
        });
    });
}