Quick Patrol

Quick Patrol in MediaWiki

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey to install this script.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Quick Patrol
// @namespace    http://rabbi.town/
// @version      0.2
// @description  Quick Patrol in MediaWiki
// @author       Milkory
// @match        *://*.huijiwiki.com/*
// @match        *://*.fandom.com/*
// @match        *://*.wikipedia.org/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  let mwApi;

  window.addEventListener('load', init, false);

  async function init() {
    mwApi = new mw.Api();
    let rights = (
      await mwApi.get({
        action: 'query',
        meta: 'userinfo',
        uiprop: 'rights',
      })
    ).query.userinfo.rights;

    if (rights.includes('patrol')) {
      $('.mw-changeslist-unpatrolled').attr('data-mw-revid', function (_i, value) {
        $(this)
          .find('.unpatrolled')
          .css('cursor', 'pointer')
          .attr('title', 'Click to partoll')
          .click(function () {
            let me = $(this);
            if ($(this).text() == '!') {
              $(this).text('~').css('color', 'darkgreen').attr('title', 'Patrolling...');
              patrol(
                value,
                function () {
                  me.text('✔')
                    .css({
                      'text-decoration': 'none',
                      'border-bottom': 'none',
                      color: 'green',
                      cursor: 'default',
                    })
                    .attr('title', 'Successfully partolled');
                },
                function () {
                  console.log(`[QuickPatrol] FAILED (revid: ${value})`);
                  me.text('!').css('color', 'red').attr('title', 'Click to partol');
                }
              );
            }
          });
        return value;
      });
    }
  }

  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);
  }
})();