MediaWiki:Gadget-allmessageFilter.js

来自萌娘文库
星海-interfacebot讨论 | 贡献2022年9月6日 (二) 01:35的版本 (跨站同步)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/* <pre> */
/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * -------------------------------------------------------------------------
 */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/allmessageFilter/MediaWiki:Gadget-allmessageFilter.js|user=[[U:AnnAngela]]|longId=4f769b86c99bf278bee8d120937a50b49682e4e2|shortId=4f769b8|message=refactor: Stop using jQuery deprecated event shorthand methods}}';

"use strict";
(function (isCompleted) {
    function run() {
        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 (_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: "".concat(mw.config.get("wgServer")).concat(mw.config.get("wgScriptPath"), "/MediaWiki:").concat(name),
                    rel: "nofollow"
                }).html(name.replace(rfilter, "<span style=\"font-weight: bold;\">".concat(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 oouiDialog.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.key === "Enter") {
                submit.trigger("click");
            }
        });
    }
    if (isCompleted) {
        run();
    }
    else {
        $(window).on("load", run);
    }
})(document.readyState === "complete");

/* </pre> */