打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Gadget-ShowContributors.js

MediaWiki界面页面
OctoberSama留言 | 贡献2025年6月11日 (三) 00:15的版本

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
原作者BearBin
BSD 3-Clause License

BSD 3-Clause License

Copyright (c) 2023, BearBin

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice,this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE US OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(() => {
    "use strict";
    $((() => (async () => {
        if (
            ![0, 2, 4, 10, 12, 14, 828, 274].includes(mw.config.get("wgNamespaceNumber")) ||
            0 === mw.config.get("wgArticleId") ||
            !["view", "history"].includes(mw.config.get("wgAction"))
        )
            return;

        // 添加样式(含移动端媒体查询)
        mw.loader.addStyleTag(`
#show-contributor-button {
    float: right;
    margin-left: .5em;
    margin-right: 0;
}
#show-contributor-header {
    background: #fff;
    border-bottom: 1px solid #aaa;
    font-weight: 600;
    padding: .3em;
    position: sticky;
    text-align: center;
    top: 0;
}
#show-contributor-headline {
    font-size: 1.3em;
}
#show-contributor-close {
    border-radius: 50%;
    cursor: pointer;
    position: absolute;
    right: 5px;
    top: 5px;
}
#show-contributor-close:hover {
    background-color: #eee;
}
#show-contributor-table {
    margin: 0;
    width: 100%;
}
#show-contributor-table .user-avatar {
    border-radius: 50%;
    height: 20px;
    width: 20px;
}
/* Citizen 桌面样式 */
.skin-citizen #show-contributor-button {
    float: none;
    margin-left: 0;
    margin-right: .5em;
    display: inline-flex;
    align-items: center;
}
/* Citizen 容器样式 */
.skin-citizen .page-actions {
    display: flex;
    align-items: center;
    gap: .5em;
}
/* Citizen 移动端样式:将按钮定位到标题栏右侧 */
@media screen and (max-width: 1119px) {
    .skin-citizen .page-actions {
        position: absolute;
        top: 0.4em;
        right: 0.8em;
    }
    .skin-citizen #show-contributor-button {
        margin: 0;
        float: none;
    }
}
        `);

        await mw.loader.using([
            "mediawiki.api",
            "mediawiki.notification",
            "oojs-ui",
            "oojs-ui.styles.icons-interactions",
            "jquery.tablesorter"
        ]);

        class t extends OO.ui.Dialog {
            $table = $('<table id="show-contributor-table" class="wikitable" />');
            $tbody = $("<tbody />");
            $body;
            got = !1;
            static static = {
                ...super.static,
                name: "ShowContributor",
                tagName: "div"
            };
            initialize() {
                return super.initialize(),
                    this.$body.append(
                        $('<div id="show-contributor-header" />').append(
                            $('<div id="show-contributor-headline">本页贡献统计</div>'),
                            new OO.ui.IconWidget({
                                icon: "close",
                                id: "show-contributor-close"
                            }).$element.on("click", (() => this.close()))
                        ),
                        this.$table.append(
                            $("<thead><th>用户</th><th>编辑数</th><th>增加字节数</th><th>删减字节数</th></thead>"),
                            this.$tbody
                        )
                    ),
                    this
            }
            getContributors = async () => {
                const t = new mw.Api,
                    e = {};
                let o = "",
                    i = 0;
                const r = {
                    action: "query",
                    format: "json",
                    prop: "revisions",
                    titles: mw.config.get("wgPageName"),
                    rvprop: "user|size",
                    rvlimit: "max",
                    rvdir: "newer"
                };
                do {
                    o && (r.rvcontinue = o);
                    try {
                        const n = await t.get(r);
                        o = n.continue?.rvcontinue;
                        for (const {
                            user: t,
                            size: o
                        } of Object.values(n.query.pages)[0].revisions)
                            e[t] ||= [],
                                e[t].push(o - i),
                                i = o
                    } catch (t) {
                        mw.notify(`获取编辑记录失败:${t}`, {
                            type: "error"
                        })
                    }
                } while (o);
                return e
            };
            addRow = (t, {
                user: e,
                count: o,
                add: i,
                remove: r
            }) => {
                t.append(
                    $("<tr />").append(
                        $("<td />").append(
                            $(`<a href="${mw.config.get("wgArticlePath").replace("$1", `User:${e}`)}" />`).append(
                                `<img class="user-avatar" src="https://commons.moegirl.org.cn/extensions/Avatar/avatar.php?user=${e}" />`,
                                e
                            )
                        ),
                        `<td>${o}</td>`,
                        `<td>${i}</td>`,
                        `<td>${r}</td>`
                    )
                )
            };
            showContributors = t => {
                this.$tbody.empty();
                for (const e in t)
                    this.addRow(this.$tbody, {
                        user: e,
                        count: t[e].length,
                        add: t[e].reduce(((t, e) => e > 0 ? t + e : t), 0),
                        remove: t[e].reduce(((t, e) => e < 0 ? t + e : t), 0)
                    });
                this.got = !0
            }
        }

        const e = new OO.ui.WindowManager({
            id: "show-contributor"
        });
        $(document.body).append(e.$element);

        const o = new t({
            size: "large"
        });
        e.addWindows([o]);

        const i = new OO.ui.ButtonWidget({
            label: "本页贡献者",
            icon: "search",
            flags: "progressive",
            id: "show-contributor-button"
        });

        // 插入按钮(统一插入 .page-actions,不动态判断屏幕宽度)
        if ("citizen" === mw.config.get("skin")) {
            $(".page-actions").prepend(i.$element);
        } else {
            $("#bodyContent").prepend(i.$element);
        }

        i.on("click", (async () => {
            if (!o.got) {
                i.setLabel("正在查询");
                const t = await o.getContributors();
                o.showContributors(t),
                    o.$table.tablesorter(),
                    i.setLabel("本页贡献者")
            }
            e.openWindow(o)
        }));
    })()))
})();