MediaWiki:Gadget-patrolPlus.js

提供: 萌えっ娘百科事典
移動先: 案内検索

注意: 保存後、変更を確認するにはブラウザーのキャッシュを消去する必要がある場合があります。

  • Firefox / Safari: Shift を押しながら 再読み込み をクリックするか、Ctrl-F5 または Ctrl-R を押してください (Mac では ⌘-R)
  • Google Chrome: Ctrl-Shift-R を押してください (Mac では ⌘-Shift-R)
  • Internet Explorer: Ctrl を押しながら 最新の情報に更新 をクリックするか、Ctrl-F5 を押してください
  • Opera: メニュー → 設定 (Mac では Opera → 環境設定) に移動し、プライバシーとセキュリティ → 閲覧データを消去 → キャッシュされた画像およびファイル からキャッシュをクリアしてください。
$(function() {
    // Only load this script on RecentChanges and Watchlist
    // Use wgCanonicalSpecialPageName to provide i18n support
    if (['Recentchanges', 'Watchlist'].indexOf(mw.config.get('wgCanonicalSpecialPageName')) === -1) return;

    var patrolling = false;

    // For each unptrolled changes
    $('abbr.unpatrolled').each(function() {
        var self = $(this);

        // This rules out grouped changes
    	if (self.closest('tr').index() === 0 && self.closest('tbody').find('tr')[1]) return;

        // Get the link to diff
        // XXX: Why not('[href*="diff=0"]')
    	var link = self.closest('li,tr').find('a[href*="diff"]').not('[href*="diff=0"]').first();

        // If the link does not exist (new page), then return
        if (!link[0]) return;

        // 历史遗留问题
        // XXX: What problem is it?
        if (/action=history/.test(link.attr('href'))) return;

        var container = $('<a href="#" class="patrolLink"></a>');
        var revid = +link.attr('href').match(/diff=(\d+)/)[1];

        // Replace the node with container and add itself to it
        self.replaceWith(container).appendTo(container).before('[').after(']');
        self = container;

        function clickHandler(event) {
            event.preventDefault();

            // There's a on-going patrolling request, abort
            if (patrolling) return;

            // Asks for confirmation
            if (!window.confirm('你确定要标记此编辑为已巡查吗?')) return;

            patrolling = true;

            // Disable other links
            $('a.patrolLink').not(self).css({
                color: '#aaa',
                "text-decoration": 'none'
            });

            // Replace the element with a span showing status
            // Note that listener on self is removed ATM
            var textStatus = $('<span></span>', {
                html: '[<img src="https://static.mengniang.org/common/d/d1/Windows_10_loading.gif" style="height: 1em; margin-top: -.25em;">正在标记中……]'
            });
            self.replaceWith(textStatus);

            // Do the actual patrol request
            var api = new mw.Api();
            api.postWithToken('patrol', {
                action: 'patrol',
                format: 'json',
                revid: revid
            }).then(function(data) {
                textStatus.text('[标记成功]');
                window.setTimeout(function() {
                    textStatus.replaceWith('\u00A0');
                }, 3000);

                patrolling = false;
                $('a.patrolLink').removeAttr('style');
            }, function(error) {
                textStatus.text('[标记失败:' + error + ',请在3秒后重试]');
                window.setTimeout(function() {
                    textStatus.replaceWith(self);
                    // Re-registering events
                    self.on('click', clickHandler);
                    
                    patrolling = false;
                    $('a.patrolLink').removeAttr('style');
                }, 3000);
            });
        }

        self.on('click', clickHandler);
    });
});