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

MediaWiki:Gadget-ShowContributors.js:修订间差异

MediaWiki界面页面
第8行: 第8行:
         ) return;
         ) return;


        // 添加样式
         mw.loader.addStyleTag(`
         mw.loader.addStyleTag(`
            #show-contributor-button {
#show-contributor-button {
                float: right;
    float: right;
                margin-left: .5em;
    margin-left: .5em;
                margin-right: 0;
    margin-right: 0;
            }
}
            #show-contributor-header {
#show-contributor-header {
                background: #fff;
    background: #fff;
                border-bottom: 1px solid #aaa;
    border-bottom: 1px solid #aaa;
                font-weight: 600;
    font-weight: 600;
                padding: .3em;
    padding: .3em;
                position: sticky;
    position: sticky;
                text-align: center;
    text-align: center;
                top: 0;
    top: 0;
            }
}
            #show-contributor-headline {
#show-contributor-headline {
                font-size: 1.3em;
    font-size: 1.3em;
            }
}
            #show-contributor-close {
#show-contributor-close {
                border-radius: 50%;
    border-radius: 50%;
                cursor: pointer;
    cursor: pointer;
                position: absolute;
    position: absolute;
                right: 5px;
    right: 5px;
                top: 5px;
    top: 5px;
            }
}
            #show-contributor-close:hover {
#show-contributor-close:hover {
                background-color: #eee;
    background-color: #eee;
            }
}
            #show-contributor-table {
#show-contributor-table {
                margin: 0;
    margin: 0;
                width: 100%;
    width: 100%;
            }
}
            #show-contributor-table .user-avatar {
#show-contributor-table .user-avatar {
                border-radius: 50%;
    border-radius: 50%;
                height: 20px;
    height: 20px;
                width: 20px;
    width: 20px;
            }
}
            /* Citizen 样式调整 */
.skin-citizen #show-contributor-button {
            .skin-citizen #show-contributor-button {
    display: inline-flex;
                display: inline-flex;
    align-items: center;
                align-items: center;
}
            }
`);
            @media screen and (max-width: 1119px) {
                .skin-citizen #show-contributor-button {
                    float: right;
                    margin-left: auto;
                }
            }
            .skin-citizen .page-actions {
                display: flex;
                align-items: center;
                gap: .5em;
            }
        `);


         await mw.loader.using([
         await mw.loader.using([
第75行: 第62行:
             $table = $('<table id="show-contributor-table" class="wikitable" />');
             $table = $('<table id="show-contributor-table" class="wikitable" />');
             $tbody = $("<tbody />");
             $tbody = $("<tbody />");
            $body;
             got = false;
             got = false;
             static static = {
             static static = {
                 ...super.static,
                 ...super.static,
第82行: 第69行:
                 tagName: "div"
                 tagName: "div"
             };
             };
             initialize() {
             initialize() {
                 super.initialize();
                 super.initialize();
第93行: 第81行:
                     ),
                     ),
                     this.$table.append(
                     this.$table.append(
                         $("<thead><tr><th>用户</th><th>编辑数</th><th>增加字节数</th><th>删减字节数</th></tr></thead>"),
                         $("<thead><th>用户</th><th>编辑数</th><th>增加字节数</th><th>删减字节数</th></thead>"),
                         this.$tbody
                         this.$tbody
                     )
                     )
第99行: 第87行:
                 return this;
                 return this;
             }
             }
             getContributors = async () => {
 
             async getContributors() {
                 const api = new mw.Api();
                 const api = new mw.Api();
                 const users = {};
                 const users = {};
                 let rvcontinue = "";
                 let rvcontinue = "";
                 let lastSize = 0;
                 let lastSize = 0;
                 const params = {
                 const params = {
                     action: "query",
                     action: "query",
第113行: 第103行:
                     rvdir: "newer"
                     rvdir: "newer"
                 };
                 };
                 do {
                 do {
                     if (rvcontinue) params.rvcontinue = rvcontinue;
                     if (rvcontinue) params.rvcontinue = rvcontinue;
                     try {
                     try {
                         const data = await api.get(params);
                         const result = await api.get(params);
                         rvcontinue = data.continue?.rvcontinue;
                         rvcontinue = result.continue?.rvcontinue;
                         const revisions = Object.values(data.query.pages)[0].revisions;
                         for (const { user, size } of Object.values(result.query.pages)[0].revisions) {
                        for (const { user, size } of revisions) {
                             users[user] ||= [];
                             users[user] ||= [];
                             users[user].push(size - lastSize);
                             users[user].push(size - lastSize);
第129行: 第119行:
                     }
                     }
                 } while (rvcontinue);
                 } while (rvcontinue);
                 return users;
                 return users;
             };
             }
             addRow = (tbody, { user, count, add, remove }) => {
 
                 tbody.append(
             addRow($target, { user, count, add, remove }) {
                 $target.append(
                     $("<tr />").append(
                     $("<tr />").append(
                         $("<td />").append(
                         $("<td />").append(
第145行: 第137行:
                     )
                     )
                 );
                 );
             };
             }
             showContributors = (data) => {
 
             showContributors(data) {
                 this.$tbody.empty();
                 this.$tbody.empty();
                 for (const user in data) {
                 for (const user in data) {
                     const count = data[user].length;
                     const edits = data[user];
                    const add = data[user].reduce((t, v) => v > 0 ? t + v : t, 0);
                    this.addRow(this.$tbody, {
                    const remove = data[user].reduce((t, v) => v < 0 ? t + v : t, 0);
                        user,
                     this.addRow(this.$tbody, { user, count, add, remove });
                        count: edits.length,
                        add: edits.reduce((sum, n) => n > 0 ? sum + n : sum, 0),
                        remove: edits.reduce((sum, n) => n < 0 ? sum + n : sum, 0)
                     });
                 }
                 }
                 this.got = true;
                 this.got = true;
             };
             }
         }
         }


        const dialogWindow = new ContributorDialog({ size: "large" });
         const windowManager = new OO.ui.WindowManager({ id: "show-contributor" });
         const windowManager = new OO.ui.WindowManager({ id: "show-contributor" });
         $(document.body).append(windowManager.$element);
         $(document.body).append(windowManager.$element);
        windowManager.addWindows([dialogWindow]);


         const dialog = new ContributorDialog({ size: "large" });
         const $button = new OO.ui.ButtonWidget({
        windowManager.addWindows([dialog]);
 
        const button = new OO.ui.ButtonWidget({
             label: "本页贡献者",
             label: "本页贡献者",
             icon: "search",
             icon: "search",
             flags: "progressive",
             flags: "progressive",
             id: "show-contributor-button"
             id: "show-contributor-button"
         });
         }).$element;
 
        const openDialog = async () => {
            if (!dialogWindow.got) {
                $button.find(".oo-ui-labelElement-label").text("正在查询");
                const data = await dialogWindow.getContributors();
                dialogWindow.showContributors(data);
                dialogWindow.$table.tablesorter();
                $button.find(".oo-ui-labelElement-label").text("本页贡献者");
            }
            windowManager.openWindow(dialogWindow);
        };
 
        $button.on("click", openDialog);


         // 插入按钮位置
         const insertButton = () => {
        const skin = mw.config.get("skin");
            const skin = mw.config.get("skin");
        const width = window.innerWidth;
            const width = window.innerWidth;
            $("#show-contributor-button").remove();


        if (skin === "citizen") {
            if (skin === "citizen") {
            if (width <= 1119) {
                if (width <= 1119) {
                $(".citizen-page-heading").append(button.$element);
                    $(".citizen-page-heading").append($button);
            } else if (width >= 1200) {
                } else if (width >= 1200) {
                $(".page-actions").prepend(button.$element);
                    $(".page-actions").prepend($button);
                }
             } else {
             } else {
                 console.warn("宽度不在插入按钮的设定范围内。");
                 $("#bodyContent").prepend($button);
             }
             }
         } else {
         };
            $("#bodyContent").prepend(button.$element);
 
        }
        // 初始插入
        insertButton();


         button.on("click", async () => {
         // 窗口尺寸变化时重新插入(节流)
             if (!dialog.got) {
        let resizeTimer = null;
                button.setLabel("正在查询");
        $(window).on("resize", () => {
                const data = await dialog.getContributors();
             clearTimeout(resizeTimer);
                dialog.showContributors(data);
            resizeTimer = setTimeout(insertButton, 200); // 节流,避免高频触发
                dialog.$table.tablesorter();
                button.setLabel("本页贡献者");
            }
            windowManager.openWindow(dialog);
         });
         });
     })());
     })());
})();
})();

2025年6月11日 (三) 00:35的版本

(() => {
    "use strict";
    $(() => (async () => {
        if (
            ![0, 2, 4, 10, 12, 14, 828, 274].includes(mw.config.get("wgNamespaceNumber")) ||
            mw.config.get("wgArticleId") === 0 ||
            !["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;
}
.skin-citizen #show-contributor-button {
    display: inline-flex;
    align-items: center;
}
`);

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

        class ContributorDialog extends OO.ui.Dialog {
            $table = $('<table id="show-contributor-table" class="wikitable" />');
            $tbody = $("<tbody />");
            got = false;

            static static = {
                ...super.static,
                name: "ShowContributor",
                tagName: "div"
            };

            initialize() {
                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
                    )
                );
                return this;
            }

            async getContributors() {
                const api = new mw.Api();
                const users = {};
                let rvcontinue = "";
                let lastSize = 0;

                const params = {
                    action: "query",
                    format: "json",
                    prop: "revisions",
                    titles: mw.config.get("wgPageName"),
                    rvprop: "user|size",
                    rvlimit: "max",
                    rvdir: "newer"
                };

                do {
                    if (rvcontinue) params.rvcontinue = rvcontinue;
                    try {
                        const result = await api.get(params);
                        rvcontinue = result.continue?.rvcontinue;
                        for (const { user, size } of Object.values(result.query.pages)[0].revisions) {
                            users[user] ||= [];
                            users[user].push(size - lastSize);
                            lastSize = size;
                        }
                    } catch (err) {
                        mw.notify(`获取编辑记录失败:${err}`, { type: "error" });
                        break;
                    }
                } while (rvcontinue);

                return users;
            }

            addRow($target, { user, count, add, remove }) {
                $target.append(
                    $("<tr />").append(
                        $("<td />").append(
                            $(`<a href="${mw.config.get("wgArticlePath").replace("$1", `User:${user}`)}" />`).append(
                                `<img class="user-avatar" src="https://commons.moegirl.org.cn/extensions/Avatar/avatar.php?user=${user}" />`,
                                user
                            )
                        ),
                        `<td>${count}</td>`,
                        `<td>${add}</td>`,
                        `<td>${remove}</td>`
                    )
                );
            }

            showContributors(data) {
                this.$tbody.empty();
                for (const user in data) {
                    const edits = data[user];
                    this.addRow(this.$tbody, {
                        user,
                        count: edits.length,
                        add: edits.reduce((sum, n) => n > 0 ? sum + n : sum, 0),
                        remove: edits.reduce((sum, n) => n < 0 ? sum + n : sum, 0)
                    });
                }
                this.got = true;
            }
        }

        const dialogWindow = new ContributorDialog({ size: "large" });
        const windowManager = new OO.ui.WindowManager({ id: "show-contributor" });
        $(document.body).append(windowManager.$element);
        windowManager.addWindows([dialogWindow]);

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

        const openDialog = async () => {
            if (!dialogWindow.got) {
                $button.find(".oo-ui-labelElement-label").text("正在查询");
                const data = await dialogWindow.getContributors();
                dialogWindow.showContributors(data);
                dialogWindow.$table.tablesorter();
                $button.find(".oo-ui-labelElement-label").text("本页贡献者");
            }
            windowManager.openWindow(dialogWindow);
        };

        $button.on("click", openDialog);

        const insertButton = () => {
            const skin = mw.config.get("skin");
            const width = window.innerWidth;
            $("#show-contributor-button").remove();

            if (skin === "citizen") {
                if (width <= 1119) {
                    $(".citizen-page-heading").append($button);
                } else if (width >= 1200) {
                    $(".page-actions").prepend($button);
                }
            } else {
                $("#bodyContent").prepend($button);
            }
        };

        // 初始插入
        insertButton();

        // 窗口尺寸变化时重新插入(节流)
        let resizeTimer = null;
        $(window).on("resize", () => {
            clearTimeout(resizeTimer);
            resizeTimer = setTimeout(insertButton, 200); // 节流,避免高频触发
        });

    })());
})();