From a5d41956f4d2b3d72d54121d3a81a588e60f795f Mon Sep 17 00:00:00 2001 From: ZoruaFox <96456728+ZoruaFox@users.noreply.github.com> Date: Sat, 7 Oct 2023 08:46:00 +0800 Subject: [PATCH] refactor(WhoIsActive.js):pull --- src/Gadgets/SysOp/WhoIsActive/WhoIsActive.js | 119 ++++++++++++++----- 1 file changed, 86 insertions(+), 33 deletions(-) diff --git a/src/Gadgets/SysOp/WhoIsActive/WhoIsActive.js b/src/Gadgets/SysOp/WhoIsActive/WhoIsActive.js index 027f6d7..333eb6d 100644 --- a/src/Gadgets/SysOp/WhoIsActive/WhoIsActive.js +++ b/src/Gadgets/SysOp/WhoIsActive/WhoIsActive.js @@ -1,20 +1,44 @@ /** - * SPDX-License-Identifier: CC BY-SA-4.0 + * SPDX-License-Identifier: CC-BY-SA-4.0 * _addText: '{{Gadget Header|license=CC-BY-SA-4.0}}' * + * @base * @base * @source * @dependency ext.gadget.i18n, mediawiki.api */ +/** + * +--------------------------------------------------------+ + * | === WARNING: GLOBAL GADGET FILE === | + * +--------------------------------------------------------+ + * | All changes should be made in the repository, | + * | otherwise they will be lost. | + * +--------------------------------------------------------+ + * | Changes to this page may affect many users. | + * | Please discuss changes at talk page before editing. | + * +--------------------------------------------------------+ + */ /* */ -'use strict'; - +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } +function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } +function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } $(function whoIsActive() { if (mw.config.get('wgNamespaceNumber') < 0) { return; } + var api = new mw.Api({ + ajax: { + headers: { + 'Api-User-Agent': "YsArxiv/1.1 (WhoIsActive/1.1; ".concat(mw.config.get('wgWikiID'), ")") + } + } + }); var filteredLinks = []; - var localizedUserNamespace = mw.config.get('wgFormattedNamespaces')[2]; + var _mw$config$get = mw.config.get('wgFormattedNamespaces'), + localizedUserNamespace = _mw$config$get[2]; $('.mw-body-content').find("a[title^=\"User:\"]:not(.mw-changeslist-date):not([href*=\"undo\"]), a[title^=\"".concat(localizedUserNamespace, ":\"]:not(.mw-changeslist-date):not([href*=\"undo\"])")).each(function (_index, element) { var _link$attr; var link = $(element); @@ -35,72 +59,101 @@ $(function whoIsActive() { return; } var i18nMessages = function i18nMessages() { - var localize = window.i18n.localize; + var _i18n = i18n, + localize = _i18n.localize; return { - thisweek: localize({ - en: 'Edited thismonthly', + OverAYear: localize({ + en: 'Edited over a year ago', + 'zh-hans': '一年未有编辑', + 'zh-hant': '一年未有編輯' + }), + ThisWeek: localize({ + en: 'Edited this week', 'zh-hans': '一周内有编辑', 'zh-hant': '一周內有編輯' }), - thismonth: localize({ - en: 'Edited thismonthly', + ThisMonth: localize({ + en: 'Edited this month', 'zh-hans': '一月内有编辑', 'zh-hant': '一月內有編輯' }), - thisyear: localize({ + ThisYear: localize({ en: 'Edited this year', 'zh-hans': '一年内有编辑', 'zh-hant': '一年內有編輯' - }), - overayear: localize({ - en: 'Edited over a year ago', - 'zh-hans': '一年未有编辑', - 'zh-hant': '一年未有編輯' }) }; }; - var _messages = i18nMessages(); - var messages = function messages(key) { - return _messages[key] || key; + var messages = i18nMessages(); + var message = function message(key) { + return messages[key] || key; }; - var getLastActiveMarker = function getLastActiveMarker(timestamp) { + var getLastActiveMarker = function getLastActiveMarker(timestamp, indicator) { var date = Date.parse(timestamp); var now = Date.now(); var diff = Math.floor((now - date) / (1000 * 60 * 60 * 24)); var timespan; if (diff > 365) { - timespan = 'overayear'; + timespan = 'OverAYear'; } else if (diff > 30) { - timespan = 'thisyear'; + timespan = 'ThisYear'; } else if (diff > 7) { - timespan = 'thismonth'; + timespan = 'ThisMonth'; } else { - timespan = 'thisweek'; + timespan = 'ThisWeek'; } - var iconPath = "".concat(mw.config.get('wgServer') + mw.config.get('wgScriptPath'), "/resources/lib/ooui/themes/wikimediaui/images/icons/userContributions-ltr.svg"); - var marker = "") + " ").concat(messages(timespan), ""); - return $(marker); + var elementName = indicator === true ? 'div' : mw.config.get('skin') === 'citizen' ? 'section' : ['gongbi'].indexOf(mw.config.get('skin')) !== -1 ? 'li' : 'div'; + // The following classes are used here: + // * mw-whoisactive-OverAYear + // * mw-whoisactive-ThisYear + // * mw-whoisactive-ThisMonth + // * mw-whoisactive-ThisWeek + var $icon = $("<".concat(elementName, ">")).addClass("mw-whoisactive-span mw-whoisactive-".concat(timespan)).append($('').addClass("mw-whoisactive-icon mw-whoisactive-icon-".concat(timespan)).attr({ + alt: message(timespan), + title: message(timespan) + })).append($('').addClass("mw-whoisactive-text".concat(indicator === true ? ' mw-whoisactive-notext' : '')).text(message(timespan))); + return $icon; }; var _loop = function _loop() { var item = _filteredLinks[_i]; var username = item.username; var element = item.element; - var api = new mw.Api(); - api.get({ - format: 'json', + var params = { action: 'query', list: 'usercontribs', - uclimit: '1', + uclimit: 1, ucuser: username - }).then(function (result) { + }; + api.get(params).then(function (result) { if (result['query'].usercontribs.length > 0) { - var timestamp = result['query'].usercontribs[0].timestamp; - getLastActiveMarker(timestamp).insertAfter(element); + var _result$query$usercon = _slicedToArray(result['query'].usercontribs, 1), + timestamp = _result$query$usercon[0].timestamp; + getLastActiveMarker(timestamp, true).insertAfter(element); } }); }; for (var _i = 0, _filteredLinks = filteredLinks; _i < _filteredLinks.length; _i++) { _loop(); } + var wgRelevantUserName = mw.config.get('wgRelevantUserName'); + if (wgRelevantUserName && mw.config.get('wgNamespaceNumber') === 2 && mw.config.get('wgAction') === 'view') { + var relevantUserPageName = new mw.Title(wgRelevantUserName, 2).toText(); + var pageName = new mw.Title(mw.config.get('wgPageName')).toText(); + if (relevantUserPageName === pageName) { + var params = { + action: 'query', + list: 'usercontribs', + uclimit: 1, + ucuser: wgRelevantUserName + }; + api.get(params).then(function (result) { + if (result['query'].usercontribs.length > 0) { + var _result$query$usercon2 = _slicedToArray(result['query'].usercontribs, 1), + timestamp = _result$query$usercon2[0].timestamp; + getLastActiveMarker(timestamp, false).prependTo($('#footer-info, .page-info')); + } + }); + } + } }); /* */