跳转到内容

User:NHC/markUserInCat.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * 修改自
 * https://zh1.bitforum.be.eu.org/w/index.php?title=MediaWiki:Gadget-MarkRights.js&oldid=70307084
 *
 * 在最近修改、监视列表、条目历史记录等位置以特殊格式显示有在特定分类中的用户
 * @author [[User:WhitePhosphorus]]
 */

$(function () {
    const CATEGORY_MARKS = [
        // 每行一个标记:分类名(不要加ns前缀),标记文字,标记颜色
        ["臺灣1000專題參與者", "1000", "#7F00FF"],  // 紫色
    ];
    const markUG = function () {
        let n_marks = CATEGORY_MARKS.length;
        let $users = $('a.mw-userlink:not(.mw-anonuserlink)');

        const getUsername = function (url) {
            let username = mw.util.getParamValue('title', url);
            let decode1 = function (username) {
                return decodeURIComponent((function (u) {
                    try {
                        return decodeURIComponent(u.replace('User:', '').replace(/_/g, ' '));
                    } catch (e) {
                        return u.replace('User:', '').replace(/_/g, ' ').replace(/%(?!\d+)/g, '%25');
                    }
                })(username))
            };
            if (username) {
                return decode1(username);
            }
            username = url.match(/\/wiki\/User:(.+?)$/);
            let decode2 = function (username) {
                return decodeURIComponent((function (u) {
                    try {
                        return decodeURIComponent(u.replace(/_/g, ' '));
                    } catch (e) {
                        return u.replace(/_/g, ' ').replace(/%(?!\d+)/g, '%25');
                    }
                })(username))
            };
            if (username) {
                return decode2(username[1]);
            }
            return null;
        };

        //let users = new Set($users.map((_, e) => sanitizeUsername(e.textContent)));
        let users = new Set($users.map((_, e) => getUsername($(e).attr('href'))));
        let users_marks = {};
        users.forEach(u => users_marks[u] = '');

        const done = function () {
            $users.each((_, el) => {
                let username = getUsername($(el).attr('href'));
                if (!username || !users_marks[username]) return;
                $(el).append(users_marks[username]);
            });
        };

        const process = function (data, text, color) {
            //debugger;
            if (!data.query || !data.query.categorymembers) return;
            let cat_users = data.query.categorymembers.map(e => e.title.substr(5));  // remove User: prefix
            // Intersection
            cat_users.filter(u => users.has(u)).forEach(u => users_marks[u] += `<sup style="color:${color}">${text}</sup>`);
            if (--n_marks <= 0) {
                done();
            }
        };
        let api = new mw.Api();
        for (let [category, mark_text, mark_color] of CATEGORY_MARKS) {
            // TODO: 懒得实现需要 cmcontinue 的情况了,反正分类不超过 500 个成员就不用管
            api.get({
                format: 'json',
                action: 'query',
                list: 'categorymembers',
                cmtitle: 'Category:' + category,
                cmprop: 'title',
                cmnamespace: 2,  // user
                cmlimit: 'max',
            }).done(data => process(data, mark_text, mark_color));
        }
    };
    mw.hook('wikipage.content').add(function(e) {
        if (e.attr('id') === 'mw-content-text') {
            markUG();
            return;
        }
        if (e.hasClass('mw-changeslist')) markUG();
    });
});