User:Bosco/common.js

From Test Wiki
Revision as of 13:11, 30 December 2025 by Bosco (talk | contribs) (=)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// rollback-summary (zh) with Simplified + Traditional Chinese
(() => {
    const messages = {
        hans: {
            prompt: '请输入自定义回退摘要(留空则使用系统预设摘要)',
            cancelNotify: '已取消回退操作。',
            summary: '已还原$1的编辑:$2',
            summaryUser: '[[Special:Contributions/$1|$1]]([[User talk:$1|对话]])',
            summaryNoUser: '已隐藏用户'
        },
        hant: {
            prompt: '請輸入自定義回退摘要(留空則使用系統預設摘要)',
            cancelNotify: '已取消回退操作。',
            summary: '已還原$1的編輯:$2',
            summaryUser: '[[Special:Contributions/$1|$1]]([[User talk:$1|對話]])',
            summaryNoUser: '已隱藏用戶'
        }
    };

    const hansLangs = ['zh-hans','zh-cn','zh-my','zh-sg'];
    const hantLangs = ['zh-hant','zh-hk','zh-mo','zh-tw'];

    const lang = mw.config.get('wgUserLanguage');
    let chosen;
    if (hansLangs.includes(lang)) {
        chosen = messages.hans;
    } else if (hantLangs.includes(lang)) {
        chosen = messages.hant;
    } else {
        chosen = messages.hans; // 預設簡體
    }

    const loadedMap = new WeakMap();

    mw.hook('wikipage.content').add(() => {
        for (const link of $('.mw-rollback-link a')) {
            if (loadedMap.has(link)) {
                continue;
            }
            loadedMap.set(link, true);

            $(link)
                .on('click', (ev) => {
                    const summary = prompt(chosen.prompt);
                    if (summary === null) {
                        ev.preventDefault();
                        mw.notify(chosen.cancelNotify);
                        return;
                    } else if (summary === '') {
                        return;
                    }

                    ev.preventDefault();
                    const url = new URL(link.href);
                    const username = url.searchParams.get('from');
                    url.searchParams.set(
                        'summary',
                        mw.format(
                            chosen.summary,
                            username ? mw.format(chosen.summaryUser, username) : chosen.summaryNoUser,
                            summary
                        )
                    );
                    window.location.assign(url.href);
                })
                .css('color', '#099');
        }
    });
})();