MediaWiki:Gadget-ShowContributors.js
更多操作
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
BSD 3-Clause License |
---|
BSD 3-Clause License Copyright (c) 2023, BearBin Redistribution and use in source and binary forms, with or without
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")) || 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; } /* Citizen 样式调整 */ .skin-citizen #show-contributor-button { display: inline-flex; 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([ "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 />"); $body; 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><tr><th>用户</th><th>编辑数</th><th>增加字节数</th><th>删减字节数</th></tr></thead>"), this.$tbody ) ); return this; } getContributors = async () => { 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 data = await api.get(params); rvcontinue = data.continue?.rvcontinue; const revisions = Object.values(data.query.pages)[0].revisions; for (const { user, size } of revisions) { users[user] ||= []; users[user].push(size - lastSize); lastSize = size; } } catch (err) { mw.notify(`获取编辑记录失败:${err}`, { type: "error" }); break; } } while (rvcontinue); return users; }; addRow = (tbody, { user, count, add, remove }) => { tbody.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 count = data[user].length; const add = data[user].reduce((t, v) => v > 0 ? t + v : t, 0); const remove = data[user].reduce((t, v) => v < 0 ? t + v : t, 0); this.addRow(this.$tbody, { user, count, add, remove }); } this.got = true; }; } 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 skin = mw.config.get("skin"); const width = window.innerWidth; if (skin === "citizen") { if (width <= 1119) { $(".citizen-page-heading").append(button.$element); } else if (width >= 1200) { $(".page-actions").prepend(button.$element); } else { console.warn("宽度不在插入按钮的设定范围内。"); } } else { $("#bodyContent").prepend(button.$element); } 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); }); })()); })();