跳转到内容

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

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * Generated by ChatGPT.
 * License: CC0
 * 
 * MediaWiki Gadget to convert ConvenientDiscussions participants list into ping2 templates.
 *
 * Usage:
 * 1. Install this script as a gadget in your wiki personal JS.
 * 2. Paste your raw list when prompted. The tool will remove your own username,
 *    chunk into groups of 50, and output {{ping2|...}} blocks for manual copy.
 */
(function () {
    'use strict';

    function chunkArray(arr, size) {
        const result = [];
        for (let i = 0; i < arr.length; i += size) {
            result.push(arr.slice(i, i + size));
        }
        return result;
    }

    function generatePing2Chunks(users) {
        const chunks = chunkArray(users, 50);
        const total = chunks.length;
        const blocks = chunks.map((chunk, idx) => {
            const params = chunk.map((user, i) => `${i+1}=${user}`).join('|');
            const prefix = total > 1 ? `(${idx+1}/${total}) ` : '';
            return `${prefix}{{ping2|${params}}}`;
        });
        return blocks;
    }

    function cleanInput(raw) {
        // Split on ideographic comma U+3001 and zero-width space U+200B
        const parts = raw.split(/[、​]+/);
        return parts
            .map(u => u.trim())
            .filter(u => u.length > 0);
    }

    function ping2Tool() {
        const userName = mw.user.getName();
        const raw = prompt('Paste the raw participants list:');
        if (!raw) {
            mw.notify('No input provided.', { type: 'error' });
            return;
        }
        let users = cleanInput(raw).filter(u => u !== userName);

        if (users.length === 0) {
            mw.notify('No users left after removing your username.', { type: 'warning' });
            return;
        }

        const blocks = generatePing2Chunks(users);
        let output = '';

        if (blocks.length > 1) {
            output += `提及对象超过50人,请分${blocks.length}次发送。\n\n`;
        }
        output += blocks.join('\n\n');

        // Show result in prompt for manual copying
        prompt('Ping2 templates generated (copy the text below):', output);
    }

    // Add to Tools menu
    mw.loader.using('mediawiki.util').then(function () {
        $(function () {
            mw.util.addPortletLink(
                'p-tb',
                '#',
                'Ping All from CD',
                't-pingallfromcd',
                'Convert ConvenientDiscussions participants list into ping2 templates',
                null,
                '#t-print'
            );
            $('#t-pingallfromcd').click(function (e) {
                e.preventDefault();
                ping2Tool();
            });
        });
    });
})();