User:Irukaza/common.js

H萌娘,万物皆可H的百科全书!
< User:Irukaza
imported>=海豚=2021年3月23日 (二) 21:28的版本
跳到导航 跳到搜索

注意:这类代码页面在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • 按住CTRL+SHIFT+DEL 或 ⌘-Shift-R来清除缓存!
  • 或尝试在地址栏的地址最后添加代码?_=1来访问最新页面。
    添加代码后的本页地址如下:-{R|https://hmoegirl.com/User:Irukaza/common.js?_=1}-
  • 你还可以在设置中勾选小工具在页面右上角添加清除缓存按钮!
mw.loader.load('https://www.hmoegirl.com/index.php?title=User:=海豚=/js/userStatus.js&action=raw&ctype=text/javascript');
/* eslint-disable no-magic-numbers */
/* global mw */
"use strict";
$(() => (async () => {
    await mw.loader.using(["mw.Api", "mediawiki.Uri"]);
    const groupsKey = ["bureaucrat", "sysop", "patroller", "goodeditor", "bot"].reverse();
    const groupsStr = {
        bureaucrat: "政",
        sysop: "管",
        patroller: "巡",
        goodeditor: "优",
        bot: "机",
    };
    let cache;
    try {
        cache = JSON.parse(localStorage.getItem("AnnTools-usergroup"));
        if (!$.isPlainObject(cache)
            || typeof cache.timestamp !== "number" || cache.timestamp < new Date().getTime() - 30 * 60 * 1000
            || !$.isPlainObject(cache.groups)) {
            cache = {};
        } else {
            for (const i of groupsKey) {
                if (!Array.isArray(cache.groups[i])) {
                    cache = {};
                    break;
                }
            }
        }
    } catch (e) {
        console.info("AnnTools-usergroup", e);
        cache = {};
    }
    localStorage.setItem("AnnTools-usergroup", JSON.stringify(cache));
    if (!$.isPlainObject(cache.groups)) {
        const api = new mw.Api();
        const result = await (async () => {
            const result = {};
            const eol = Symbol();
            let aufrom = undefined;
            while (aufrom !== eol) {
                const _result = await api.post({
                    action: "query",
                    list: "allusers",
                    augroup: groupsKey.join("|"),
                    aulimit: "max",
                    auprop: "groups",
                    aufrom,
                });
                if (_result.continue) {
                    aufrom = _result.continue.aufrom;
                } else {
                    aufrom = eol;
                }
                _result.query.allusers.forEach(({
                    name,
                    groups,
                }) => {
                    groups.forEach((group) => {
                        if (groupsKey.includes(group)) {
                            result[group] = result[group] || [];
                            if (!result[group].includes(name)) {
                                result[group].push(name);
                            }
                        }
                    });
                });
            }
            return result;
        })();
        cache.timestamp = new Date().getTime();
        cache.groups = result;
        localStorage.setItem("AnnTools-usergroup", JSON.stringify(cache));
    }
    $("a.mw-userlink").each((_, ele) => {
        const uri = new mw.Uri(ele.href);
        let username;
        const path = decodeURI(uri.path);
        if (/^\/User:[^/=%]+/.test(path)) {
            username = path.match(/^\/User:([^/=%]+)/)[1].replace(/_/g, " ");
        } else if (/^User:[^/=%]+/.test(uri.query.title)) {
            username = uri.query.title.match(/^User:([^/=%]+)/)[1].replace(/_/g, " ");
        }
        if (username) {
            const self = $(ele);
            groupsKey.forEach((group) => {
                if (cache.groups[group].includes(username)) {
                    self.after(`<sup class="markrights-${group}">${groupsStr[group]}<sup>`);
                }
            });
        }
    });
    $("body").append("<style>.markrights-bureaucrat{color:#66CCFF}.markrights-sysop{color:#FF7F50}.markrights-patroller{color:#DA70D6}.markrights-goodeditor{color:#FFB6C1}.markrights-bot{color:#40E0D0}sup[class^=markrights]+sup[class^=markrights]{margin-left:2px}</style>");
})());
//改自对A姐,删除了萌百不必要显示的职位,修改了颜色。