跳转到内容

User:XZPeiQi/PatrolProBeta.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
(function() {
    function ready() {
        const api = new mw.Api();
        mw.log.warn("PatrolPro state: ready");

        function onError(e) {
            let message;
            mw.log.warn("错误", e);
            switch (e) {
                case "badvalue":
                case "missingparma":
                    message = "脚本不兼容";
                    break; 
                case "permissiondenied":
                    message = "权限不足";
                    break; 
                case "badtoken":
                    message = "请重新登录";
                    break; 
                case "http":
                    message = "网络错误";
                    break; 
                default:
                    message = "未知错误";
            }
            makeMessage(message);
        }

        function makeMessage(message) {
            let msg = new OO.ui.MessageWidget({
                label: message
            });
            msg.$element.addClass("pp-msg");
            $(document.body).append(msg.$element);
            setTimeout(() => {
                msg.toggle();
            }, 4000);
        }

        function patrol() {
            let revid = mw.config.get("wgCurRevisionId");
            api.getToken("patrol")
                .done(function(patrolToken) {
                    api.post({
                            action: "patrol",
                            token: patrolToken,
                            revid: revid
                        })
                        .done(() => {
                            mw.log.warn("巡查了", mw.config.get("wgPageName"));
                            makeMessage("巡查成功");
                        })
                        .fail((e) => {
                            onError(e);
                        });
                })
                .fail((e) => {
                    onError(e);
                });
        }

        function rollback(addComment, comment1, comment2, commentContent) {
            let title = mw.config.get("wgRelevantPageName");
            api.get({
                    action: "query",
                    prop: "revisions",
                    rvprop: "user",
                    rvlimit: 1,
                    titles: title
                })
                .done(function(result) {
                    let user = result.query.pages[Object.keys(result.query.pages)[0]].revisions[0].user;
                    api.getToken("rollback")
                        .done(function(rollbackToken) {
                            api.post({
                                    action: "rollback",
                                    title: title,
                                    user: user,
                                    summary: `[PatrolPro] ${addComment && commentContent ? commentContent : ""}`,
                                    token: rollbackToken
                                })
                                .done(function() {
                                    makeMessage("回退成功");
                                    mw.log.warn("回退了", title);
                                    if (addComment && commentContent) {
                                        if (comment1) {
                                            api.getToken("csrf")
                                                .done(function(csrfToken) {
                                                    api.post({
                                                            action: "edit",
                                                            title: `Talk:${title}`,
                                                            section: "new",
                                                            sectiontitle: "回退留言",
                                                            text: `您好,${user},您在这里编辑的内容不符合方针与指引,现在已经被回退。巡查员留言:${commentContent} ${unescape("%7E%7E%7E%7E")}`,
                                                            summary: "PatrolPro巡查自动留言",
                                                            token: csrfToken
                                                        })
                                                        .done(() => {})
                                                        .fail((e) => {
                                                            onError(e);
                                                        });
                                                })
                                                .fail((e) => {
                                                    onError(e);
                                                });
                                        }

                                        if (comment2) {
                                            api.getToken("csrf")
                                                .done(function(csrfToken) {
                                                    api.post({
                                                            action: "edit",
                                                            title: `User Talk:${user}`,
                                                            section: "new",
                                                            sectiontitle: "回退留言",
                                                            text: `您好,您在${title}编辑的内容不符合方针与指引,现在已经被回退。巡查员留言:${commentContent} ${unescape("%7E%7E%7E%7E")}`,
                                                            summary: "PatrolPro巡查自动留言",
                                                            token: csrfToken
                                                        })
                                                        .done(() => {})
                                                        .fail((e) => {
                                                            onError(e);
                                                        });
                                                })
                                                .fail((e) => {
                                                    onError(e);
                                                });
                                        }
                                    }
                                })
                                .fail((e) => {
                                    onError(e);
                                });
                        })
                        .fail((e) => {
                            onError(e);
                        });
                })
                .fail((e) => {
                    onError(e);
                });
        }

        function PatrolDialog(config) {
            PatrolDialog.super.call(this, config);
            this.config = config;
        }

        OO.inheritClass(PatrolDialog, OO.ui.ProcessDialog);

        PatrolDialog.static.name = "patrolDialog";
        PatrolDialog.static.title = "巡查";
        PatrolDialog.static.actions = [{
                action: "confirm",
                label: "确认",
                flags: ["primary", "progressive"]
            },
            {
                action: "rollback1",
                label: "回退",
                flags: ["primary", "destructive"]
            },
            {
                action: "rollback2",
                label: "回退(不评论)",
                flags: ["destructive"]
            },
            {
                action: "cancel",
                label: "取消",
                flags: ["close"]
            }
        ];

        PatrolDialog.prototype.getBodyHeight = function() {
            return 450;
        };

        PatrolDialog.prototype.initialize = function() {
            PatrolDialog.super.prototype.initialize.call(this, arguments);
            this.talkPage1c = new OO.ui.CheckboxInputWidget({
                icon: "edit"
            });
            this.talkPage1l = new OO.ui.LabelWidget({
                label: "在本页面讨论页留言"
            });
            this.talkPage1a = new OO.ui.ActionFieldLayout(this.talkPage1l, this.talkPage1c);
            this.talkPage2c = new OO.ui.CheckboxInputWidget({
                icon: "edit",
                selected: true
            });
            this.talkPage2l = new OO.ui.LabelWidget({
                label: "在作者讨论页留言"
            });
            this.talkPage2a = new OO.ui.ActionFieldLayout(this.talkPage2l, this.talkPage2c);
            this.notice1 = new OO.ui.LabelWidget({
                label: "请巡查此页面的编辑"
            });
            this.notice2 = new OO.ui.LabelWidget({
                label: "建议在回退时编写留言给作者"
            });
            this.commentBox = new OO.ui.MultilineTextInputWidget({
                autosize: true,
                placeholder: "编写留言",
                maxRows: 8,
                minRows: 0
            });
            this.noticePanel = new OO.ui.PanelLayout({
                framed: false,
                padded: true
            });
            this.configPanel = new OO.ui.PanelLayout({
                framed: false,
                padded: true
            });
            this.noticePanel.$element.append(this.notice1.$element, this.notice2.$element);
            this.configPanel.$element.append(this.commentBox.$element, this.talkPage1a.$element, this.talkPage2a.$element);
            this.page = new OO.ui.PageLayout();
            this.page.$element.append(this.noticePanel.$element, this.configPanel.$element);
            this.$body.append(this.page.$element);
        };

        PatrolDialog.prototype.getSetupProcess = function(data) {
            return PatrolDialog.super.prototype.getSetupProcess.call(this, data);
        };

        PatrolDialog.prototype.getActionProcess = function(action) {
            return PatrolDialog.super.prototype.getActionProcess.call(this, action)
                .next(() => {
                    let closing;
                    if (action === "cancel") {
                        closing = this.close({
                            action: action
                        });
                        return closing;
                    } else if (action === "confirm") {
                        patrol();
                        closing = this.close({
                            action: action
                        });
                        return closing;
                    } else if (action === "rollback1") {
                        if (!this.talkPage1c.isSelected() && !this.talkPage2c.isSelected()) {
                            return new OO.ui.Error("至少应向一个页面发布留言");
                        }
                        if (this.commentBox.getValue().length === 0) {
                            return new OO.ui.Error("请编写留言");
                        }
                        rollback(true, this.talkPage1c.isSelected(), this.talkPage2c.isSelected(), this.commentBox.getValue());
                        closing = this.close({
                            action: action
                        });
                        return closing;
                    } else if (action === "rollback2") {
                        rollback(false);
                        closing = this.close({
                            action: action
                        });
                        return closing;
                    }
                });
        };

        if (mw.config.get("wgNamespaceNumber") >= 0) {
            let html = `<li id="ca-patrolpro" class="vector-tab-noicon mw-list-item"><a href="javascript:patrolpro();"><span>高级巡查</span></a></li><style>.pp-msg{z-index: 9999; right: 1cm; bottom: 1cm; position: fixed; max-width: 480px;}</style>`;
            $(".vector-menu-content-list").append(html);
            window.patrolpro = () => {
                let wm = new OO.ui.WindowManager();
                let dialog = new PatrolDialog();
                $(document.body).append(wm.$element);
                wm.addWindows([dialog]);
                wm.openWindow(dialog);
            };
        }
    }

    mw.loader.enqueue(["mediawiki.api", "mediawiki.page.ready"], ready);
})();