FANDOM


Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Chrome - Windows: Hold the Ctrl key and press the F5 key. OS X: Hold both the ⌘ Cmd and ⇧ Shift keys and press the R key.
  • Safari - Hold down the ⇧ Shift key and click the Reload toolbar button.
  • Firefox - Windows: Hold the Ctrl key, and press F5. OS X: Hold the ⌘ Cmd and ⇧ Shift keys, and then press R.
  • Internet Explorer - Hold the Ctrl key, and press F5 (or click the Refresh button).
/**
 * Name:        PatrolRC
 * Author:      KockaAdmiralac <1405223@gmail.com>
 * Description: Adds patrol links to RecentChanges
 * Version:     v1.1
 * TODO:        Merge with PatrolAll
 */
(function() {
    var config = mw.config.get([
        'stylepath',
        'wgCanonicalSpecialPageName'
    ]);
    if (
        config.wgCanonicalSpecialPageName !== 'Recentchanges' ||
        window.PatrolRCLoaded
    ) {
        return;
    }
    window.PatrolRCLoaded = true;
 
    var PatrolRC = {
        preload: function() {
            this.api = new mw.Api();
            this.api.get({
                action: 'query',
                meta: 'allmessages',
                ammessages: [
                    'error',                          // "error"
                    'markaspatrolleddiff',            // "mark as patrolled"
                    'visualeditor-dialog-action-done' // "done"
                ].join('|')
            }).done($.proxy(this.cbPreload, this));
        },
        cbPreload: function(d) {
            var msg = d.query.allmessages;
            this.i18n = {
                error: msg[0]['*'],
                patrol: msg[1]['*'],
                done: msg[2]['*']
            };
            window.ajaxCallAgain = window.ajaxCallAgain || [];
            window.ajaxCallAgain.push($.proxy(this.init, this));
            this.init();
        },
        init: function() {
            this.api.get({
                action: 'query',
                list: 'recentchanges',
                rctype: 'new|edit',
                rclimit: 'max',
                rctoken: 'patrol',
                rcprop: 'patrolled|ids',
                rcshow: '!patrolled'
            }).done($.proxy(this.cbList, this));
        },
        cbList: function(d) {
            this.changes = d.query.recentchanges;
            this.token = this.changes[0].patroltoken;
            if (!this.token) {
                console.warn('[PatrolRC] Unable to obtain patrol token');
                return;
            }
            $('.rc-conntent abbr.unpatrolled').each($.proxy(this.each, this));
        },
        each: function(_, el) {
            var $ch = $(el).parent().parent().children();
            if (
                $ch.length === 2 ||
                $ch.first().html().trim().length === 0
            ) {
                $ch.last().append(
                    '&nbsp;(',
                    $('<a>', {
                        'class': 'PatrolRC',
                        text: this.i18n.patrol
                    }).click($.proxy(this.click, this)),
                    ')'
                );
            }
        },
        getLink: function($par, name) {
            return $par.find(
                '> a[href*="&' + name + '="], > a[href*="?' + name + '="]'
            ).first().attr('href');
        },
        getRcidByCurid: function(curid) {
            var changes = this.changes.filter(function(el) {
                return el.pageid === Number(curid) && el.type === 'new';
            });
            if (changes.length === 1) {
                return changes[0].rcid;
            }
        },
        click: function(e) {
            var $this = $(e.target),
                $par = $this.parent(),
                $rcid = this.getLink($par, 'rcid'),
                $curid = this.getLink($par, 'curid'),
                rcid;
            $this.html($('<img>', {
                src: config.stylepath + '/common/images/ajax.gif'
            }));
            if ($rcid) {
                rcid = new mw.Uri($rcid).query.rcid;
            } else if ($curid) {
                rcid = this.getRcidByCurid(new mw.Uri($curid).query.curid);
            } else {
                var t = new mw.Title(new mw.Uri(
                    $par.find('a').first().attr('href')
                ).path.substring(6));
                if (t.getNamespaceId() === 1201) {
                    rcid = this.getRcidByCurid(t.getNameText());
                }
            }
            if (rcid) {
                this.api.post({
                    action: 'patrol',
                    rcid: rcid,
                    token: this.token
                }).done(this.cbGeneratePatrol($this));
            } else {
                $this.text(this.i18n.error);
            }
        },
        // Why do I think this is a good idea
        cbGeneratePatrol: function($el) {
            return $.proxy(function(d) {
                if (d.error) {
                    $el.text(this.i18n.error + ': ' + d.error.code);
                } else {
                    $el.text(this.i18n.done).css({
                        color: 'gray',
                        'text-decoration': 'line-through'
                    });
                }
            }, this);
        }
    };
 
    mw.loader.using('mediawiki.api').then($.proxy(PatrolRC.preload, PatrolRC));
})();
Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+