跳转到内容
打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
登录后可编辑和发表评论。

MediaWiki:Gadget-registerToDelete.js

MediaWiki界面

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl + F5Ctrl + R(Mac为 R
  • Google Chrome:Ctrl + Shift + R(Mac为 Shift R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl + F5
// <pre>
"use strict";
$(() => {
    const { wgRevisionId, wgArticleId, wgIsProbablyEditable, wgNamespaceNumber, wgUserName, wgPageName, wgNamespaceIds } = mw.config.get();
    try {
        if (
            $(".will2Be2Deleted")[0]
            || wgRevisionId === 0 && wgArticleId === 0
            || !wgIsProbablyEditable
            || wgNamespaceNumber === 8
        ) {
            return;
        }

        const $body = $("body");
        $("#mw-notification-area").appendTo($body);
        let loadReason = false;

        class FFDWindow extends OO.ui.ProcessDialog {
            static static = {
                ...super.static,
                tagName: "div",
                name: "lr-ffd",
                title: wgULS("挂删", "掛删"),
                actions: [
                    {
                        action: "cancel",
                        label: "取消",
                        flags: ["safe", "close", "destructive"],
                    },
                    {
                        action: "submit",
                        label: wgULS("确认", "確認"),
                        flags: ["primary", "progressive"],
                    },
                ],
            };
            constructor(config) {
                // Parent constructor
                super(config);

                this.storage = config.data.storage;
            }
            initialize() {
                // Parent method
                super.initialize();

                this.panelLayout = new OO.ui.PanelLayout({
                    scrollable: false,
                    expanded: false,
                    padded: true,
                });
                this.reasonsDropdown = new OO.ui.DropdownInputWidget({
                    options: [{
                        data: "",
                        label: "其他(请自行说明理由)",
                    }, {
                        data: "",
                        disabled: true,
                        label: "加载中……",
                    }],
                });
                this.detailsText = new OO.ui.TextInputWidget();
                this.dbCheckbox = new OO.ui.CheckboxInputWidget();
                this.enterCheckbox = new OO.ui.CheckboxInputWidget({
                    selected: this.storage.getItem("enterToSubmit"),
                });

                this.reasonsDropdownMenu = this.reasonsDropdown.dropdownWidget.getMenu();

                const reasonsField = new OO.ui.FieldLayout(this.reasonsDropdown, {
                    label: wgULS("挂删理由", "掛删緣由"),
                    align: "top",
                });
                const detailsField = new OO.ui.FieldLayout(this.detailsText, {
                    label: wgULS("详情", "詳情"),
                    align: "top",
                });
                const enterField = new OO.ui.FieldLayout(this.enterCheckbox, {
                    label: wgULS("理由详情按回车提交(浏览器级设置)", "緣由詳情按回車鍵提交(瀏覽器級設置)"),
                    align: "inline",
                });

                this.panelLayout.$element.append(
                    reasonsField.$element,
                    detailsField.$element,
                    enterField.$element,
                );

                this.reasonsDropdownMenu.connect(this, { toggle: "dropdownToggle" });
                this.detailsText.connect(this, { enter: "onEnter" });
                this.enterCheckbox.connect(this, { change: "setStorage" });

                this.$body.append(this.panelLayout.$element);
            }
            updateReasons(reasons) {
                this.reasonsDropdown.setOptions([{
                    data: "",
                    label: "其他(请自行说明理由)",
                }, ...reasons]);
            }
            setStorage(selected) {
                this.storage.setItem("enterToSubmit", selected);
            }
            onEnter() {
                if (this.enterCheckbox.isSelected()) {
                    this.executeAction("submit");
                }
            }
            dropdownToggle(visible) {
                if (visible) {
                    // Manually resize
                    const newHeight = parseFloat(this.$frame.css("height")) + 300;
                    this.withoutSizeTransitions(() => {
                        this.$frame.css("height", newHeight);
                        this.reasonsDropdownMenu.clip();
                        $(this.$frame).animate({
                            height: newHeight,
                        }, 700);
                    });
                } else {
                    this.updateSize();
                }
            }
            getBodyHeight() {
                return this.panelLayout.$element.outerHeight(true);
            }
            getReadyProcess(data) {
                return super.getReadyProcess(data).next(() => {
                    this.reasonsDropdown.focus();
                }, this);
            }
            getActionProcess(action) {
                if (action === "cancel") {
                    return new OO.ui.Process(() => {
                        this.close({ action });
                    }, this);
                } else if (action === "submit") {
                    return new OO.ui.Process($.when((async () => {
                        this.reason = this.reasonsDropdown.getValue();
                        this.detail = this.detailsText.getValue();
                        this.reason = `${this.reason ? `${this.reason}${this.detail ? `:${this.detail}` : ""}` : this.detail}`; // 压行
                        try {
                            await this.flagTemplate();
                            this.close({ action });
                            mw.notify(wgULS("即将刷新……", "即將刷新……"), {
                                title: wgULS("挂删成功", "掛删成功"),
                                type: "success",
                                tag: "lr-ffd",
                            });
                            setTimeout(() => location.reload(), 730);
                        } catch (e) {
                            console.error("[FlagForDeletion] Error:", e);
                            throw new OO.ui.Error(e);
                        }
                    })()).promise(), this);
                }
                // Fallback to parent handler
                return super.getActionProcess(action);
            }
            async flagTemplate() {
                const api = new mw.Api();
                const d = await api.postWithToken("csrf", {
                    action: "edit",
                    assertuser: wgUserName,
                    format: "json",
                    title: wgPageName,
                    text: `<noinclude>{{即将删除|1=${this.reason}|user=${wgUserName}}}</noinclude>`,
                    summary: `挂删:${this.reason}`,
                    nocreate: true,
                    watchlist: "nochange",
                    tags: "Automation tool",
                    contentmodel: "wikitext",
                });
                if (d.error) {
                    throw d.error.code;
                }
            }
        }

        const storage = new LocalObjectStorage("AnnTools-registerToDelete");

        const windowManager = new OO.ui.WindowManager();
        $body.append(windowManager.$element);
        const ffdDialog = new FFDWindow({
            size: "medium",
            data: { storage },
        });
        windowManager.addWindows([ffdDialog]);

        $(mw.util.addPortletLink("p-cactions", "#", wgULS("挂删", "掛删"), "ca-lr-ffd", `${wgULS("挂删本页", "掛删本頁")}[alt-shift-d]`), "d").on("click", async (e) => {
            e.preventDefault();
            windowManager.openWindow(ffdDialog);
            $body.css("overflow", "auto");
            if (loadReason === false) {
                loadReason = true;
                const { parse: { text: { "*": html } } } = await new mw.Api().post({
                    action: "parse",
                    assertuser: wgUserName,
                    page: wgNamespaceNumber === wgNamespaceIds.file ? "MediaWiki:Filedelete-reason-dropdown" : "MediaWiki:Deletereason-dropdown",
                    prop: "text",
                });
                const container = $("<div>");
                container.html(html);
                const result = [];
                const getReason = (ele) => {
                    result.push({
                        data: ele.innerText.trim(),
                        label: ele.innerText.trim(),
                    });
                };
                container.children(".mw-parser-output").children("ul").children("li").each((_, ele) => {
                    const $ele = $(ele);
                    if ($ele.children("ul").length > 0) {
                        result.push({
                            optgroup: $ele.clone().find("*").remove().end().text().trim(),
                        });
                        $ele.children("ul").children("li").each((_, e) => getReason(e));
                    } else {
                        getReason(ele);
                    }
                });
                ffdDialog.updateReasons(result);
            }
        });
    } catch (e) {
        /* eslint-disable no-var, prefer-arrow-functions/prefer-arrow-functions, prefer-arrow-callback, prefer-template */
        var parseError = function (errLike, _space/* ? */) {
            let space = _space;
            if (_space === void 0) {
                space = 4;
            }
            return JSON.stringify(errLike, function (_, v) {
                if (v instanceof Error) {
                    var stack = [];
                    if (v.stack) {
                        Reflect.apply(stack.push, stack, v.stack.split("\n").map(function (n) {
                            return n.trim();
                        }).filter(function (n) {
                            var _a;
                            return ((_a = n === null || n === void 0 ? void 0 : n.length) !== null && _a !== void 0 ? _a : -1) > 0;
                        }));
                    }
                    var keys = Object.keys(v).filter(function (key) {
                        return !Reflect.has(Error.prototype, key);
                    });
                    if (keys.length) {
                        stack.push(JSON.stringify(Object.fromEntries(keys.map(function (key) {
                            return [key, v[key]];
                        })), null, space));
                    }
                    return stack.join("\n").trim() || "";
                }
                return v;
            }, space).replace(/^"(.*)"$/, "$1");
        };
        oouiDialog.alert("错误信息:<br>" + oouiDialog.sanitize(parseError(e)), {
            title: "挂删工具发生错误",
        });
        console.error("[FlagForDeletion] Setup error:", e);
        /* eslint-enable */
    }
});
// </pre>