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

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

MediaWiki界面页面
(未显示同一用户的36个中间版本)
第1行: 第1行:
(() => {
// 原作者萌娘百科BearBin
//遵循BSD 3-Clause License使用代码
//全文请参见上方文档
//GitHub仓库地址:https://github.com/BearBin1215/MoegirlPedia
(function() {
     "use strict";
     "use strict";
     $((() => (async () => {
     $(function() {
        if (
        (function() {
            ![0, 2, 4, 10, 12, 14, 828, 274].includes(mw.config.get("wgNamespaceNumber")) ||
            if (
            0 === mw.config.get("wgArticleId") ||
                ![0, 2, 4, 10, 12, 14, 828, 274].includes(mw.config.get("wgNamespaceNumber")) ||
            !["view", "history"].includes(mw.config.get("wgAction"))
                0 === mw.config.get("wgArticleId") ||
        )
                !["view", "history"].includes(mw.config.get("wgAction"))
            return;
            )
                return;


        // 添加样式(含移动端媒体查询)
            mw.loader.using([
        mw.loader.addStyleTag(`
                "mediawiki.api",
#show-contributor-button {
                "mediawiki.notification",
    float: right;
                "oojs-ui",
    margin-left: .5em;
                "oojs-ui.styles.icons-interactions",
    margin-right: 0;
                "jquery.tablesorter"
}
            ]).then(function() {
#show-contributor-header {
                function ShowContributorDialog(config) {
    background: #fff;
                    OO.ui.Dialog.call(this, config);
    border-bottom: 1px solid #aaa;
                    this.$table = $('<table id="show-contributor-table" class="wikitable" />');
    font-weight: 600;
                    this.$tbody = $("<tbody />");
    padding: .3em;
                    this.got = false;
    position: sticky;
                }
    text-align: center;
                OO.inheritClass(ShowContributorDialog, OO.ui.Dialog);
    top: 0;
                ShowContributorDialog.static = ShowContributorDialog.static || {};
}
                ShowContributorDialog.static.name = "ShowContributor";
#show-contributor-headline {
                ShowContributorDialog.static.tagName = "div";
    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([
                ShowContributorDialog.prototype.initialize = function() {
            "mediawiki.api",
                    OO.ui.Dialog.prototype.initialize.call(this);
            "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(
                     this.$body.append(
                         $('<div id="show-contributor-header" />').append(
                         $('<div id="show-contributor-header" />').append(
                             $('<div id="show-contributor-headline">本页贡献统计</div>'),
                             $('<div id="show-contributor-headline">本页贡献统计</div>'),
                             new OO.ui.IconWidget({
                             new OO.ui.IconWidget({icon:"close",id:"show-contributor-close"}).$element.on("click", function() {
                                icon: "close",
                                this.close();
                                id: "show-contributor-close"
                            }.bind(this))
                            }).$element.on("click", (() => this.close()))
                         ),
                         ),
                         this.$table.append(
                         this.$table.append(
第107行: 第45行:
                             this.$tbody
                             this.$tbody
                         )
                         )
                     ),
                     );
                     this
                     return this;
            }
                };
            getContributors = async () => {
 
                const t = new mw.Api,
                ShowContributorDialog.prototype.getContributors = function() {
                    e = {};
                    var self = this;
                let o = "",
                    return new Promise(function(resolve, reject) {
                    i = 0;
                        var api = new mw.Api();
                const r = {
                        var contributors = {};
                    action: "query",
                        var continueToken = "";
                    format: "json",
                        var prevSize = 0;
                    prop: "revisions",
                        var params = {
                    titles: mw.config.get("wgPageName"),
                            action: "query",
                    rvprop: "user|size",
                            format: "json",
                    rvlimit: "max",
                            prop: "revisions",
                    rvdir: "newer"
                            titles: mw.config.get("wgPageName"),
                            rvprop: "user|size",
                            rvlimit: "max",
                            rvdir: "newer"
                        };
                        function fetchRevisions() {
                            if (continueToken) params.rvcontinue = continueToken;
                            api.get(params).then(function(response) {
                                continueToken = response.continue ? response.continue.rvcontinue : undefined;
                                var revisions = Object.values(response.query.pages)[0].revisions;
                                for (var i = 0; i < revisions.length; i++) {
                                    var user = revisions[i].user;
                                    var size = revisions[i].size;
                                    contributors[user] = contributors[user] || [];
                                    contributors[user].push(size - prevSize);
                                    prevSize = size;
                                }
                                if (continueToken) {
                                    fetchRevisions();
                                } else {
                                    resolve(contributors);
                                }
                            }).catch(function(error) {
                                mw.notify('获取编辑记录失败:' + error, { type: "error" });
                                reject(error);
                            });
                        }
                        fetchRevisions();
                    });
                };
 
                ShowContributorDialog.prototype.addRow = function(tbody, data) {
                    var user = data.user;
                    var count = data.count;
                    var add = data.add;
                    var remove = data.remove;
                    tbody.append(
                        $("<tr />").append(
                            $("<td />").append(
                                $(`<a href="${mw.config.get("wgArticlePath").replace("$1", `User:${user}`)}" />`).text(user)
                            ),
                            `<td>${count}</td>`,
                            `<td>${add}</td>`,
                            `<td>${remove}</td>`
                        )
                    );
                 };
                 };
                 do {
 
                     o && (r.rvcontinue = o);
                 ShowContributorDialog.prototype.showContributors = function(contributors) {
                     try {
                     this.$tbody.empty();
                         const n = await t.get(r);
                     for (var user in contributors) {
                        o = n.continue?.rvcontinue;
                         if (contributors.hasOwnProperty(user)) {
                        for (const {
                            var edits = contributors[user];
                            user: t,
                            var add = edits.reduce(function(sum, delta) {
                             size: o
                                return delta > 0 ? sum + delta : sum;
                        } of Object.values(n.query.pages)[0].revisions)
                             }, 0);
                             e[t] ||= [],
                             var remove = edits.reduce(function(sum, delta) {
                                e[t].push(o - i),
                                 return delta < 0 ? sum + delta : sum;
                                 i = o
                            }, 0);
                    } catch (t) {
                            this.addRow(this.$tbody, {
                        mw.notify(`获取编辑记录失败:${t}`, {
                                user: user,
                             type: "error"
                                count: edits.length,
                         })
                                add: add,
                                remove: remove
                             });
                         }
                     }
                     }
                } while (o);
                    this.got = true;
                 return e
                 };
            };
 
            addRow = (t, {
                 var windowManager = new OO.ui.WindowManager({id:"show-contributor"});
                user: e,
                $(document.body).append(windowManager.$element);
                 count: o,
                 var dialog = new ShowContributorDialog({size:"large"});
                add: i,
                 windowManager.addWindows([dialog]);
                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({
                var button = new OO.ui.ButtonWidget({
            id: "show-contributor"
                    label:"本页贡献者",
        });
                    icon:"search",
        $(document.body).append(e.$element);
                    flags:"progressive",
                    id:"show-contributor-button"
                });


        const o = new t({
                var insertButton = function() {
            size: "large"
                    $("#show-contributor-button").detach();
        });
                    var skin = mw.config.get("skin");
        e.addWindows([o]);
                    if (skin === "citizen") {
                        if (window.innerWidth >= 1120) {
                            $(".page-actions").prepend(button.$element);
                        } else {
                            $(".firstHeading-container").append(button.$element);
                        }
                    } else if (skin === "vector-2022") {
                        $("header.mw-body-header.vector-page-titlebar").append(button.$element);
                    } else {
                        $("#bodyContent").prepend(button.$element);
                    }
                };


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


        // 插入按钮(统一插入 .page-actions,不动态判断屏幕宽度)
                // 仅在断点穿越时更新插入位置
        if ("citizen" === mw.config.get("skin")) {
                var isWide = window.innerWidth >= 1120;
            $(".page-actions").prepend(i.$element);
                $(window).on("resize", function() {
        } else {
                    clearTimeout(window.__showContributorResizeTimer);
            $("#bodyContent").prepend(i.$element);
                    window.__showContributorResizeTimer = setTimeout(function() {
        }
                        var nowWide = window.innerWidth >= 1120;
                        if (nowWide !== isWide) {
                            isWide = nowWide;
                            insertButton();
                        }
                    }, 200);
                });


        i.on("click", (async () => {
                // 点击事件
            if (!o.got) {
                button.on("click", function() {
                i.setLabel("正在查询");
                    (function() {
                const t = await o.getContributors();
                        if (!dialog.got) {
                o.showContributors(t),
                            button.setLabel("正在查询");
                    o.$table.tablesorter(),
                            dialog.getContributors().then(function(contributors) {
                    i.setLabel("本页贡献者")
                                dialog.showContributors(contributors);
            }
                                dialog.$table.tablesorter();
            e.openWindow(o)
                                button.setLabel("本页贡献者");
        }));
                                windowManager.openWindow(dialog);
    })()))
                            }).catch(function(error) {
                                console.error('获取贡献者数据失败:', error);
                                button.setLabel("本页贡献者");
                            });
                        } else {
                            windowManager.openWindow(dialog);
                        }
                    })();
                });
            });
        })();
    });
})();
})();

2025年6月17日 (二) 19:06的版本

// 原作者萌娘百科BearBin
//遵循BSD 3-Clause License使用代码
//全文请参见上方文档
//GitHub仓库地址:https://github.com/BearBin1215/MoegirlPedia
(function() {
    "use strict";
    $(function() {
        (function() {
            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.using([
                "mediawiki.api",
                "mediawiki.notification",
                "oojs-ui",
                "oojs-ui.styles.icons-interactions",
                "jquery.tablesorter"
            ]).then(function() {
                function ShowContributorDialog(config) {
                    OO.ui.Dialog.call(this, config);
                    this.$table = $('<table id="show-contributor-table" class="wikitable" />');
                    this.$tbody = $("<tbody />");
                    this.got = false;
                }
                OO.inheritClass(ShowContributorDialog, OO.ui.Dialog);
                ShowContributorDialog.static = ShowContributorDialog.static || {};
                ShowContributorDialog.static.name = "ShowContributor";
                ShowContributorDialog.static.tagName = "div";

                ShowContributorDialog.prototype.initialize = function() {
                    OO.ui.Dialog.prototype.initialize.call(this);
                    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", function() {
                                this.close();
                            }.bind(this))
                        ),
                        this.$table.append(
                            $("<thead><th>用户</th><th>编辑数</th><th>增加字节数</th><th>删减字节数</th></thead>"),
                            this.$tbody
                        )
                    );
                    return this;
                };

                ShowContributorDialog.prototype.getContributors = function() {
                    var self = this;
                    return new Promise(function(resolve, reject) {
                        var api = new mw.Api();
                        var contributors = {};
                        var continueToken = "";
                        var prevSize = 0;
                        var params = {
                            action: "query",
                            format: "json",
                            prop: "revisions",
                            titles: mw.config.get("wgPageName"),
                            rvprop: "user|size",
                            rvlimit: "max",
                            rvdir: "newer"
                        };
                        function fetchRevisions() {
                            if (continueToken) params.rvcontinue = continueToken;
                            api.get(params).then(function(response) {
                                continueToken = response.continue ? response.continue.rvcontinue : undefined;
                                var revisions = Object.values(response.query.pages)[0].revisions;
                                for (var i = 0; i < revisions.length; i++) {
                                    var user = revisions[i].user;
                                    var size = revisions[i].size;
                                    contributors[user] = contributors[user] || [];
                                    contributors[user].push(size - prevSize);
                                    prevSize = size;
                                }
                                if (continueToken) {
                                    fetchRevisions();
                                } else {
                                    resolve(contributors);
                                }
                            }).catch(function(error) {
                                mw.notify('获取编辑记录失败:' + error, { type: "error" });
                                reject(error);
                            });
                        }
                        fetchRevisions();
                    });
                };

                ShowContributorDialog.prototype.addRow = function(tbody, data) {
                    var user = data.user;
                    var count = data.count;
                    var add = data.add;
                    var remove = data.remove;
                    tbody.append(
                        $("<tr />").append(
                            $("<td />").append(
                                $(`<a href="${mw.config.get("wgArticlePath").replace("$1", `User:${user}`)}" />`).text(user)
                            ),
                            `<td>${count}</td>`,
                            `<td>${add}</td>`,
                            `<td>${remove}</td>`
                        )
                    );
                };

                ShowContributorDialog.prototype.showContributors = function(contributors) {
                    this.$tbody.empty();
                    for (var user in contributors) {
                        if (contributors.hasOwnProperty(user)) {
                            var edits = contributors[user];
                            var add = edits.reduce(function(sum, delta) {
                                return delta > 0 ? sum + delta : sum;
                            }, 0);
                            var remove = edits.reduce(function(sum, delta) {
                                return delta < 0 ? sum + delta : sum;
                            }, 0);
                            this.addRow(this.$tbody, {
                                user: user,
                                count: edits.length,
                                add: add,
                                remove: remove
                            });
                        }
                    }
                    this.got = true;
                };

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

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

                var insertButton = function() {
                    $("#show-contributor-button").detach();
                    var skin = mw.config.get("skin");
                    if (skin === "citizen") {
                        if (window.innerWidth >= 1120) {
                            $(".page-actions").prepend(button.$element);
                        } else {
                            $(".firstHeading-container").append(button.$element);
                        }
                    } else if (skin === "vector-2022") {
                        $("header.mw-body-header.vector-page-titlebar").append(button.$element);
                    } else {
                        $("#bodyContent").prepend(button.$element);
                    }
                };

                // 初始插入
                insertButton();

                // 仅在断点穿越时更新插入位置
                var isWide = window.innerWidth >= 1120;
                $(window).on("resize", function() {
                    clearTimeout(window.__showContributorResizeTimer);
                    window.__showContributorResizeTimer = setTimeout(function() {
                        var nowWide = window.innerWidth >= 1120;
                        if (nowWide !== isWide) {
                            isWide = nowWide;
                            insertButton();
                        }
                    }, 200);
                });

                // 点击事件
                button.on("click", function() {
                    (function() {
                        if (!dialog.got) {
                            button.setLabel("正在查询");
                            dialog.getContributors().then(function(contributors) {
                                dialog.showContributors(contributors);
                                dialog.$table.tablesorter();
                                button.setLabel("本页贡献者");
                                windowManager.openWindow(dialog);
                            }).catch(function(error) {
                                console.error('获取贡献者数据失败:', error);
                                button.setLabel("本页贡献者");
                            });
                        } else {
                            windowManager.openWindow(dialog);
                        }
                    })();
                });
            });
        })();
    });
})();