MediaWiki:PatrolRC/code.js

/** * Name:       PatrolRC * Author:     KockaAdmiralac <1405223@gmail.com> * Description: Adds patrol links to RecentChanges */ (function {   var config = mw.config.get([ 'stylepath', 'wgCanonicalSpecialPageName' ]);   if ( config.wgCanonicalSpecialPageName !== 'Recentchanges' || window.PatrolRCLoaded ) {       return;    }    window.PatrolRCLoaded = true;

var api, token, text;

function init { api.get({           action: 'query',            list: 'recentchanges',            rctoken: 'patrol'        }).done(function(d) {            token = d.query.recentchanges[0].patroltoken;            if (!token) {                console.warn('[PatrolRC] Unable to obtain patrol token');                return;            }            $('.rc-conntent abbr.unpatrolled').each(function { var $ch = $(this).parent.parent.children; if (                   $ch.length === 2 ||                    $ch.first.html.trim.length === 0                ) { $ch.last.append(                       ' (', $('', {                           'class': 'PatrolRC',                            text: text                        }).click(click), ')'                   );                }            });        });    }

function click { var $link = $(this).parent .find('> a[href*="&rcid="], > a[href*="?rcid="]'); if ($link.length === 1) { var url = new mw.Uri($link.attr('href')), $this = $(this); // console.log(url.query.rcid, token); $this.html($(' ', { src: config.stylepath + '/common/images/ajax.gif' }));           api.post({                action: 'patrol',                rcid: url.query.rcid,                token: token            }).done(function(d) {                if (d.error) {                    $this.html('error');                } else {                    // TODO: Find i18n message for this                    $this.html('done').css({ color: 'gray', 'text-decoration': 'line-through' });               }            });        } else { $(this).text('error'); }   }

mw.loader.using('mediawiki.api').then(function {       api = new mw.Api;        api.get({ action: 'query', meta: 'allmessages', ammessages: 'markaspatrolleddiff' }).done(function(d) { text = d.query.allmessages[0]['*']; init; window.ajaxCallAgain = window.ajaxCallAgain || []; window.ajaxCallAgain.push(init); });   }); });