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

MediaWiki:Gadget-ShowContributors.js

MediaWiki界面页面
OctoberSama留言 | 贡献2025年6月10日 (二) 23:46的版本

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

  • 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
            }
            /* 桌面端样式 */
            @media (min-width: 1120px) {
                .skin-citizen #show-contributor-button {
                    float: none;
                    margin-left: 0;
                    margin-right: .5em;
                    display: inline-flex;
                    align-items: center
                }
                .skin-citizen .page-actions {
                    display: flex;
                    align-items: center;
                    gap: .5em
                }
            }
            /* 移动端样式 */
            @media (max-width: 1119px) {
                .skin-citizen #show-contributor-button {
                    position: absolute;
                    right: 0;
                    top: 0;
                    margin: 0.5em;
                }
                .skin-citizen .citizen-page-heading {
                    position: relative;
                    padding-right: 100px;
                }
            }
            /* 其他通用样式 */
            #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
            }
        `);

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

        class ContributorDialog extends OO.ui.Dialog {
            // ...保持原有类定义不变...
        }

        const windowManager = new OO.ui.WindowManager({
            id: "show-contributor"
        });
        $(document.body).append(windowManager.$element);
        const dialog = new ContributorDialog({
            size: "large"
        });
        windowManager.addWindows([dialog]);
        
        const button = new OO.ui.ButtonWidget({
            label: "本页贡献者",
            icon: "search",
            flags: "progressive",
            id: "show-contributor-button"
        });

        // 动态插入按钮
        const insertButton = () => {
            if ("citizen" === mw.config.get("skin")) {
                if (window.matchMedia("(min-width: 1120px)").matches) {
                    // 桌面端:插入到.page-actions
                    $(".page-actions").prepend(button.$element);
                } else {
                    // 移动端:插入到.citizen-page-heading
                    $(".citizen-page-heading").append(button.$element);
                }
            } else {
                // 其他皮肤
                $("#bodyContent").prepend(button.$element);
            }
        };

        // 初始插入
        insertButton();
        
        // 监听窗口大小变化
        $(window).on("resize", () => {
            if ("citizen" === mw.config.get("skin")) {
                button.$element.detach();
                insertButton();
            }
        });

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