跳转到内容

User:魔琴/gadgets/quickredirectplus/index.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/* <nowiki>
 * 此脚本主要由ChatGPT生成。
 * License: CC0 1.0 Universal
 * <https://creativecommons.org/publicdomain/zero/1.0/deed.en>
 */

( function () {
    var api = new mw.Api();

    function RedirectDialog( config ) {
        RedirectDialog.parent.call( this, config );
    }
    OO.inheritClass( RedirectDialog, OO.ui.ProcessDialog );

    // Dialog configuration
    RedirectDialog.static.name = 'redirectDialog';
    RedirectDialog.static.title = 'QR+快速创建重定向页面';
    RedirectDialog.static.actions = [
        { action: 'cancel', label: '取消', flags: 'safe' },
        { action: 'create', label: '创建', flags: [ 'primary', 'progressive' ] }
    ];
    RedirectDialog.static.size = 'medium';

    RedirectDialog.prototype.initialize = function () {
        RedirectDialog.parent.prototype.initialize.call( this );
        var panel = new OO.ui.PanelLayout({ padded: true, expanded: false });

        this.targetInput = new OO.ui.TextInputWidget({ placeholder: '待建页面' });
        this.sectionInput = new OO.ui.TextInputWidget();
        this.templateInput = new OO.ui.TextInputWidget({ placeholder: '不需「重定向」三字。每类用空格分开,如错字 繁简 模板' });
        this.summaryInput = new OO.ui.TextInputWidget();

        var fieldset = new OO.ui.FieldsetLayout({ label: '重定向设置' });
        fieldset.addItems([
            new OO.ui.FieldLayout(this.targetInput, { label: '将哪个页面重定向到此?', align: 'top' }),
            new OO.ui.FieldLayout(this.sectionInput, { label: '到哪个章节?(可选)', align: 'top' }),
            new OO.ui.FieldLayout(this.templateInput, { label: '重定向分类(可选)', align: 'top' }),
            new OO.ui.FieldLayout(this.summaryInput, { label: '编辑摘要(可选)', align: 'top' })
        ]);

        panel.$element.append(fieldset.$element);
        this.$body.append(panel.$element);
    };

    RedirectDialog.prototype.getActionProcess = function ( action ) {
        var dialog = this;
        if ( action === 'cancel' ) {
            return new OO.ui.Process(function () { dialog.close(); });
        }
        if ( action === 'create' ) {
            return new OO.ui.Process()
                .next(function () {
                    var title = dialog.targetInput.getValue().trim();
                    if (!title) {
                        mw.notify('请填写目标页面名称。', { type: 'error' });
                        return;
                    }
                    var current = mw.config.get('wgPageName').replace(/_/g, ' ');
                    var section = dialog.sectionInput.getValue().trim();
                    current = section
                        ? current + '#' + section
                        : current;
                    var lines = ['#REDIRECT [[' + current + ']]'];
                    var raw = dialog.templateInput.getValue().trim();
                    if (raw) {
                        raw.split(/\s+/).filter(Boolean).forEach(function(item) {
                            if (lines.length === 1) {
                                lines.push('', '{{Redirect category shell|');
                            }
                            lines.push('{{' + item + '重定向}}');
                        });
                        if (lines.length > 2) {
                            lines.push('}}');
                        }
                    }
                    var summary = dialog.summaryInput.getValue().trim();
                    summary = summary
                        ? summary + ' // '
                        : summary;
                    
                    // Send API request and handle response
                    api.post({
                        action: 'edit',
                        title: title,
                        text: lines.join('\n'),
                        summary: summary + '[[User:魔琴/gadgets/quickredirectplus|QR+快速重定向]]到[[' + current + ']]',
                        createonly: true,
                        token: mw.user.tokens.get('csrfToken')
                    })
                    .done(function () {
                        mw.notify('重定向创建成功:' + title, { type: 'success' });
                        dialog.close();
                    })
                    .fail(function (error) {
                        var info = error || '未知错误';
                        // Always notify raw error while
                        // Specific message for existing page
                        if (info.indexOf('articleexists') !== -1) {
                            mw.notify('该页面已经存在!', { type: 'error' });
                        } else {
                            mw.notify('错误:' + info, { type: 'error' });
                        }
                    });
                });
        }
        return RedirectDialog.parent.prototype.getActionProcess.apply(this, arguments);
    };

    function openRedirectDialog() {
        var dialog = new RedirectDialog();
        var wm = new OO.ui.WindowManager();
        $('body').append(wm.$element);
        wm.addWindows([dialog]);
        wm.openWindow(dialog);
    }

    function showRedirectButton() {
        var button = new OO.ui.ButtonWidget({ label: 'QR+快速创建重定向', icon: 'articleRedirect' });
        button.on('click', openRedirectDialog);
        $('#siteSub').append($('<div>').append(button.$element));
    }

    $(function () {
        mw.loader.using(['mediawiki.util', 'mediawiki.api', 'ext.gadget.site-lib'/* ,'ext.gadget.HanAssist'*/, 'oojs-ui-windows'], function () {
            var count = 0;
            if (mw.config.get('wgAction') === 'view' && !mw.config.get('wgIsRedirect')) {
                mw.util.addPortletLink(
                    'p-cactions',
                    '#',
                    'QR+快速创建重定向',
                    'ca-quickredirectplus',
                    '使用快速对话框创建重定向',
                    null,
                    null
                );
                $('#ca-quickredirectplus').click(function (e) {
                    if (count === 0){
                        showRedirectButton();
                        count++;
                    }
                    openRedirectDialog();
                });
            }
        });
    });
})();

// </nowiki>