跳转到内容

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

勤求古训,博采众方
鹿野耕云留言 | 贡献
无编辑摘要
鹿野耕云留言 | 贡献
无编辑摘要
第1行: 第1行:
// [[en:MediaWiki:Gadget-edittop.js]]
// [[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. 基础校验:仅在允许的操作和非负命名空间生效
// **                ***WARNING GLOBAL GADGET FILE***                **
    const ALLOWED_ACTIONS = ['view', 'purge'];
// **            changes to this file affaect many users.             **
    const currentAction = mw.config.get('wgAction');
// **          please discuss on the talk page before editing        **
    const currentNamespace = mw.config.get('wgNamespaceNumber');
// **                                                                  **
    if (!ALLOWED_ACTIONS.includes(currentAction) || currentNamespace < 0) {
// **********************************************************************
        return;
// 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


if ($.inArray(mw.config.get('wgAction'), ['view', 'purge']) !== -1 && mw.config.get('wgNamespaceNumber') >= 0) {
    // 2. 页面加载完成后执行(确保DOM已渲染)
$(function edittop_hook() {
    $(function() {
var localtitles = {
        // 核心配置:简化版多语言标题(保留主流语种,按需扩展)
bg: 'Редактиране на началото',
        const BUTTON_TITLES = {
bn: 'সূচনা অনুচ্ছেদ সম্পাদনা করুন',
            en: 'Edit lead section',
cs: 'Editovat úvodní sekci',
            zh: '编辑简介',
en: 'Edit lead section',
            'zh-hans': '编辑简介',
fa: 'ویرایش بخش آغازین',
            'zh-cn': '编辑简介',
fr: 'Modifier le résumé introductif',
            'zh-hant': '編輯首段',
id: 'Sunting bagian atas',
            'zh-tw': '編輯首段',
it: 'Modifica della sezione iniziale',
            de: 'Einleitungsabschnitt bearbeiten',
ja: '導入部を編集',
            es: 'Editar sección introductoria',
kk: 'Кіріспе бөлімді өңдеу',
            fr: 'Modifier le résumé introductif',
min: 'Suntiang bagian ateh',
            ja: '導入部を編集',
ko: '도입부를 편집',
            ru: 'Редактировать вступление'
pa: 'ਸੋਧ',
        };
pt: 'Editar a seção superior',
'pt-br': 'Editar a seção superior',
sr: 'Уреди уводни део',
vi: 'Sửa phần mở đầu'
};
localtitles.zh = localtitles['zh-hans'] = localtitles['zh-cn'] = localtitles['zh-sg'] = localtitles['zh-my'] = '编辑简介';
localtitles['zh-hant'] = localtitles['zh-hk'] = localtitles['zh-mo'] = localtitles['zh-tw'] = '編輯首段';


var our_content = $('#content, #mw_content').first();
        // 3. 确定按钮标题(优先用户语言,兜底英文)
var span1 = our_content.find('span.mw-editsection:not(.plainlinks)').first();
        const userLang = mw.config.get('wgUserLanguage');
if (!span1.length) return;
        const buttonTitle = BUTTON_TITLES[userLang] ?? BUTTON_TITLES.en;
var span0 = span1.clone();


$('#mw_header h1, #content h1').first().append(span0);
        // 4. 定位内容容器(兼容Vector 2022/旧版/自定义皮肤)
span0.find('a').each(function(idx) {
        const $contentContainer = $('#content, #mw_content, .mw-content-container').first();
var a = $(this);
        if (!$contentContainer.length) {
var href = a.attr('href') || '';
            console.warn('Edittop Gadget: Content container not found, exiting.');
a.attr('title', localtitles[mw.config.get('wgUserLanguage')] || localtitles.zh);
            return;
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
        // 5. 克隆现有编辑分段按钮(确保有可复用的按钮模板)
a.attr('href', mw.util.getUrl(mw.config.get('wgPageName')) + '?veaction=edit&vesection=0&summary=/*%20top%20*/%20')
        const $originalEditSection = $contentContainer.find('.mw-editsection:not(.plainlinks)').first();
} else { // transcluded, not VE
        if (!$originalEditSection.length) {
a.attr('href', mw.util.getUrl(mw.config.get('wgPageName')) + '?action=edit&section=0&summary=/*%20top%20*/%20')
            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);

2025年11月2日 (日) 20:02的版本

// [[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);