「MediaWiki:Gadget-allmessageFilter.js」の版間の差分

提供: 萌えっ娘百科事典
移動先: 案内検索
(bot work)
(タグ: Bot)
 
(// Edit Via InPageEdit)
73行目: 73行目:
 
             row.attr('id', name).append('<td class="am_title"></td><td class="am_default"></td>');
 
             row.attr('id', name).append('<td class="am_title"></td><td class="am_default"></td>');
 
             $('<a/>').addClass('external').attr({
 
             $('<a/>').addClass('external').attr({
                 href: 'https://zh.moegirl.org/MediaWiki:' + name,
+
                 href: 'https://zh.moegirl.org.cn/MediaWiki:' + name,
 
                 rel: 'nofollow'
 
                 rel: 'nofollow'
 
             }).html(name.replace(rfilter, '<span style="font-weight: bold;">' + filter + '</span>')).appendTo(row.find('.am_title'));
 
             }).html(name.replace(rfilter, '<span style="font-weight: bold;">' + filter + '</span>')).appendTo(row.find('.am_title'));

2022年3月29日 (火) 13:21時点における版

$(window).on('load', function() {
    if (mw.config.get('wgNamespaceNumber') !== -1 || mw.config.get('wgCanonicalSpecialPageName') !== "Allmessages") return;
    mw.util.addCSS([
        ".TablePager_nav {",
        "    user-select: none;",
        "}",
        ".TablePager_nav-enabled {",
        "    cursor: pointer;",
        "}",
        "#mw-allmessages-filter-status {",
        "    color: red;",
        "}"
    ].join('\n'));
    var containter = $('<fieldset/>'),
        api = new mw.Api(),
        length = +$('#mw-table_pager_limit_label').val();
    var index = 0,
        filter, rfilter, am;
    var enablePrevLink = function enablePrevLink(b) {
        if (!b) $('.TablePager_nav-prev div').removeClass("TablePager_nav-enabled").addClass("TablePager_nav-disabled");
        else $('.TablePager_nav-prev div').removeClass("TablePager_nav-disabled").addClass("TablePager_nav-enabled");
    };
    var enableNextLink = function enableNextLink(b) {
        if (!b) $('.TablePager_nav-next div').removeClass("TablePager_nav-enabled").addClass("TablePager_nav-disabled");
        else $('.TablePager_nav-next div').removeClass("TablePager_nav-disabled").addClass("TablePager_nav-enabled");
    };
    var check = function check(am) {
        if (am.length <= length) {
            enableNextLink(false);
            enablePrevLink(false);
        } else if (index === 0) {
            enablePrevLink(false);
            enableNextLink(true);
        } else if (index + length < am.length) {
            enablePrevLink(true);
            enableNextLink(true);
        } else {
            enablePrevLink(true);
            enableNextLink(false);
        }
    };
    var init = function init() {
        if (!$('#mw-allmessages-form')[0]) return;
        $('#mw-allmessages-form').remove();
        $('.TablePager_nav td').remove();
        $('.TablePager_nav tr').append('<td style="width: 50%;" class="TablePager_nav-prev"><div class="TablePager_nav-disabled">上一页</div></td><td style="width: 50%;" class="TablePager_nav-next"><div class="TablePager_nav-disabled">下一页</div></td>');
        $('.TablePager_nav-prev div').on('click', function() {
            if ($(this).hasClass('TablePager_nav-disabled')) return false;
            index -= length;
            if (index < 0) index = 0;
            load(am.slice(index, index + length));
        });
        $('.TablePager_nav-next div').on('click', function() {
            if ($(this).hasClass('TablePager_nav-disabled')) return false;
            index += length;
            load(am.slice(index, index + length));
        });
    };
    var load = function load(_am) {
        check(am);
        var head = $('#mw-allmessagestable thead');
        if (head.find('tr').length !== 1) {
            head.find('td:first').removeAttr('rowspan');
            head.find('tr:first td:last').text('当前信息文字');
            head.find('tr:not(:first)').remove();
        }
        var list = $('#mw-allmessagestable tbody');
        list.empty();
        _am.forEach(function(n) {
            var name = n.name,
                text = n['*'];
            var row = $('<tr/>');
            row.attr('id', name).append('<td class="am_title"></td><td class="am_default"></td>');
            $('<a/>').addClass('external').attr({
                href: 'https://zh.moegirl.org.cn/MediaWiki:' + name,
                rel: 'nofollow'
            }).html(name.replace(rfilter, '<span style="font-weight: bold;">' + filter + '</span>')).appendTo(row.find('.am_title'));
            row.find('.am_default').text(text);
            list.append(row);
        });
    };
    $('#mw-allmessages-form').after(containter);
    containter.append('<legend>搜索</legend>');
    var table = $('<table/>');
    containter.append(table);
    table.append('<tr></tr><tr></tr>');
    $('<td/>').addClass('mw-label').html('<label for="mw-allmessages-filter">以含有此字符串过滤:</label>').appendTo(table.find('tr:first'));
    $('<td/>').addClass('mw-input').html('<input size="20" value="" id="mw-allmessages-filter">').appendTo(table.find('tr:first'));
    $('<td/>').appendTo(table.find('tr:last'));
    var submit = $('<button/>');
    submit.text('搜索');
    $('<td/>').addClass('mw-input').append(submit).appendTo(table.find('tr:last'));
    var input = $('#mw-allmessages-filter');
    submit.on('click', function() {
        if (!input.val()) return alert('请输入内容以搜索系统消息');
        $('#mw-allmessages-filter-status').remove();
        init();
        filter = input.val();
        rfilter = RegExp(filter, 'i');
        containter.append('<div id="mw-allmessages-filter-status">正在加载中……</div>');
        api.post({
            action: 'query',
            format: 'json',
            meta: 'allmessages',
            amfilter: filter
        }).then(function(data) {
            am = data.query.allmessages;
            index = 0;
            $('#mw-allmessages-filter-status').remove();
            load(am.slice(index, index + length));
        }, function() {
            $('#mw-allmessages-filter-status').remove();
            containter.append('<div id="mw-allmessages-filter-status">发生错误,请重试!</div>');
        });
    });
    input.on('keypress', function(e) {
        if (e.which == 13) submit.click();
    });
});