跳转到内容

User:BlackShadowG/js/StatusChanger-t.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//此腳本最初來源於萌娘百科(zh.moegirl.org),原作者為AnnAngela,源地址為https://zh.moegirl.org/User:AnnAngela/js/userStatus.js,經原作者同意後由BlackShadowG修改後用於維基百科。
//使用說明參見:[[User:BlackShadowG/StatusChanger]]
"use strict";
setTimeout(async () => {
    try {
        const builtinStatus = {
            online: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/9/94/Symbol_support_vote.svg"> <b style="color:green;">在線</b>',
            offline: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/8/85/Symbol_unrelated.svg"> <b style="color:red;">離線</b>',
            busy: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/c/c5/Symbol_support2_vote.svg"> <b style="color:blue;">忙碌</b>',
            editing: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/e/e0/Accessories-text-editor.svg"> <b style="color:green;">編輯中</b>',
            vandal: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/0/09/Berisping.png"> <b style="color:purple;">對抗破壞中</b>',
            away: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/6/6c/Time2wait.svg"> <b style="color:grey;">已離開</b>',
            sleeping: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/5/54/Symbol_wait.svg"> <b style="color:purple;">睡覺中</b>',
            eating: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/d/d1/Symbol_dot_dot_dot_violet.svg"> <b style="color:violet;">吃飯中</b>',
            wikibreak: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/6/61/Symbol_abstain_vote.svg"> <b style="color:brown;">正在放維基假期</b>',
            holiday: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/3/30/Symbol_deferred.svg"> <b style="color:#7B68EE;">放假中</b>',
            school: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/4/4a/School.svg"> <b style="color:red;">在學校</b>',
            working: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/e/e0/Accessories-text-editor.svg"> <b style="color:orange;">在工作</b>',
            _unknown: '<img class="pt-userstatus-img" src="https://backend.710302.xyz/https/upload.wikimedia.org/wikipedia/commons/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: "https://baike.710302.xyz/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", "mediawiki.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="https://baike.710302.xyz/wiki/${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="https://baike.710302.xyz/wiki/${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}`,
                        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", "mediawiki.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);
        }
    }
}, 1);