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

勤求古训,博采众方
中医百科移动页面Wiki/MediaWiki:Gadget-Util.jsMediaWiki:MediaWiki:Gadget-Util.js,不留重定向
中医百科移动页面MediaWiki:MediaWiki:Gadget-Util.jsMediaWiki:Gadget-Util.js,不留重定向
(没有差异)

2024年8月31日 (六) 14:39的版本

/**

* SPDX-License-Identifier: GPL-3.0-or-later
* _addText: 'Template:Gadget Header'
*
* @source {@link https://git.qiuwen.net.cn/InterfaceAdmin/QiuwenGadgets/src/branch/master/src/Util}
* @author 安忆 <i@anyi.in>
* @license GPL-3.0-or-later {@link https://www.qiuwenbaike.cn/wiki/H:GPL-3.0}
*/

/**

* Copyright (C)  安忆
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/**

* +------------------------------------------------------------+
* |            === 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 by opening an issue before editing. |
* +------------------------------------------------------------+
*/

/* <nowiki> */

(() => {

"use strict";

// dist/Util/Util.js function asyncGeneratorStep(n, t, e, r, o, a, c) {

 try {
   var i = n[a](c), u = i.value;
 } catch (n2) {
   return void e(n2);
 }
 i.done ? t(u) : Promise.resolve(u).then(r, o);

} function _asyncToGenerator(n) {

 return function() {
   var t = this, e = arguments;
   return new Promise(function(r, o) {
     var a = n.apply(t, e);
     function _next(n2) {
       asyncGeneratorStep(a, r, o, _next, _throw, "next", n2);
     }
     function _throw(n2) {
       asyncGeneratorStep(a, r, o, _next, _throw, "throw", n2);
     }
     _next(void 0);
   });
 };

} function _createForOfIteratorHelper(r, e) {

 var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
 if (!t) {
   if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
     t && (r = t);
     var n = 0, F = function() {
     };
     return { s: F, n: function() {
       return n >= r.length ? { done: true } : { done: false, value: r[n++] };
     }, e: function(r2) {
       throw r2;
     }, f: F };
   }
   throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
 }
 var o, a = true, u = false;
 return { s: function() {
   t = t.call(r);
 }, n: function() {
   var r2 = t.next();
   return a = r2.done, r2;
 }, e: function(r2) {
   u = true, o = r2;
 }, f: function() {
   try {
     a || null == t.return || t.return();
   } finally {
     if (u) throw o;
   }
 } };

} function _unsupportedIterableToArray(r, a) {

 if (r) {
   if ("string" == typeof r) return _arrayLikeToArray(r, a);
   var t = {}.toString.call(r).slice(8, -1);
   return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
 }

} function _arrayLikeToArray(r, a) {

 (null == a || a > r.length) && (a = r.length);
 for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
 return n;

} var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => {

 for (var name in all) __defProp(target, name, {
   get: all[name],
   enumerable: true
 });

}; var __copyProps = (to, from, except, desc) => {

 if (from && typeof from === "object" || typeof from === "function") {
   var _iterator = _createForOfIteratorHelper(__getOwnPropNames(from)), _step;
   try {
     for (_iterator.s(); !(_step = _iterator.n()).done; ) {
       let key = _step.value;
       if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
         get: () => from[key],
         enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
       });
     }
   } catch (err) {
     _iterator.e(err);
   } finally {
     _iterator.f();
   }
 }
 return to;

}; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(

 // If the importer is in node compatibility mode or this is not an ESM
 // file that has been converted to a CommonJS file using a Babel-
 // compatible transform (i.e. "__esModule" has not been set), then set
 // "default" to the CommonJS "module.exports" for node compatibility.
 isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
   value: mod,
   enumerable: true
 }) : target,
 mod

)); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", {

 value: true

}), mod); //! src/Util/Util.ts var Util_exports = {}; __export(Util_exports, {

 addEventListenerWithRemover: () => addEventListenerWithRemover,
 changeOpacityWhenMouseEnterOrLeave: () => changeOpacityWhenMouseEnterOrLeave,
 checkA11yConfirmKey: () => checkA11yConfirmKey,
 checkDependencies: () => checkDependencies,
 delay: () => delay,
 generateArray: () => generateArray,
 generateSvgDataUrl: () => generateSvgDataUrl,
 getBody: () => getBody,
 getWpSummaryContent: () => getWpSummaryContent,
 getWpTextbox1Content: () => getWpTextbox1Content,
 initMwApi: () => initMwApi,
 isValidKey: () => isValidKey,
 oouiConfirmWithStyle: () => oouiConfirmWithStyle,
 scrollTop: () => scrollTop,
 setWpSummaryContent: () => setWpSummaryContent,
 setWpTextbox1Content: () => setWpTextbox1Content,
 uniqueArray: () => uniqueArray,
 userIsInGroup: () => userIsInGroup

}); module.exports = __toCommonJS(Util_exports); //! src/Util/modules/addEventListenerWithRemover.ts var addEventListenerWithRemover = ({

 target,
 type,
 listener,
 options = {}

}) => {

 target.addEventListener(type, listener, options);
 return {
   remove: () => {
     target.removeEventListener(type, listener, options);
   }
 };

}; //! src/Util/modules/changeOpacityWhenMouseEnterOrLeave.ts var changeOpacityWhenMouseEnterOrLeave = (event, opacity = 0.7) => {

 event.currentTarget.style.opacity = event.type === "mouseenter" ? "1" : opacity.toString();

}; //! src/Util/modules/checkA11yConfirmKey.ts var checkA11yConfirmKey = (event) => {

 if (["click", "keydown"].includes(event.type)) {
   if (event.type === "keydown") {
     return ["Enter", " "].includes(event.key);
   }
   return true;
 }
 return false;

}; //! src/Util/modules/generateArray.ts function generateArray(...args) {

 return args.flatMap((arg) => {
   if (Array.isArray(arg)) {
     return arg;
   }
   if (arg instanceof NodeList) {
     return [...arg];
   }
   return [arg];
 });

} //! src/Util/modules/initMwApi.ts function initMwApi(userAgent, apiUri) {

 const apiOptions = {
   ajax: {
     headers: {
       "Api-User-Agent": userAgent ? "Qiuwen/1.1 (".concat(userAgent, ")") : "Qiuwen/1.1"
     }
   }
 };
 if (apiUri) {
   return new mw.ForeignApi(apiUri, apiOptions);
 }
 return new mw.Api(apiOptions);

} //! src/Util/modules/uniqueArray.ts var uniqueArray = function uniqueArray2(args) {

 /**
  * @see {@link https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array/922982}
  * @license CC-BY-SA-4.0
  */
 const result = [];
 var _iterator2 = _createForOfIteratorHelper(args), _step2;
 try {
   for (_iterator2.s(); !(_step2 = _iterator2.n()).done; ) {
     const item = _step2.value;
     if (!result.includes(item)) {
       result[result.length] = item;
     }
   }
 } catch (err) {
   _iterator2.e(err);
 } finally {
   _iterator2.f();
 }
 return result;

}; //! src/Util/modules/checkDependencies.ts var checkDependencies = /* @__PURE__ */ function() {

 var _ref = _asyncToGenerator(function* (gadgetNames) {
   const api = initMwApi("Util-CheckDependencies");
   gadgetNames = uniqueArray(generateArray(gadgetNames));
   var _iterator3 = _createForOfIteratorHelper(gadgetNames), _step3;
   try {
     for (_iterator3.s(); !(_step3 = _iterator3.n()).done; ) {
       const gadgetName = _step3.value;
       if (!mw.user.options.get("gadget-".concat(gadgetName))) {
         yield api.postWithEditToken({
           action: "options",
           change: "gadget-".concat(gadgetName, "=1")
         });
         yield mw.loader.using("ext.gadget.".concat(gadgetName));
       }
     }
   } catch (err) {
     _iterator3.e(err);
   } finally {
     _iterator3.f();
   }
 });
 return function checkDependencies2(_x) {
   return _ref.apply(this, arguments);
 };

}(); //! src/Util/modules/delay.ts var delay = (ms) => {

 return new Promise((resolve) => {
   setTimeout(resolve, ms);
 });

}; //! src/Util/modules/generateSvgDataUrl.ts var generateSvgDataUrl = (svg) => {

 /*!
  * SPDX-License-Identifier: MIT
  *
  * @file svg-to-data-uri.js {@link https://github.com/heyallan/svg-to-data-uri/}
  *
  * @author Allan Moreno
  * @license MIT {@link https://github.com/heyallan/svg-to-data-uri/blob/master/LICENSE}
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in all
  * copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
 svg = svg.trim();
 svg = svg.slice(svg.indexOf("<svg"));
 if (!svg.startsWith("<svg") || !svg.endsWith("svg>")) {
   return "";
 }
 if (!svg.includes("http://www.w3.org/2000/svg")) {
   svg = svg.replace(/<svg/g, "<svg xmlns='http://www.w3.org/2000/svg'");
 }
 svg = svg.replace(//g, "");
 svg = svg.replace(/version=["'](.{0,}?)["'](?=[\s>])/g, "");
 svg = svg.replace(/"'(.{1,})'"/g, "'$1'");
 svg = svg.replace(/"/g, "'");
 svg = svg.replace(/>\s{1,}</g, "><");
 svg = svg.replace(/\s{2,}/g, " ");
 svg = svg.trim();
 if (!svg.startsWith("<svg") || !svg.endsWith("svg>")) {
   return "";
 }
 svg = svg.replace(/&/g, "&");
 svg = svg.replace(/[%#<>?[\\\]^`{|}]/g, encodeURIComponent);
 svg = "data:image/svg+xml,".concat(svg);
 return svg;

}; //! src/Util/modules/getBody.ts var getBody = () => {

 return $.ready.then(() => {
   const $body = $("body");
   return $body;
 });

}; //! src/Util/modules/isValidKey.ts var isValidKey = (object, key) => {

 return key in object;

}; //! src/Util/modules/oouiConfirmWithStyle.tsx var import_ext_gadget = __toESM(require("ext.gadget.React"), 1); var oouiConfirmWithStyle = (message) => OO.ui.confirm($(/* @__PURE__ */ import_ext_gadget.default.createElement("div", {

 className: "oo-ui-window-foot",
 style: {
   border: ".1rem solid #0645ad",
   display: "flex",
   justifyContent: "space-evenly"
 }

}, /* @__PURE__ */ import_ext_gadget.default.createElement("span", {

 style: {
   fontSize: "1.2rem",
   fontWeight: "500",
   lineHeight: "1.8",
   padding: ".4em 0"
 }

}, message)))).then((isConfirm) => isConfirm); //! src/Util/modules/scrollTop.ts var scrollTop = (targetHeight, effectsOptionsOrDuration = {}) => {

 const options = typeof effectsOptionsOrDuration === "number" || typeof effectsOptionsOrDuration === "string" ? {
   duration: effectsOptionsOrDuration,
   easing: "linear"
 } : {
   duration: "slow",
   easing: "linear",
   ...effectsOptionsOrDuration
 };
 $(document).find("html, body").animate({
   scrollTop: targetHeight
 }, options);

}; //! src/Util/modules/userIsInGroup.ts var userIsInGroup = (groups) => {

 const {
   wgUserGroups,
   wgGlobalGroups
 } = mw.config.get();
 return [...wgUserGroups || [], ...wgGlobalGroups || []].some((element) => {
   return generateArray(groups).includes(element);
 });

}; //! src/Util/modules/wpSummaryContent.ts var getWpSummaryContent = ({

 $editForm

}) => {

 var _$editForm$find$val, _$editForm$find;
 return (_$editForm$find$val = (_$editForm$find = $editForm.find("#wpSummary")) === null || _$editForm$find === void 0 ? void 0 : _$editForm$find.val()) !== null && _$editForm$find$val !== void 0 ? _$editForm$find$val : "";

}; var setWpSummaryContent = ({

 $editForm,
 content

}) => {

 $editForm.find("#wpSummary").val(content);

}; //! src/Util/modules/wpTextbox1Content.ts var getWpTextbox1Content = ({

 $editForm

}) => {

 var _$editForm$find$textS;
 return (_$editForm$find$textS = $editForm.find("#wpTextbox1").textSelection("getContents")) !== null && _$editForm$find$textS !== void 0 ? _$editForm$find$textS : "";

}; var setWpTextbox1Content = ({

 $editForm,
 content

}) => {

 $editForm.find("#wpTextbox1").textSelection("setContents", content);

};

})();