QuickPatrol_v2

MediaWiki巡查工具 | A patrol tool for MediaWiki

当前为 2024-11-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         QuickPatrol_v2
// @namespace    qp_tool_v2
// @version      1.21
// @description  MediaWiki巡查工具 | A patrol tool for MediaWiki
// @author       teaSummer
// @match        *://*/wiki/*
// @match        *://*/w/*
// @require      https://fastly.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @license      MIT
// @grant        none
// ==/UserScript==

// 为简易、快速巡查而生(不过各位还请认真巡查喔),并且支持InPageEdit。  
// Make patrol easier and quicker, also support for InPageEdit.
//
// 使用方法:点击任意处的“!”(若在InPageEdit的快速差异页面,请待修订版本号变蓝后点击)  
// How to use: Click "! "anywhere (If you are in the Quick Diff page of InPageEdit, click after the revision number turns blue)
//
// 此脚本基于[脚本445948](https://greasyfork.org/zh-CN/scripts/445948)开发。  
// This script is based on [Script 445948](https://greasyfork.org/zh-CN/scripts/445948).

(function () {
    'use strict';

    const config = {
        liveUpdate: false // 全局实时更新 | Global Live Update
    };

    let mwApi, rights;

    async function init() {
        try {
            mwApi = new mw.Api();
        } catch (e) {
            console.log('[QuickPatrol] Failed to get mwApi. Will try again in 0.8s.');
            await setTimeout(init, 800);
            return;
        }

        console.log('[QuickPatrol] Checking rights...');

        rights = (
            await mwApi.get({
                action: 'query',
                meta: 'userinfo',
                uiprop: 'rights',
            })
        ).query.userinfo.rights;

        if (!config.liveUpdate) {
            quick_patrol();
        }
        return;
    }

    function with_ipe() {
        $('.diff-version:not([title])').css({ color: 'blue', cursor: 'pointer' });
            $('.diff-version').click(function () {
                let me = $(this);
                if (me.attr('title')) return;
                let value = me.text().replace('版本', '');
                $(this).css('color', 'deeppink').attr('title', '快速巡查中…');
                patrol(
                    value,
                    function () {
                        me.text('✔')
                            .css({
                                color: 'green',
                                cursor: 'default',
                            })
                            .attr('title', '已快速巡查');
                    },
                    function () {
                        console.log(`[QuickPatrol] FAILED (revid: ${value})`);
                    }
                );
                return value;
            });
    }

    async function quick_patrol() {
        if (!rights) {
            init();
            return;
        }
        if (rights.includes('patrol')) {
            $('.mw-changeslist-reviewstatus-unpatrolled:not(.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey), .revisionpatrol-unpatrolled').attr('data-mw-revid', function (_i, value) {
                let that = $(this);
                that.find('.revisionpatrol-icon-unpatrolled').after('<span class="unpatrolled custom-unpatrolled" title="该编辑尚未巡查">!</span>');
                that.find('.revisionpatrol-icon-unpatrolled+.custom-unpatrolled').css({
                    color: 'red',
                    'font-weight': 'bold',
                    position: 'absolute',
                    left: '-0.8em',
                    'margin-left': '-1px',
                    'text-decoration': 'underline dotted'
                });
                that.find('.revisionpatrol-icon-unpatrolled').remove();
                that.find('.unpatrolled')
                    .css('cursor', 'pointer')
                    .click(async function () {
                        let me = $(this);
                        if (me.hasClass('custom-unpatrolled')) {
                            me.css('left', '-1em');
                        }
                        if (me.text() == '!') {
                            $(this).text('#').css('color', 'magenta').attr('title', '快速巡查中…');
                            if (!value && that.attr('data-mw-logid')) {
                                try {
                                    value = new RegExp(`"logid":${that.attr('data-mw-logid')},.+?,"revid":([0-9]+)`).exec(JSON.stringify((await mwApi
                                        .get({
                                            action: 'query',
                                            list: 'logevents',
                                            leprop: 'ids',
                                            letitle: that.find('td.mw-enhanced-rc-nested').attr('data-target-page'),
                                            letype: that.attr('data-mw-logaction').split('/')[0],
                                            lelimit: 'max',
                                            format: 'json',
                                        })
                                    )))[1];
                                } catch (e) {
                                    return;
                                }

                            }
                            patrol(
                                value,
                                function () {
                                    me.text('✔')
                                        .css({
                                            color: 'green',
                                            cursor: 'default',
                                        })
                                        .attr('title', '已快速巡查').removeClass('unpatrolled');
                                    if (me.hasClass('custom-unpatrolled')) {
                                        me.css({
                                            left: '-1.15em',
                                            'font-weight': 'normal'
                                        });
                                    }
                                    that.removeClass('mw-rcfilters-highlight-color-c5').removeClass('mw-changeslist-reviewstatus-unpatrolled');
                                    that.attr('title', that.attr('title').replace(/、?\u200B?未巡查、?\u200B?/g, ''));
                                    if (that.attr('title') == '已高亮:') that.removeAttr('title');
                                },
                                function () {
                                    console.log(`[QuickPatrol] FAILED (revid: ${value})`);
                                    me.text('!').css('color', 'red').attr('title', '该编辑尚未巡查');
                                    if (me.hasClass('custom-unpatrolled')) {
                                        me.css('left', '-0.8em');
                                    }
                                }
                            );
                        }
                    });
                return value;
            });
            with_ipe();
        }
    }

    function patrol(revid, successFallback, failFallback) {
        mwApi
            .get({
                action: 'query',
                meta: 'tokens',
                type: 'patrol',
                format: 'json',
            })
            .done(function (data) {
                console.log(`[QuickPatrol] TRYING (revid: ${revid})`);
                mwApi
                    .post({
                        action: 'patrol',
                        revid: revid,
                        token: data.query.tokens.patroltoken,
                        format: 'json',
                    })
                    .done(function () {
                        console.log(`[QuickPatrol] SUCCESS (revid: ${revid})`);
                        successFallback();
                    })
                    .fail(failFallback);
            })
            .fail(failFallback);
    }


    window.addEventListener('load', init, false);
    if (config.liveUpdate) {
        window.setInterval(quick_patrol, 1000);
    } else {
        window.setInterval(with_ipe, 1000);
    }
})();