User:SolidBlock/rcpatrol.js

来自滚动的天空Wiki

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
/**
 * 
 * 搬运自英文维基百科,有修改
 * 关于本脚本的介绍:https://en.wikipedia.org/wiki/User:Awesome_Aasim/rcpatrol
 * 
* MIT Licensed - see https://github.com/Awesome-Aasim/WikiRCPatrol/blob/master/LICENSE
* 
* Copyright (c) 2020 Awesome Aasim and contributors
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.

* This script is a work in progress.  Your help in developing this tool is welcomed at https://github.com/Awesome-Aasim/WikiRCPatrol.
* Contributions and changes to this script should be made at the GitHub repository above.
* All other changes will be lost if this file is rebuilt and saved.
* By contributing to this project, you agree to release your work under the MIT license.
**/
// <nowiki>

mw.loader.using(['oojs-ui-core', 'oojs-ui.styles.icons-editing-core', 'oojs-ui.styles.icons-movement', 'oojs-ui.styles.icons-interactions', 'oojs-ui.styles.icons-layout', 'oojs-ui.styles.icons-alerts'], function () {
    if (!window.rcpatrol) { // stops multiple instances of RC patrol from running
        //necessary resources
        var rcpatrol = {};
        window.rcpatrol = rcpatrol;
        if (mw.config.get("wgPageName").toLowerCase() == "Special:最近更改".toLowerCase()) {
            rcpatrol = true;
            $(document).ready(function () {
                var rcpatrollocation = mw.config.get("wgArticlePath").replace("$1", "Special:空白页面/RCPatrol");
                $("#mw-content-text").prepend('<a href="' + rcpatrollocation + '">最近更改巡查</a>(<a href="' + rcpatrollocation + '?unpatrolledonly=1">仅显示未巡查编辑</a>)');
            });
        }
        if ((mw.config.get("wgPageName").toLowerCase() == "Special:空白页面/RCPatrol".toLowerCase())) {
            /**
             * Initialize variables related to RC patrol
             */
            rcpatrol.changes = [];
            rcpatrol.currentChange = 0;
            window.setInterval(() => {
                $("#rcpatroldiff").find("a").attr("target", "_blank");
                $(".mw-rollback-link").hide();
                $(".ve-init-mw-diffPage-diffMode").hide();
                $(".mw-revslider-container").hide();
                if (rcpatrol.currentChange == 0) {
                    rcpatrol.previouseditbutton.setDisabled(true);
                } else if (!rcpatrol.isDisabled) {
                    rcpatrol.previouseditbutton.setDisabled(false);
                }
            }, 100);
            $(document).ready(function () {
                rcpatrol.fetch();
                $("#firstHeading, #section_0").html("最近更改巡查");
                $("title").text("最近更改巡查 - " + mw.config.get("wgSiteName"));
                /*
                if (mw.config.get("skin") == "minerva") {
                    $("body").html($("main").html());
                    $("#siteNotice").prepend('<a id="rcpatrolexit" href="/">Exit</a>');
                    $("#rcpatrolexit").click(function () {
                        window.history.back();
                    })
                }
                */
                $("#mw-content-text").html("");
                $("#mw-content-text").append('<div id="rcpatrolbuttons"></div>');
                $("#mw-content-text").append('<div id="rcpatroldiff"></div>');
                $("#rcpatrolbuttons").prepend(rcpatrol.rcpatrolbar.$element);
                $("#rcpatrolbuttons").prepend(rcpatrol.rollbackbar.$element);
                $("#rcpatrolbuttons").prepend(rcpatrol.dropdownmenu.$element);
                $("#rcpatrolbuttons").prepend('<a href="/wiki/Special:空白页面/RCPatrol?unpatrolledonly=1">仅显示没有巡查的编辑</a><br>');
                if (mw.config.get('wgUserGroups').includes('sysop')) {
                    $("#rcpatrolbuttons").append('管理员工具:<span id="rcpatroladmintools"></span>');
                }
                $("#rcpatrolbuttons").append('页面工具:<span id="rcpatrolpagetools"></span>');
                $("#rcpatroldiff").css({
                    overflow: "auto"
                });
            });
        }
    }
    /**
     * Internationalization here
     */
    rcpatrol.i18n = {
        reasontorollback: {
            en: 'Reason to rollback (optional)',
            zh: '回退原因(可选)'
        },
        rollback: {
            en: 'Rollback',
            zh: '回退'
        },
        rollingback: {
            en: 'Rolling back...',
            zh: '正在回退'
        },
        rollbackfailed: {
            en: 'Rollback failed',
            zh: '回退失败'
        },
        rollbacksuccess: {
            en: 'Rollback complete',
            zh: '回退完成'
        },
        rollbacksummary: {
            en: 'RCP reverted edits by [[Special:Contributions/$2|$2]] ([[User_talk:$2|Talk]]); changed back to last revision by [[Special:Contributions/$1|$1]]',
            zh: 'RCP由[[Special:贡献/$2|$2]] ([[User_talk:$2|讨论]])回退的编辑;改变为[[Special:Contributions/$1|$1]]的最后版本'
        },
        warnsummary: {
            en: 'RCP send warning to $1 about [[$2]]',
            zh: 'RCP向$1发送关于[[$2]]的警告'
        },
        reportsummary: {
            en: 'RCP report $1',
            zh: 'RCP报告$1'
        },
        rollbacktitle: {
            en: "Revert this user's edits",
            zh: '回退该用户的编辑'
        },
        previousedit: {
            en: 'Previous edit',
            zh: '先前的编辑'
        },
        previousedittitle: {
            en: 'Load the previous edit in the batch',
            zh: '加载批次中的先前编辑'
        },
        nextedit: {
            en: 'Next edit',
            zh: '下一个编辑'
        },
        nextedittitle: {
            en: 'Load the next edit in the batch',
            zh: '加载批次中的下一个编辑'
        },
        refresh: {
            en: 'Refresh',
            zh: '刷新'
        },
        connectionlost: {
            en: 'Lost connection',
            zh: '失去连接'
        },
        connectionlostdiffmessage: {
            en: "无法加载差异。请检查网络连接。连接恢复时自动重新加载差异。"
        },
        thank: {
            en: 'Thank',
            zh: '感谢'
        },
        thanktitle: {
            en: "Thank this user for their edits",
            zh: '感谢此用户的编辑'
        },
        rollbackandwarn: {
            en: 'Rollback and warn',
            zh: '回退并警告'
        },
        endoflist: {
            en: 'Reached end of list.  Loading next batch...',
            zh: '到达列表末尾。正在加载下一批次……'
        },
        thankssent: {
            en: 'Thanks sent!',
            zh: '已发送感谢!'
        },
        rcpatroltitle: {
            en: 'Recent Changes Patrol',
            zh: '最近更改巡查'
        },
        rcpatroltitlewithdiff: {
            en: 'Recent Changes Patrol "$1"',
            zh: '最近更改巡查“$1”'
        },
        delete: {
            en: 'Delete',
            zh: '删除'
        },
        protect: {
            en: 'Protect',
            zh: '保护'
        },
        block: {
            en: 'Block poster',
            zh: '封禁发送者'
        },
        history: {
            en: 'View page history',
            zh: '查看页面历史'
        },
        diff: {
            en: 'View diff',
            zh: '查看差异'
        }
    };
    /**
     * End of internationaliztaion.  DO NOT EDIT BELOW THIS LINE
     */
    rcpatrol.msgs = {};
    for (var i in rcpatrol.i18n) {
        rcpatrol.msgs[i] = rcpatrol.i18n[mw.config.get("wgUserLanguage")] ? rcpatrol.i18n[mw.config.get("wgUserLanguage")] : rcpatrol.i18n.zh; //always fall back to Chinese if the message translation is incomplete
    }
    /**
     * Configuration for English Wikipedia(不适用于滚动的天空Wiki)
     * Eventually this will be stored elsewhere on a separate page, maybe at [[Project:RC Patrol Script/config.js]]
     * The idea is that any particular modules that need to be disabled or reconfigured can be done so on a wiki by wiki basis by simply "reprogramming" it here.
     * The configuration file should be locked so that only administrators can edit it.
     */
    rcpatrol.reportpage = "Wikipedia:Administrator intervention against vandalism"; //the page that users should be reported on
    rcpatrol.reportstring = "\n\n* {{vandal|1=$1}} - $2"; //the string pattern to use for the report
    rcpatrol.dropdown = [
        {
            keycode: 84, //t
            val: "Test edit",
            summary: "Test edit",
            template: "uw-test"
        },
        {
            keycode: 69, //e
            val: "Disruptive edit",
            summary: "[[WP:DE|Disruptive edit]]",
            template: "uw-disruptive"
        },
        {
            keycode: 77, //m
            val: "Manual of style violation",
            summary: "Violates [[WP:MOS|manual of style]]",
            template: "uw-mos"
        },
        {
            keycode: 65, //a
            val: "Personal attack",
            summary: "[[WP:NPA|Personal attack]]",
            template: "uw-npa"
        },
        {
            keycode: 76, //l
            val: "BLP violation",
            summary: "Violation of the [[WP:BLP|biographies of living people policy]]",
            template: "uw-biog"
        },
        {
            keycode: 78, //n
            val: "Neutral point of view violation",
            summary: "Violates [[WP:NPOV|neutral point of view]]",
            template: "uw-npov"
        },
        {
            keycode: 85, //u
            val: "Unsourced",
            summary: "[[WP:UNSOURCED|Unsourced]]",
            template: "uw-unsourced"
        },
        {
            keycode: 68, //d
            val: "Unexplained content removal",
            summary: "Unexplained removal of content",
            template: "uw-delete"
        },
        {
            keycode: 66, //b
            val: "Page blanking",
            summary: "Blanking the page",
            template: "uw-blank"
        },
        {
            keycode: 86, //v
            val: "Vandalism",
            summary: "Unconstructive edit",
            template: "uw-vandalism"
        },
        {
            keycode: 83, //s
            val: "Link spam",
            summary: "Inappropriate external link",
            template: "uw-spam"
        },
        {
            keycode: 80, //p
            val: "Advertising",
            summary: "Promotional language in article",
            template: "uw-advert"
        }
    ];
    /**
     * Load all OOUI items, including buttons, etc.
     */

    rcpatrol.rcpatrolbar = new OO.ui.HorizontalLayout({ align: 'inline' });
    rcpatrol.rcpatrolbox = new OO.ui.TextInputWidget({
        autosize: true,
        placeholder: '回退原因(可选)',
        icon: 'textSummary',
        align: 'inline'
    });
    rcpatrol.rcpatrolbutton = new OO.ui.ButtonWidget({
        autosize: true,
        label: '回退',
        flags: [
            'primary',
            'progressive'
        ],
        icon: "editUndo",
        align: 'inline'
    });
    rcpatrol.rollbackbar = new OO.ui.ActionFieldLayout(rcpatrol.rcpatrolbox, rcpatrol.rcpatrolbutton, { align: "inline" });
    rcpatrol.previouseditbutton = new OO.ui.ButtonWidget({
        autosize: true,
        label: '上一个编辑',
        icon: "previous",
        align: 'inline'
    });
    rcpatrol.previouseditbutton.$description = $("<div></div>").css("font-weight","normal").css("font-size","smaller").appendTo(rcpatrol.previouseditbutton.$label);

    rcpatrol.nexteditbutton = new OO.ui.ButtonWidget({
        autosize: true,
        label: '下一个编辑',
        icon: "next",
        align: 'inline'
    });
    rcpatrol.nexteditbutton.$description = $("<div></div>").css("font-weight","normal").css("font-size","smaller").appendTo(rcpatrol.nexteditbutton.$label);

    rcpatrol.fetchbutton = new OO.ui.ButtonWidget({
        autosize: true,
        label: '刷新',
        icon: "reload",
        align: 'inline'
    });
    rcpatrol.thankbutton = new OO.ui.ButtonWidget({
        autosize: true,
        label: '感谢',
        icon: "heart",
        align: 'inline'
    });
    rcpatrol.patrolbutton = new OO.ui.ButtonWidget({
        autosize: true,
        label: '标记为已巡查',
        icon: 'check',
        align: 'inline'
    });
    rcpatrol.patrolcheckbox = new OO.ui.CheckboxInputWidget({
        value: 'jumptonext'
    });
    rcpatrol.dropdownmenu = new OO.ui.DropdownWidget({
        label: "回退并警告……",
        icon: "speechBubbleAdd",
        menu: {
            items: []
        }
    });
    rcpatrol.rcpatrolbar.addItems([
        rcpatrol.previouseditbutton,
        rcpatrol.nexteditbutton,
        rcpatrol.fetchbutton,
        rcpatrol.thankbutton,
        rcpatrol.patrolbutton,
        new OO.ui.FieldLayout(rcpatrol.patrolcheckbox, {'label': '巡查完成后跳入下一编辑','align':'inline'})
    ]);
    rcpatrol.rcpatrolbutton.$element.attr("title", "回退此用户的编辑 [ctrl-alt R]");
    rcpatrol.thankbutton.$element.attr("title", "感谢此用户的编辑 [ctrl-alt =]");
    rcpatrol.patrolbutton.$element.attr("title", "将此用户的编辑标记为已巡查");
    rcpatrol.nexteditbutton.$element.attr("title", "加载批次中的下一个编辑 [ctrl-alt space]");
    rcpatrol.previouseditbutton.$element.attr("title", "加载批次中的上一个编辑 [ctrl-alt ,]");
    for (var j in rcpatrol.dropdown) {
        var temp = new OO.ui.MenuOptionWidget({
            data: rcpatrol.dropdown[j].keycode,
            label: rcpatrol.dropdown[j].val
        });
        temp.$element.attr("title", rcpatrol.dropdown[j].val + (String.fromCharCode(rcpatrol.dropdown[j].keycode) ? " [ctrl-alt " + String.fromCharCode(rcpatrol.dropdown[j].keycode) + "]" : ""));
        rcpatrol.dropdownmenu.getMenu().addItems([temp]);
    }
    /**
     * Disables/enables the RC patrol controls
     * @param {*} bool whether to disable the controls or not
     */
    rcpatrol.setDisabled = function (bool) {
        rcpatrol.isDisabled = bool;
        rcpatrol.dropdownmenu.setDisabled(bool);
        rcpatrol.previouseditbutton.setDisabled(bool);
        rcpatrol.nexteditbutton.setDisabled(bool);
        rcpatrol.rcpatrolbutton.setDisabled(bool);
        rcpatrol.rcpatrolbox.setDisabled(bool);
        rcpatrol.thankbutton.setDisabled(bool);
        rcpatrol.patrolbutton.setDisabled(bool);
        rcpatrol.patrolbutton.setLabel('标记为已巡查');
        rcpatrol.fetchbutton.setDisabled(bool);
    };
    /**
     * Fetches a list of recent changes and loads it onto RC patrol
     */
    rcpatrol.fetch = function () {
        if (!rcpatrol.fetchbutton.isDisabled()) {
            $("#rcpatroldiff").fadeOut();
            rcpatrol.setDisabled(true);
            var unpatrolledonly = (new URL(window.location.href)).searchParams.get("unpatrolledonly") || undefined;
            var rcuser = (new URL(window.location.href)).searchParams.get("rcuser") || undefined;
            $.get(mw.config.get("wgScriptPath") + "/api.php", {
                "action": "query",
                "format": "json",
                "list": "recentchanges",
                "rcprop": "title|timestamp|flags|loginfo|parsedcomment|patrolled|user|ids|tags",
                "rcshow": "!bot" + /* ((new URL(window.location.href)).searchParams.get("oresreview") ? "|oresreview" : "") + */(unpatrolledonly ? "|unpatrolled" : ""),
                "rctoponly": undefined,
                "rcuser": rcuser,
                "rclimit": "max",
                //"rctype": "edit|new",
                "uselang": mw.config.get("wgUserLanguage")
            }).done(function (result) {
                rcpatrol.changes = result.query.recentchanges;
                console.log(result.query.recentchanges);
                rcpatrol.setDisabled(false);
                rcpatrol.currentChange = 0;
                rcpatrol.previousChange = -1;
                rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
            }).fail(function () {
                window.setTimeout(rcpatrol.fetch, 2500);
            });
        }
    };

    /**
     * 探测合并的编辑,并检查下个非合并更改的id。
     * @param resentchanges 最近更改列表
     * @param currentChangeIndex 当前更改在列表中的索引
     */
    rcpatrol.getrevid = function (recentchanges, currentChangeIndex) {
        currentChangeIndex = currentChangeIndex || rcpatrol.currentChange;
        var currentChangeObject = recentchanges[currentChangeIndex];
        var nextChangeIndex = currentChangeIndex + 1; // 尝试找到下一个不合并的编辑
        var mergedIndexList = currentChangeObject ? [currentChangeIndex] : []; // 如果没有可用编辑了,则mergedIndexList为空
        var nextChangeObject = recentchanges[nextChangeIndex];
        var middle_revid = currentChangeObject!=undefined ? currentChangeObject.old_revid : undefined; // 用于比较old_revid和revid的衔接性,不影响currentChangeObject.old_revid
        for (var i = 0; i < 50; i++) {
            if (!nextChangeObject) {
                console.log('break! nextChangeObject = ');
                break;
            }
            if (nextChangeObject!=undefined && nextChangeObject.revid == middle_revid) { // 注意:越是next的编辑越old
                // 如果下个编辑正好与此编辑衔接
                mergedIndexList.push(nextChangeIndex);
                middle_revid = nextChangeObject.old_revid;
                nextChangeIndex++;
                nextChangeObject = recentchanges[nextChangeIndex];
            } else break;
        }
        rcpatrol.nextChange = nextChangeIndex;
        rcpatrol.mergedIndexList = mergedIndexList;
        console.log('mergedIndexList:', mergedIndexList);
    };

    /**
     * Loads a change and places it in the RC patrol diff output.
     * @param {*} change the diff of the change to load
     */
    rcpatrol.loadChange = function (change) {
        $("#rcpatroldiff").fadeOut();
        $("#rcpatroladmintools").fadeOut();
        $("#rcpatrolpagetools").fadeOut();
        rcpatrol.setDisabled(true);
        // $.get(mw.config.get("wgScriptPath") + "/api.php", {
        //     "action": "query",
        //     "format": "json",
        //     "prop": "revisions",
        //     "titles": change.title,
        //     "rvlimit": "1",
        //     "uselang": mw.config.get("wgUserLanguage")
        // }).done(function (result) {
        //     for (var pageid in result.query.pages) {
        //         change.revid = result.query.pages[pageid].revisions[0].revid;
        //         change.user = result.query.pages[pageid].revisions[0].user;
        //         break;
        //     }
        //     $.get(mw.config.get("wgScriptPath") + "/api.php", {
        //         "action": "query",
        //         "format": "json",
        //         "prop": "revisions",
        //         "titles": change.title,
        //         "rvexcludeuser": change.user,
        //         "rvlimit": "1",
        //         "uselang": mw.config.get("wgUserLanguage")
        //     }).done(function (result) {
        var oldid, revid;
        // if (unpatrolledonly)
        rcpatrol.getrevid(rcpatrol.changes);
        var mergedIndexList = rcpatrol.mergedIndexList;
        if (!mergedIndexList.length) {
        	mw.notify("没有需要查看的编辑了!");
        	return;
        }
        oldid = rcpatrol.changes[mergedIndexList[mergedIndexList.length - 1]].old_revid;
        revid = rcpatrol.changes[mergedIndexList[0]].revid;
        // else
        // {
        //     try {
        //         for (var pageid in result.query.pages) {
        //             oldid = result.query.pages[pageid].revisions[0].revid;
        //             break;
        //         }
        //     } catch (Error) {
        //         var temp = oldid;
        //         oldid = change.revid;
        //         change.revid = "";
        // }
        // }

        console.log(mw.config.get("wgScriptPath") + "/index.php?oldid=" + oldid + "&diff=" + revid);
        var scriptpath = mw.config.get('wgScriptPath');
        var loadurl = mw.config.get("wgScriptPath") + "/index.php?oldid=" + oldid + (revid ? "&diff=" + revid : "");
        if (mw.config.get("wgMFMode")) {
            loadurl = mw.config.get("wgArticlePath").replace("$1", "Special:移动版差异/" + oldid + (revid ? "..." + revid : ""));
        }
        $.get(loadurl, {
            safemode: "1",
            uselang: mw.config.get("wgUserLanguage"),
            useskin: mw.config.get("skin"),
            useskinversion: "2"
        }).done(function (result) {
            var element = document.createElement('html');
            element.innerHTML = result;

            // 给“上一个编辑”和“下一个编辑”添加注释
            var previous = rcpatrol.changes[rcpatrol.previousChange];
            if (previous !== undefined) {
                rcpatrol.previouseditbutton.$description.text(previous.user + "," + previous.title);
            } else rcpatrol.previouseditbutton.$description.text("");
            var next = rcpatrol.changes[rcpatrol.nextChange];
            if (next !== undefined) {
                rcpatrol.nexteditbutton.$description.text(next.user + "," + next.title);
            } else rcpatrol.nexteditbutton.$description.text("");

			var mwContentText = element.querySelector("#mw-content-text");
			if (element.getElementsByTagName('body')[0].classList.contains('ns-subject')) {
				var mwParserOutput = mwContentText.getElementsByClassName('mw-parser-output');
				if (mwParserOutput.length > 0) mwParserOutput[0].classList.add('hantsect');
			}
            $("#rcpatroldiff").html(mwContentText);
            if (mw.config.get("wgMFMode")) {
                mw.loader.load("mobile.special.mobilediff.styles");
            } else {
                mw.loader.load("mediawiki.diff.styles");
            }
            if (mwContentText.getElementsByClassName('gallery').length != 0) {
            	mw.loader.load('mediawiki.page.gallery.styles'); // 正常加载与gallery有关的样式
            }

            $("#firstHeading, #section_0").html('最近更改巡查 “<a target=\"_blank\" href=\"' + scriptpath + '/index.php?title=' + change.title + '\">' + change.title + "</a>”");
            $("title").text("最近更改巡查 “" + change.title + "” - " + mw.config.get("wgSiteName"));

            $("#rcpatroldiff").fadeIn();
            $("#rcpatroladmintools").fadeIn();
            $("#rcpatrolpagetools").fadeIn();
            rcpatrol.rcpatrolbox.setValue("");
            rcpatrol.rcpatrolbutton.setLabel("回退");
            var unpatrolled = change.unpatrolled == "";
            var autopatrolled = change.autopatrolled == "";
            rcpatrol.setDisabled(false);

            // 配置巡查按钮
            var mergedIndexList = rcpatrol.mergedIndexList;
            var [patrolledChanges, unpatrolledChanges, autopatrolledChanges] = [[], [], []];
            for (var index of mergedIndexList) {
                var c = rcpatrol.changes[index];
                if (c.patrolled == "") patrolledChanges.push(change);
                if (c.unpatrolled == "") unpatrolledChanges.push(change);
                if (c.autopatrolled == "") autopatrolledChanges.push(change);
            }
            if (unpatrolledChanges.length) {
                // 还有未巡查编辑的情况
                rcpatrol.patrolbutton.setLabel(unpatrolledChanges.length == 1 ? "标记为已巡查" : "将未巡查的" + unpatrolledChanges.length + "个版本标记为已巡查");
            } else {
                // 编辑都已巡查的情况
                rcpatrol.patrolbutton.setDisabled(true);
                if (autopatrolledChanges.length >= patrolledChanges.length) {
                    rcpatrol.patrolbutton.setLabel(patrolledChanges.length == 1 ? "已自动巡查" : "" + autopatrolledChanges.length + "个版本已自动巡查");
                } else {
                    rcpatrol.patrolbutton.setLabel(patrolledChanges.length == 1 ? "已巡查" : "" + patrolledChanges.length + "个版本已巡查");
                }
            }

            $("#rcpatroladmintools").html('');
            $("#rcpatroladmintools").append('<a target="_blank" href="' + scriptpath + '/index.php?title=' + change.title + '&action=delete">删除</a>');
            $("#rcpatroladmintools").append(' &bull; ');
            $("#rcpatroladmintools").append('<a target="_blank" href="' + scriptpath + '/index.php?title=' + change.title + '&action=protect">保护</a>');
            $("#rcpatroladmintools").append(' &bull; ');
            $("#rcpatroladmintools").append('<a target="_blank" href="' + scriptpath + '/index.php?title=Special:Block/' + change.user + '">封禁发送者</a>');
            $("#rcpatrolpagetools").html('');
            $("#rcpatrolpagetools").append('<a target="_blank" href="' + scriptpath + '/index.php?title=' + change.title + '&action=history">查看页面历史</a>');
            $("#rcpatrolpagetools").append(' &bull; ');
            $("#rcpatrolpagetools").append('<a target="_blank" href="' + scriptpath + '/index.php?oldid=' + oldid + '&diff=' + change.revid + '">查看差异</a>');
            
            var autopatrol = (new URL(window.location.href)).searchParams.get("autopatrol") || undefined;
            if (autopatrol) setTimeout(function (){rcpatrol.patrolbutton.$element.click();},1000);
            
        }).fail(function () {
            $("#rcpatroldiff").fadeIn(1000);
            $("#rcpatroldiff").text("无法加载差异。请检查网络连接。连接恢复时自动重新加载差异。");
            window.setTimeout(function () {
                rcpatrol.loadChange(change);
            }, 1000);
        });
    }
    /*
    $("#rcpatroldiff").load(loadurl, function (response, status, xhr) {
        if (status == "error") {
        } else {
            $("#rcpatroldiff").find("form").hide();
            $("#rcpatroldiff").find("#firstHeading").hide();
        }
    });
    */
    //         }).fail(function (result) {
    //             $("#rcpatroldiff").fadeIn(1000);
    //             $("#rcpatroldiff").text("无法加载差异。请检查网络连接。连接恢复时自动重新加载差异。");
    //             window.setTimeout(function () {
    //                 rcpatrol.loadChange(change)
    //             }, 1000);
    //         });
    //     }).fail(function (result) {
    //         $("#rcpatroldiff").fadeIn(1000);
    //         $("#rcpatroldiff").text("无法加载差异。请检查网络连接。连接恢复时自动重新加载差异。");
    //         window.setTimeout(function () {
    //             rcpatrol.loadChange(change)
    //         }, 1000);
    //     });
    // };
    /**
     * Rolls back edits on diffs
     * @param {*} page the page to revert the edits on
     * @param {*} user the user to revert
     * @param {*} afterSuccess what to do after the revert is successful
     * @param {*} afterFail what to do if the revert fails
     */
    rcpatrol.revert = function (page, user, afterSuccess, afterFail) {
        if (!rcpatrol.rcpatrolbutton.isDisabled()) {
            var summary = rcpatrol.rcpatrolbox.getValue();
            rcpatrol.rcpatrolbutton.setLabel("正在回退……");
            rcpatrol.setDisabled(true);
            $.get(mw.config.get("wgScriptPath") + "/api.php", {
                "action": "query",
                "format": "json",
                "meta": "tokens",
                "type": "rollback"
            }).done(function (result) {
                if (result.error) {
                    rcpatrol.rcpatrolbutton.setLabel("回退失败");
                    alert(result.error.info);
                    afterFail();
                } else if (result.query) {
                    $.post(mw.config.get("wgScriptPath") + "/api.php", {
                        "action": "rollback",
                        "format": "json",
                        "title": page,
                        "token": result.query.tokens.rollbacktoken,
                        "user": user,
                        "summary": "RCP回退的编辑,来自[[Special:Contributions/$2|$2]] ([[User talk:$2|讨论]]),退回到[[Special:Contributions/$1|$1]]的最后一个版本" + (summary ? ": " + summary : "")
                    }).done(function (result) {
                        if (result.error) {
                            rcpatrol.rcpatrolbutton.setLabel("回退失败");
                            alert(result.error.info);
                            afterFail();
                        } else {
                            rcpatrol.rcpatrolbutton.setLabel("回退完成");
                            afterSuccess();
                        }
                    }).fail(function () {
                        console.log(result);
                        alert("失去连接。");
                        rcpatrol.rcpatrolbutton.setLabel("回退失败");
                        afterFail();
                    });
                } else console.log('result query is ',result.query);
            });
        }
    };
    /**
     * Mark the edit as patrolled
     **/
    rcpatrol.patrolbutton.$element.click(function (e) {
        rcpatrol.patrolbutton.setLabel('正在标记为已巡查');
        if (!rcpatrol.patrolbutton.isDisabled()) {
            e.preventDefault();
            rcpatrol.patrolbutton.setDisabled(true);

            var successCount = 0;
            var failureCount = 0;
            var length = rcpatrol.mergedIndexList.length;
            var patrolAtN;
            patrolAtN = function (N) { // N用于迭代
                var index = rcpatrol.mergedIndexList[N];
                var change = rcpatrol.changes[index];
                $.get(mw.config.get("wgScriptPath") + "/api.php", {
                    "action": "query",
                    "format": "json",
                    "meta": "tokens",
                    "type": "patrol"
                }).done(function (result) {
                	console.log(result);
                	if (result.error) {
	                    rcpatrol.rcpatrolbutton.setLabel("标记巡查失败");
	                    mw.notify(result.error.info);
	                    console.log('巡查token获取失败:',result.error.info);
	                    afterFail();
            		} else {
	                    $.post(mw.config.get("wgScriptPath") + "/api.php", {
	                        "action": "patrol",
	                        "format": "json",
	                        "rcid": change.rcid,
	                        "token": result.query.tokens.patroltoken,
	                        "formatversion": 2
	                    }).done(function (result) {
	                        if (result.error) {
	                            mw.notify(result.error.info);
	                            failureCount++;
	                        } else {
	                            successCount++;
	                            console.log("rcid为" + change.rcid + "的版本已标记为已巡查!");
	                            // rcpatrol.patrolbutton.setLabel('已标记为已巡查');
	                            change.unpatrolled = undefined;
	                            change.patrolled = "";
	                        }
	                        if (length > 1) rcpatrol.patrolbutton.setLabel("" + successCount + "/" + length + "个版本已标记为已巡查");
	
	                        if (N >= length - 1 || N > 30) {
	                            // 一系列页面都已经巡查完成
	                            console.log("本次批量巡查结束,共成功" + successCount + "次,失败" + failureCount + "次");
	                            if (failureCount) {
	                                // 有巡查失败的情况
	                                rcpatrol.patrolbutton.setLabel(successCount ? "将未成功标记为已巡查的" + failureCount + "个版本标记为已巡查" : failureCount == 1 ? "标记为已巡查" : "重新将" + failureCount + "个版本标记为已巡查");
	                                rcpatrol.patrolbutton.setDisabled(false);
	                            } else {
	                                // 均已巡查的情况
	                                mw.notify("" + successCount + "个版本已标记为已巡查!");
	                                rcpatrol.patrolbutton.setLabel(successCount > 1 ? "" + successCount + "个版本已标记为已巡查" : "已标记为已巡查");
	
	                                if (rcpatrol.patrolcheckbox.isSelected()) rcpatrol.nexteditbutton.$element.click();
	                            }
	                        } else {
	                            // 开始巡查该系列中的下一个页面
	                            patrolAtN(N + 1);
	                        }
	                    });
            		}
                });
            };
            patrolAtN(0);
        }
    });
    /**
     * Warns a user with a specified warning template
     * @param {*} user the user to warn
     * @param {*} template the template prefix to use as a warning
     * @param {*} page the relevant page
     */
    rcpatrol.warn = function (user, template, page) {
        var date = new Date();
        var months = mw.config.get("wgMonthNames");
        var currentMonth = months[date.getUTCMonth() + 1];
        var year = date.getUTCFullYear();
        var header = currentMonth + " " + year;
        $.get(mw.config.get("wgScriptPath") + "/api.php", {
            "action": "query",
            "format": "json",
            "meta": "tokens",
            "type": "csrf",
            "uselang": mw.config.get("wgUserLanguage")
        }).done(function (result) {
            var token = result.query.tokens.csrftoken;
            $.get(mw.config.get("wgScriptPath") + "/api.php", {
                "action": "parse",
                "format": "json",
                "prop": "text",
                "page": "User_talk:" + user,
                "uselang": mw.config.get("wgUserLanguage")
            }).done(function (result) {
                if (result.error) {
                    if (result.error.code == "missingtitle") {
                        $.post(mw.config.get("wgScriptPath") + "/api.php", {
                            "action": "edit",
                            "section": "new",
                            "sectiontitle": header,
                            "format": "json",
                            "title": "User_talk:" + user,
                            "text": "{{subst:" + template + "1|1=" + page + "}} ~~" + "~~",
                            "summary": "RCP send warning to " + user + " about [[" + page + "]]",
                            "token": token,
                            "uselang": mw.config.get("wgUserLanguage")
                        }).done(function (result) {
                            if (result.error) {
                                mw.notify("无法向用户" + user + "发送警告。");
                            } else {
                                mw.notify("警告已自动发送给" + user + "。");
                            }
                        });
                    }
                } else {
                    var section = "new";
                    $(result.parse.text["*"]).find(".mw-headline").each(function (i) {
                        if ($(this).text() == header) {
                            section = i + 1;
                        }
                    });
                    if (section == "new") {
                        $.post(mw.config.get("wgScriptPath") + "/api.php", {
                            "action": "edit",
                            "section": "new",
                            "sectiontitle": header,
                            "format": "json",
                            "title": "User_talk:" + user,
                            "text": "{{subst:" + template + "1|1=" + page + "}} ~~" + "~~",
                            "summary": "RCP send warning to " + user + " about [[" + page + "]]",
                            "token": token,
                            "uselang": mw.config.get("wgUserLanguage")
                        }).done(function (result) {
                            if (result.error) {
                                mw.notify("无法向" + user + "发送警告。");
                            } else {
                                mw.notify("警告已自动发送给" + user + "。");
                            }
                        });
                    } else {
                        $.get(mw.config.get("wgScriptPath") + "/api.php", {
                            "action": "parse",
                            "section": section,
                            "format": "json",
                            "prop": "wikitext",
                            "page": "User_talk:" + user,
                            "uselang": mw.config.get("wgUserLanguage")
                        }).done(function (result) {
                            if (result.error) {
                                console.error(result.error.info);
                            } else {
                                console.log(result.parse.wikitext["*"]);
                                var warninglevelstrings = result.parse.wikitext["*"].match(/<!--( ){0,}Template:.*(1|2|3|4)(im)?( ){0,}-->/g);
                                console.log(warninglevelstrings);
                                var warninglevels = warninglevelstrings[warninglevelstrings.length - 1].match(/[(1|2|3|4)]/);
                                var warninglevel = parseInt(warninglevels[warninglevels.length - 1]) + 1;
                                var oldtimestamp = (new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()) - 86400000)).toISOString();
                                $.get(mw.config.get("wgScriptPath") + "/api.php", {
                                    "action": "query",
                                    "format": "json",
                                    "prop": "revisions",
                                    "titles": "User_talk:" + user,
                                    "rvsection": section,
                                    "rvend": oldtimestamp,
                                    "uselang": mw.config.get("wgUserLanguage")
                                }).done(function (result) {
                                    var revisions = [];
                                    try {
                                        for (var pageid in result.query.pages) {
                                            revisions = result.query.pages[pageid].revisions;
                                        }
                                    } catch (Error) {

                                    }
                                    if (revisions) {
                                        if (warninglevel > 4) {
                                            rcpatrol.report(user);
                                        } else {
                                            $.post(mw.config.get("wgScriptPath") + "/api.php", {
                                                "action": "edit",
                                                "section": section,
                                                "sectiontitle": header,
                                                "format": "json",
                                                "title": "User_talk:" + user,
                                                "appendtext": "\n\n{{subst:" + template + warninglevel + "|1=" + page + "}} ~~" + "~~",
                                                "summary": "[[User:Awesome Aasim/rcpatrol|RCP]] send warning to " + user + " about [[" + page + "]]",
                                                "token": token,
                                                "uselang": mw.config.get("wgUserLanguage")
                                            }).done(function (result) {
                                                if (result.error) {
                                                    mw.notify("We could not send a warning to " + user + ".");
                                                } else {
                                                    mw.notify("A warning was automatically sent to " + user + ".");
                                                }
                                            });
                                        }
                                    } else {
                                        $.post(mw.config.get("wgScriptPath") + "/api.php", {
                                            "action": "edit",
                                            "section": section,
                                            "sectiontitle": header,
                                            "format": "json",
                                            "title": "User_talk:" + user,
                                            "appendtext": "\n\n{{subst:" + template + "1|1=" + page + "}} ~~" + "~~",
                                            "summary": "RCP send warning to " + user + " about [[" + page + "]]",
                                            "token": token,
                                            "uselang": mw.config.get("wgUserLanguage")
                                        }).done(function (result) {
                                            if (result.error) {
                                                mw.notify("无法向" + user + "发送警告。");
                                            } else {
                                                mw.notify("警告已自动发送给" + user + "。");
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                }
            });
        });
    };
    /**
     * Reports any user to a preconfigured project page
     * If admin, blocks the user for a preset time
     * @param {*} user user to report
     */
    rcpatrol.report = function (user) {

        if (mw.config.get("wgUserGroups").includes("sysop")) {
            //TODO:  get block suggestion for vandals (24 hrs for first block, 72 hrs for second block, 3^(n-1) days for the (nth block))
            //also:  block API
            var blockwindow = window.open(mw.config.get("wgArticlePath").replace("$1", "Special:封禁/" + user));
            blockwindow.alert("The user has received a final warning in the last 24 hours, so this window was opened.  When you are done blocking the user, you can close this tab and go back to RC patrol.");
        } else {
            var reportinfo = prompt("Enter information about the report here: ");
            reportinfo = reportinfo ? reportinfo : "Vandalism after final warning.";
            $.get(mw.config.get("wgScriptPath") + "/api.php", {
                "action": "query",
                "format": "json",
                "meta": "tokens",
                "type": "csrf",
                "uselang": mw.config.get("wgUserLanguage")
            }).done(function (result) {
                $.post(mw.config.get("wgScriptPath") + "/api.php", {
                    "action": "edit",
                    "format": "json",
                    "title": rcpatrol.reportpage,
                    "summary": "[[User:Awesome Aasim/rcpatrol|RCP]] report " + user,
                    "appendtext": rcpatrol.reportstring.replace("$1", user).replace("$2", reportinfo) + " ~~" + "~~",
                    "uselang": mw.config.get("wgUserLanguage")
                }).done(function (result) {
                    if (result.error) {
                        alert(result.error.info);
                    } else {
                        mw.notify("User successfully reported to admins.");
                    }
                });
            });
        }
    };
    /**
     * Handles global events, including clicks, keypresses, etc.
     */
    rcpatrol.fetchbutton.$element.click(rcpatrol.fetch);
    rcpatrol.rcpatrolbutton.$element.click(function (e) {
        rcpatrol.revert(rcpatrol.changes[rcpatrol.currentChange].title, rcpatrol.changes[rcpatrol.currentChange].user, function () {
            rcpatrol.currentChange++;
            rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
        },
            function () {
                rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
            });
    });
    rcpatrol.nexteditbutton.$element.click(function (e) {
        if (!rcpatrol.nexteditbutton.isDisabled()) {
            e.preventDefault();
            // rcpatrol.currentChange++;
            rcpatrol.previousChange = rcpatrol.currentChange;
            rcpatrol.currentChange = rcpatrol.nextChange;
            // rcpatrol.nextChange会在后面的loadChange中调用getrevid以更新
            if (rcpatrol.currentChange >= rcpatrol.changes.length) {
                mw.notify("到达列表底部。加载下一批次……");
                rcpatrol.fetch();
            } else {
                rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
            }
        }
    });
    rcpatrol.previouseditbutton.$element.click(function (e) {
        if (!rcpatrol.previouseditbutton.isDisabled()) {
            e.preventDefault();
            //rcpatrol.currentChange--;
            rcpatrol.nextChange = rcpatrol.currentChange;
            rcpatrol.currentChange = rcpatrol.previousChange;
            if (rcpatrol.currentChange < 0) {
                rcpatrol.currentChange = 0;
            }
            rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
        }
    });
    rcpatrol.thankbutton.$element.click(function (e) {
        if (!rcpatrol.thankbutton.isDisabled()) {
            e.preventDefault();
            rcpatrol.thankbutton.setDisabled(true);
            $.get(mw.config.get("wgScriptPath") + "/api.php", {
                "action": "query",
                "format": "json",
                "meta": "tokens",
                "type": "csrf"
            }).done(function (result) {
                $.post(mw.config.get("wgScriptPath") + "/api.php", {
                    "action": "thank",
                    "format": "json",
                    "rev": rcpatrol.changes[rcpatrol.currentChange].revid,
                    "token": result.query.tokens.csrftoken
                }).done(function (result) {
                    if (result.error) {
                        alert(result.error.info);
                    } else {
                        mw.notify("感谢已发送!");
                    }
                });
            });
        }
    });
    rcpatrol.rcpatrolbox.$element.keypress(function (e) {

        if (e.which == 13) {
            rcpatrol.revert(rcpatrol.changes[rcpatrol.currentChange].title, rcpatrol.changes[rcpatrol.currentChange].user, function () {
                rcpatrol.currentChange++;
                rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
            },
                function () {
                    rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
                });
        }
    });

    rcpatrol.dropdownmenu.getMenu().on('select', function () {
        var val = rcpatrol.dropdownmenu.getLabel();
        for (var option of rcpatrol.dropdown) {
            if (option.val == val) {
                rcpatrol.rcpatrolbox.setValue(option.summary);
                rcpatrol.revert(rcpatrol.changes[rcpatrol.currentChange].title, rcpatrol.changes[rcpatrol.currentChange].user, function () {
                    rcpatrol.warn(rcpatrol.changes[rcpatrol.currentChange].user, option.template, rcpatrol.changes[rcpatrol.currentChange].title);
                    rcpatrol.currentChange++;
                    rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
                    rcpatrol.dropdownmenu.getMenu().unselectItem();
                    rcpatrol.dropdownmenu.setLabel("回退并警告……");
                }, function () {
                    rcpatrol.dropdownmenu.getMenu().unselectItem();
                    rcpatrol.dropdownmenu.setLabel("回退并警告……");
                    rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
                });
                break;
            } else {
                continue;
            }
        }
    });
    /**
     * Page for processing key combinations
     */

    $(document).keydown(function (e) {
        if (e.ctrlKey && e.altKey) {
            switch (e.which) {
                case 82: e.preventDefault(); //rollback (r)
                    rcpatrol.revert(rcpatrol.changes[rcpatrol.currentChange].title, rcpatrol.changes[rcpatrol.currentChange].user, function () {
                        rcpatrol.currentChange++;
                        rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
                    },
                        function () {
                            rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
                        });
                    break;
                case 32: e.preventDefault(); //next edit ( )
                    rcpatrol.nexteditbutton.$element.click();
                    break;
                case 188: e.preventDefault(); //previous edit (<)
                    rcpatrol.previouseditbutton.$element.click();
                    break;
                case 187: e.preventDefault(); //thanks (+)
                    rcpatrol.thankbutton.$element.click();
                    break;
                default:
                    for (var option of rcpatrol.dropdown) {
                        if (option.keycode == e.which) {
                            rcpatrol.rcpatrolbox.setValue(option.summary);
                            rcpatrol.revert(rcpatrol.changes[rcpatrol.currentChange].title, rcpatrol.changes[rcpatrol.currentChange].user, function () {
                                rcpatrol.warn(rcpatrol.changes[rcpatrol.currentChange].user, option.template, rcpatrol.changes[rcpatrol.currentChange].title);
                                rcpatrol.currentChange++;
                                rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
                            }, function () {
                                rcpatrol.loadChange(rcpatrol.changes[rcpatrol.currentChange]);
                            });
                            break;
                        } else {
                            continue;
                        }
                    }
                    break;
            }
        }
    });
});
// </nowiki>