User:KomacyaN/js/userStatus.js

< User:KomacyaN
白龙讨论 | 贡献2022年10月31日 (一) 18:19的版本 (建立内容为“/* // <pre> "use strict"; $(() => (async () => { if (location.hostname.startsWith("mzh.")) { return; } try { const builtinStatus = {…”的新页面)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/*
 
// <pre>
"use strict";
$(() => (async () => {
    if (location.hostname.startsWith("mzh.")) {
        return;
    }
    try {
        const builtinStatus = {
            online: '<img class="pt-userstatus-img" src="https://img.moegirl.org.cn/common/9/94/Symbol_support_vote.svg"> <b style="color:green;">在线</b>',
            busy: '<img class="pt-userstatus-img" src="https://img.moegirl.org.cn/common/c/c5/Symbol_support2_vote.svg"> <b style="color:blue;">正在来例假中</b>',
            offline: '<img class="pt-userstatus-img" src="https://img.moegirl.org.cn/common/7/7f/Symbol_oppose_vote.svg"> <b style="color:red;">离线</b>',
            away: '<img class="pt-userstatus-img" src="https://img.moegirl.org.cn/common/6/6c/Time2wait.svg"> <b style="color:grey;">已离开</b>',
            sleeping: '<img class="pt-userstatus-img" src="https://img.moegirl.org.cn/common/5/54/Symbol_wait.svg"> <b style="color:purple;">在睡觉</b>',
            wikibreak: '<img class="pt-userstatus-img" src="https://img.moegirl.org.cn/common/6/61/Symbol_abstain_vote.svg"> <b style="color:brown;">少女祈祷中</b>',
            holiday: '<img class="pt-userstatus-img" src="https://img.moegirl.org.cn/common/3/30/Symbol_deferred.svg"> <b style="color:#7B68EE;">处于假期中</b>',
            _unknown: '<img class="pt-userstatus-img" src="https://img.moegirl.org.cn/common/8/89/Symbol_neutral_vote.svg"> <i style="color:gray;">状态不详</i>',
        };
        const originalBuiltinStatusIndex = Object.keys(builtinStatus);
        mw.loader.addStyleTag("#pt-userstatus { margin-top: 0.75em !important; margin-bottom: 0px !important; } .pt-userstatus-img { width: 25px; margin-top: -0.25em; }");
        builtinStatus.on = builtinStatus.online;
        builtinStatus.off = builtinStatus.offline;
        builtinStatus.break = builtinStatus.wikibreak;
        builtinStatus.sleep = builtinStatus.sleeping;
        const userName = mw.config.get("wgUserName");
        if (userName === null) {
            return;
        }
        const statusPage = `User:${userName}/Status`;
        const now = new Date().getTime();
        let rawStatus;
        try {
            const localStatus = JSON.parse(localStorage.getItem("AnnTools-UserStatus"));
            if (mw.config.get("wgPageName") !== statusPage
                && typeof localStatus.timestamp === "number" && localStatus.timestamp > now - 10 * 60 * 1000
                && localStatus.status in builtinStatus) {
                rawStatus = localStatus.status;
            } else {
                throw new Error();
            }
        } catch {
            try {
                rawStatus = (await $.ajax({
                    url: `${mw.config.get("wgServer")}${mw.config.get("wgScriptPath")}/index.php`,
                    type: "GET",
                    data: {
                        title: statusPage,
                        action: "raw",
                    },
                    cache: false,
                })).trim();
                if (rawStatus in builtinStatus) {
                    localStorage.setItem("AnnTools-UserStatus", JSON.stringify({
                        timestamp: now,
                        status: rawStatus,
                    }));
                }
            } catch {
                rawStatus = "_unknown";
                localStorage.removeItem("AnnTools-UserStatus");
            }
        }
        const currentStatus = rawStatus in builtinStatus ? builtinStatus[rawStatus] : (() => {
            const div = $("<div/>").html(rawStatus);
            div.find("script, style, link, iframe, frame, object, param, audio, video, base, head, meta, title, body, h1, h2, h3, h4, h5, h6, blockquote, dd, dl, dir, dt, hr, li, ul, ol, pre, a, abbr, br, cite, code, data, em, rb, rp, rt, rtc, ruby, samp, time, tt, var, wbr, area, map, track, applet, embed, noembed, picture, source, canvas, noscript, caption, col, colgroup, table, tbody, thead, tfoot, td, th, tr, button, datalist, fieldset, form, input, label, legend, meter, optgroup, option, output, progress, select, textarea, details, dialog, menu, menuitem, summary, shadow, element, content, slot, template, bgsound, blink, center, command, frameset").remove();
            div.find("img").attr("class", "pt-userstatus-img");
            return div.html();
        })();
        const pt = $("<li id=\"pt-userstatus\"><a id=\"pt-userpage-link\" href=\"javascript:void(0);\" dir=\"auto\" title=\"您的状态\"></a></li>");
        pt.find("#pt-userpage-link").html(currentStatus).on("click", async () => {
            await mw.loader.using(["oojs-ui", "mw.Api"]);
            const messageDialog = new OO.ui.MessageDialog();
            const windowManager = new OO.ui.WindowManager();
            $("body").append(windowManager.$element);
            windowManager.addWindows([messageDialog]);
            messageDialog.title.$label.html("修改自己的状态");
            const container = $("<div/>");
            container.append(`<p>修改<a href="${mw.config.get("wgServer")}${mw.config.get("wgScriptPath")}/${statusPage}">自己的状态</a>为:</p>`);
            const builtinStatusList = originalBuiltinStatusIndex.map((data, i) => data === "_unknown" ? undefined : { data, label: `${i}`, html: builtinStatus[data] }).filter(n => !!n);
            const builtinStatusSelector = new OO.ui.RadioSelectInputWidget({
                value: rawStatus,
                options: builtinStatusList.map(({ data, label }) => ({ data, label })),
            });
            builtinStatusSelector.$element.find(".oo-ui-radioSelectWidget > .oo-ui-radioOptionWidget > .oo-ui-labelElement-label").each((_, labelEle) => {
                $(labelEle).css("overflow", "visible").html((builtinStatusList.filter(({ label }) => $(labelEle).text() === label)[0] || { html: $(labelEle).html() }).html);
            });
            container.append(builtinStatusSelector.$element);
            container.append(`<p>本工具在获取状态信息后有10分钟的缓存,您可以通过直接打开<a href="${mw.config.get("wgServer")}${mw.config.get("wgScriptPath")}/${statusPage}">自己的状态页</a>来强制获取最新状态信息。`);
            messageDialog.message.$label.append(container);
            const action = new OO.ui.ActionWidget({
                action: "confirm",
                label: "提交",
                flags: "primary",
            });
            const cAction = new OO.ui.ActionWidget({
                action: "cancel",
                label: "取消",
                flags: "primary",
            });
            cAction.$element[0].addEventListener("click", () => {
                windowManager.closeWindow(messageDialog);
            }, {
                capture: true,
            });
            action.$element[0].addEventListener("click", async () => {
                windowManager.closeWindow(messageDialog);
                const fMessageDialog = new OO.ui.MessageDialog();
                windowManager.addWindows([fMessageDialog]);
                const cAction = new OO.ui.ActionWidget({
                    action: "accept",
                    label: "我知道了",
                    flags: "primary",
                });
                cAction.$element[0].addEventListener("click", () => {
                    windowManager.closeWindow(fMessageDialog);
                }, {
                    capture: true,
                });
                try {
                    const status = builtinStatusSelector.getValue();
                    await new mw.Api().postWithToken("csrf", {
                        action: "edit",
                        title: statusPage,
                        text: status,
                        summary: `修改状态为 - ${status}`,
                        tags: "Automation tool",
                        minor: true,
                    });
                    localStorage.setItem("AnnTools-UserStatus", JSON.stringify({
                        timestamp: new Date().getTime(),
                        status: status,
                    }));
                    pt.find("#pt-userpage-link").html(builtinStatus[status]);
                    rawStatus = status;
                    fMessageDialog.title.$label.html("状态修改完成!");
                    fMessageDialog.message.$label.html(`<p>你的状态已修改为:${builtinStatus[status]}</p>`);
                    const action = new OO.ui.ActionWidget({
                        action: "confirm",
                        label: "确定",
                        flags: "primary",
                    });
                    action.$element[0].addEventListener("click", () => {
                        windowManager.closeWindow(fMessageDialog);
                    }, {
                        capture: true,
                    });
                    windowManager.openWindow(fMessageDialog, {
                        actions: [action],
                    });
                } catch (e) {
                    fMessageDialog.title.$label.html("状态修改发生错误……");
                    fMessageDialog.message.$label.html(`错误信息为:${e}`);
                    windowManager.openWindow(fMessageDialog, {
                        actions: [cAction],
                    });
                }
            }, {
                capture: true,
            });
            windowManager.openWindow(messageDialog, {
                actions: [action, cAction],
            });
            return false;
        });
        $("#pt-userpage").after(pt);
        mw.loader.using(["oojs-ui", "mw.Api"]);
    } catch (reason) {
        console.error(reason);
        const lastError = sessionStorage.getItem("AnnTools-userstatus-img-Error");
        if (lastError !== reason.toString()) {
            alert(`显示用户状态工具发生错误:\n${reason}`);
            sessionStorage.setItem("AnnTools-userstatus-img-Error", reason);
        }
    }
})());
// </pre>
//Copy from Anna
 
*/