User:WhitePhosphorus/js/CatUpdates.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

/**
 * 在分类底部显示最近哪些页面被列入或移除。
 * 
 * @version 0.2 (2018-09-27)
 * @author [[User:WhitePhosphorus]]
 *
 * TODO:
 *  优化界面文字:加上字节数变更之类的信息
 *  可能会添加一个限制高度的框
 *
 */

(function($, mw) {
'use strict';

// i18n
const i18n_dict = {
    title: {'zh-hans': '分类中页面的最近更改', 'zh-hant': '分類中頁面的近期變更'},
    notice: {'zh-hans': '请注意:和Special:最近更改一样,已删除页面的版本不会出现在这里。以下的时区和您的系统设定相同。',
              'zh-hant': '請注意:和Special:近期變更一樣,已刪除頁面的版本不會出現在這裡。以下的時區和您的系統設定相同。'},
    load: {'zh-hans': '点击加载', 'zh-hant': '點擊加載'},
    loading: {'zh-hans': '加载中……', 'zh-hant': '加載中⋯⋯'},
    load_failed: {'zh-hans': '加载失败', 'zh-hant': '加載失敗'},
    reload: {'zh-hans': '点击重新加载', 'zh-hant': '點擊重新加載'},
    talk: {'zh-hans': '讨论', 'zh-hant': '對話'},
    contribs: {'zh-hans': '贡献', 'zh-hant': '貢獻'},
    block: {'zh-hans': '封禁', 'zh-hant': '封鎖'}
};

const i18n = function(word) {
    let i18n_word = i18n_dict[word];
    if (!i18n_word) {
        return undefined;
    }
    let lang = mw.config.get('wgUserLanguage');
    // TODO: this can be more accurate
    if (['zh-cn', 'zh-hans', 'zh-my', 'zh-sg'].includes(lang)) {
        return i18n_word['zh-hans'];
    } else {
        return i18n_word['zh-hant'];
    }
}

// category only
if (mw.config.get('wgCanonicalNamespace') != 'Category') {
    return;
}

// insert div at the bottom of page
$('<div id="p4js-category-updates"></div>').insertAfter('.mw-category-generated');

// add section title
$('#p4js-category-updates').prepend(`<h2>${i18n('title')}</h2>`);

// add notice
$('#p4js-category-updates').append(`<p>${i18n('notice')}</p>`);

// add status
$('#p4js-category-updates').append('<p id="p4js-load-category-updates-status"></p>');

// add load link
$('#p4js-category-updates').append(`<a id="p4js-load-category-updates">${i18n('load')}</a>`);

// add log entries
$('#p4js-category-updates').append('<ul id="p4js-category-updates-entries"></ul>');

// on click function
$('#p4js-load-category-updates').click(function(e) {
    e.preventDefault();
    $(this).hide();
    $('#p4js-load-category-updates-status').text(i18n('loading'));
    $.ajax({
        url: mw.util.wikiScript('api'),
        data: {
            action: 'query',
            list: 'recentchanges',
            rctitle: mw.config.get('wgPageName'),
            rctype: 'categorize',
            rcprop: 'user|parsedcomment|timestamp|ids',
            rclimit: 'max',
            format: 'json'
        }
    }).done(function (data) {
        console.log(data);
        if (data && data.query && data.query.recentchanges) {
            $('#p4js-load-category-updates-status').hide();
            let rcs = data.query.recentchanges;

            // show log entries
            $('#p4js-category-updates-entries').html(rcs.map(formatEntry).join('\n'));
        } else {
            // failed to get data
            $('#p4js-load-category-updates-status').text(i18n('load_failed'));
            $(this).show();
            $(this).text(i18n('reload'));
        }
    });
});

const WEEKDAYS = ['日', '一', '二', '三', '四', '五', '六'];
const PATH = mw.config.get('wgArticlePath');

// input: a recentchange entry, json object
// output: corresponding html string
const formatEntry = function(rc) {
    // convert timestamp to human readable format
    let ts = rc.timestamp;
    let date = new Date(ts);
    ts = date.getFullYear() + '年' + (date.getMonth()+1) + '月' +
        date.getDate() + '日 (' + WEEKDAYS[date.getDay()] + ') ' +
        ('0'+date.getHours()).slice(-2) + ':' +
        ('0'+date.getMinutes()).slice(-2);
    return `<li>(<a href="${PATH.replace('$1', 'Special:diff/' + rc.revid)}" class="mw-changeslist-diff">差异</a> ` +
        `| <a href="/?curid=${rc.pageid}&action=history" class="mw-changeslist-history">历史</a>)` +
        `<a href="${PATH.replace('$1', 'Special:permalink/' + rc.revid)}" class="mw-changeslist-date">${ts}</a> ` +
        `<span class="mw-changeslist-separator">. .</span> ` +
        `<a href="${PATH.replace('$1', 'User:' + rc.user)}">${rc.user}</a>` +
        `(<a href="${PATH.replace('$1', 'User_talk:' + rc.user)}">${i18n('talk')}</a> | ` +
        `<a href="${PATH.replace('$1', 'Special:contribs/' + rc.user)}">${i18n('contribs')}</a>` +
        `<span class="sysop-show"> | <a href="${PATH.replace('$1', 'Special:block/' + rc.user)}">${i18n('block')}</a></span>) ` +
        `<span class="mw-changeslist-separator">. .</span> ` +
        `${rc.parsedcomment}</li>`;
}

})(jQuery, mediaWiki);