toast.js

coco-message TheWindRises-2 js toast

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/426194/971661/toastjs.js

  1. // @original-script https://github.com/TheWindRises-2/coco-message
  2. // @original author TheWindRises-2
  3. // @license https://github.com/TheWindRises-2/coco-message/blob/main/LICENSE
  4. "use strict";
  5.  
  6. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  7.  
  8. !function (global, factory) {
  9. (typeof exports === "undefined" ? "undefined" : _typeof(exports)) === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = global || self, global.cocoMessage = factory());
  10. }(void 0, function () {
  11. "use strict";
  12.  
  13. if (typeof window === 'undefined') {
  14. return;
  15. }
  16.  
  17. var msgWrapper = c({
  18. className: "coco-msg-stage"
  19. });
  20.  
  21. function c(args, children) {
  22. var el = document.createElement("div");
  23.  
  24. for (var key in args) {
  25. var element = args[key];
  26.  
  27. if (key == "className") {
  28. key = "class";
  29. el.setAttribute(key, element);
  30. } else if (key[0] == "_") {
  31. el.addEventListener(key.slice(1), element);
  32. }
  33. }
  34.  
  35. if (typeof children == "string") {
  36. el.innerHTML = children;
  37. } else if (_typeof(children) == "object" && children.tagName) {
  38. el.appendChild(children);
  39. } else if (children) {
  40. for (var i = 0; i < children.length; i++) {
  41. var child = children[i];
  42. el.appendChild(child);
  43. }
  44. }
  45.  
  46. return el;
  47. }
  48.  
  49. function addAnimationEnd(el, fn) {
  50. ["a", "webkitA"].forEach(function (prefix) {
  51. var name = prefix + "nimationEnd";
  52. el.addEventListener(name, function () {
  53. fn();
  54. });
  55. });
  56. }
  57.  
  58. function css(el, css) {
  59. for (var key in css) {
  60. el.style[key] = css[key];
  61. }
  62.  
  63. if (el.getAttribute("style") === "") {
  64. el.removeAttribute("style");
  65. }
  66. }
  67.  
  68. function addClass(el, s) {
  69. var c = el.className || "";
  70.  
  71. if (!hasClass(c, s)) {
  72. var arr = c.split(/\s+/);
  73. arr.push(s);
  74. el.className = arr.join(" ");
  75. }
  76. }
  77.  
  78. function hasClass(c, s) {
  79. return c.indexOf(s) > -1 ? !0 : !1;
  80. }
  81.  
  82. function removeClass(el, s) {
  83. var c = el.className || "";
  84.  
  85. if (hasClass(c, s)) {
  86. var arr = c.split(/\s+/);
  87. var i = arr.indexOf(s);
  88. arr.splice(i, 1);
  89. el.className = arr.join(" ");
  90. }
  91.  
  92. if (el.className === "") {
  93. el.removeAttribute("class");
  94. }
  95. }
  96.  
  97. var initArgs = {
  98. msg: "",
  99. duration: 2000,
  100. showClose: false
  101. };
  102. var cocoMessage = {
  103. info: function info() {
  104. initConfig(arguments, "info");
  105. },
  106. success: function success() {
  107. initConfig(arguments, "success");
  108. },
  109. warning: function warning() {
  110. initConfig(arguments, "warning");
  111. },
  112. error: function error() {
  113. initConfig(arguments, "error");
  114. },
  115. loading: function loading() {
  116. return initConfig(arguments, "loading");
  117. },
  118. destroyAll: function destroyAll() {
  119. _destroyAll();
  120. },
  121. config: function config(obj) {
  122. for (var key in obj) {
  123. if (Object.hasOwnProperty.call(obj, key)) {
  124. if (obj[key] !== undefined) {
  125. initArgs[key] = obj[key];
  126. }
  127. }
  128. }
  129. }
  130. };
  131.  
  132. function initConfig(obj, type) {
  133. var args = {};
  134.  
  135. for (var key in initArgs) {
  136. args[key] = initArgs[key];
  137. }
  138.  
  139. for (var i = 0; i < obj.length; i++) {
  140. var it = obj[i];
  141.  
  142. if (it !== undefined) {
  143. if (typeof it == "string" || _typeof(it) == "object") {
  144. args.msg = it;
  145. } else if (typeof it == "boolean") {
  146. args.showClose = it;
  147. } else if (typeof it == "function") {
  148. args.onClose = it;
  149. } else if (typeof it == "number") {
  150. args.duration = it;
  151. }
  152. }
  153. }
  154.  
  155. args.type = type;
  156. return createMsgEl(args);
  157. }
  158.  
  159. function createMsgEl(args) {
  160. var type = args.type,
  161. duration = args.duration,
  162. msg = args.msg,
  163. showClose = args.showClose,
  164. onClose = args.onClose;
  165. var closable = duration === 0;
  166. var iconObj = getIconObj();
  167.  
  168. if (type == "loading") {
  169. msg = msg === "" ? "正在加载,请稍后" : msg;
  170. closable = showClose;
  171. duration = 0;
  172. }
  173.  
  174. var el = c({
  175. className: "coco-msg-wrapper"
  176. }, [c({
  177. className: "coco-msg coco-msg-fade-in ".concat(type)
  178. }, [c({
  179. className: "coco-msg-icon"
  180. }, iconObj[type]), c({
  181. className: "coco-msg-content"
  182. }, msg), c({
  183. className: "coco-msg-wait ".concat(closable ? "coco-msg-pointer" : ""),
  184. _click: function _click() {
  185. if (closable) {
  186. closeMsg(el, onClose);
  187. }
  188. }
  189. }, getMsgRight(closable))])]);
  190. var anm = el.querySelector(".coco-msg__circle");
  191.  
  192. if (anm && type !== "loading") {
  193. css(anm, {
  194. animation: "coco-msg__circle ".concat(duration, "ms linear")
  195. });
  196.  
  197. if ("onanimationend" in window) {
  198. addAnimationEnd(anm, function () {
  199. closeMsg(el, onClose);
  200. });
  201. } else {
  202. setTimeout(function () {
  203. closeMsg(el, onClose);
  204. }, duration);
  205. }
  206. }
  207.  
  208. if (type == "loading" && duration !== 0) {
  209. setTimeout(function () {
  210. closeMsg(el, onClose);
  211. }, duration);
  212. }
  213.  
  214. if (!msgWrapper.children.length) {
  215. document.body.appendChild(msgWrapper);
  216. }
  217.  
  218. msgWrapper.appendChild(el);
  219. css(el, {
  220. height: el.offsetHeight + "px"
  221. });
  222. setTimeout(function () {
  223. removeClass(el.children[0], "coco-msg-fade-in");
  224. }, 300);
  225.  
  226. if (type == "loading") {
  227. return function () {
  228. closeMsg(el, onClose);
  229. };
  230. }
  231. }
  232.  
  233. function getMsgRight(showClose) {
  234. if (showClose) {
  235. return "\n <svg class=\"coco-msg-close\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"5514\"><path d=\"M810 274l-238 238 238 238-60 60-238-238-238 238-60-60 238-238-238-238 60-60 238 238 238-238z\" p-id=\"5515\"></path></svg>\n ";
  236. } else {
  237. return "<svg class=\"coco-msg-progress\" viewBox=\"0 0 33.83098862 33.83098862\" xmlns=\"http://www.w3.org/2000/svg\">\n <circle class=\"coco-msg__background\" cx=\"16.9\" cy=\"16.9\" r=\"15.9\"></circle>\n <circle class=\"coco-msg__circle\" stroke-dasharray=\"100,100\" cx=\"16.9\" cy=\"16.9\" r=\"15.9\"></circle>\n </svg>\n ";
  238. }
  239. }
  240.  
  241. function closeMsg(el, cb) {
  242. if (!el) return;
  243. css(el, {
  244. padding: 0,
  245. height: 0
  246. });
  247. addClass(el.children[0], "coco-msg-fade-out");
  248. cb && cb();
  249. setTimeout(function () {
  250. if (!el) return;
  251. var has = false;
  252.  
  253. for (var i = 0; i < msgWrapper.children.length; i++) {
  254. if (msgWrapper.children[i] === el) {
  255. has = true;
  256. }
  257. }
  258.  
  259. has && removeChild(el);
  260. el = null;
  261.  
  262. if (!msgWrapper.children.length) {
  263. has && removeChild(msgWrapper);
  264. }
  265. }, 300);
  266. }
  267.  
  268. function getIconObj() {
  269. return {
  270. info: "\n <svg t=\"1609810636603\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"3250\"><path d=\"M469.333333 341.333333h85.333334v469.333334H469.333333z\" fill=\"#2980b9\" p-id=\"3251\"></path><path d=\"M469.333333 213.333333h85.333334v85.333334H469.333333z\" fill=\"#2980b9\" p-id=\"3252\"></path><path d=\"M384 341.333333h170.666667v85.333334H384z\" fill=\"#2980b9\" p-id=\"3253\"></path><path d=\"M384 725.333333h256v85.333334H384z\" fill=\"#2980b9\" p-id=\"3254\"></path></svg>\n ",
  271. success: "\n <svg t=\"1609781242911\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1807\"><path d=\"M455.42 731.04c-8.85 0-17.75-3.05-24.99-9.27L235.14 553.91c-16.06-13.81-17.89-38.03-4.09-54.09 13.81-16.06 38.03-17.89 54.09-4.09l195.29 167.86c16.06 13.81 17.89 38.03 4.09 54.09-7.58 8.83-18.31 13.36-29.1 13.36z\" p-id=\"1808\" fill=\"#ffffff\"></path><path d=\"M469.89 731.04c-8.51 0-17.07-2.82-24.18-8.6-16.43-13.37-18.92-37.53-5.55-53.96L734.1 307.11c13.37-16.44 37.53-18.92 53.96-5.55 16.43 13.37 18.92 37.53 5.55 53.96L499.67 716.89c-7.58 9.31-18.64 14.15-29.78 14.15z\" p-id=\"1809\" fill=\"#ffffff\"></path></svg>\n ",
  272. warning: "\n <svg t=\"1609776406944\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"18912\"><path d=\"M468.114286 621.714286c7.314286 21.942857 21.942857 36.571429 43.885714 36.571428s36.571429-14.628571 43.885714-36.571428L585.142857 219.428571c0-43.885714-36.571429-73.142857-73.142857-73.142857-43.885714 0-73.142857 36.571429-73.142857 80.457143l29.257143 394.971429zM512 731.428571c-43.885714 0-73.142857 29.257143-73.142857 73.142858s29.257143 73.142857 73.142857 73.142857 73.142857-29.257143 73.142857-73.142857-29.257143-73.142857-73.142857-73.142858z\" p-id=\"18913\" fill=\"#ffffff\"></path></svg>\n ",
  273. error: "\n <svg t=\"1609810716933\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"5514\"><path d=\"M810 274l-238 238 238 238-60 60-238-238-238 238-60-60 238-238-238-238 60-60 238 238 238-238z\" p-id=\"5515\" fill=\"#ffffff\"></path></svg>\n ",
  274. loading: "\n <div class=\"coco-msg_loading\">\n <svg class=\"coco-msg-circular\" viewBox=\"25 25 50 50\">\n <circle class=\"coco-msg-path\" cx=\"50\" cy=\"50\" r=\"20\" fill=\"none\" stroke-width=\"4\" stroke-miterlimit=\"10\"/>\n </svg>\n </div>\n "
  275. };
  276. }
  277.  
  278. function removeChild(el) {
  279. el && el.parentNode.removeChild(el);
  280. }
  281.  
  282. function _destroyAll() {
  283. for (var i = 0; i < msgWrapper.children.length; i++) {
  284. var element = msgWrapper.children[i];
  285. closeMsg(element);
  286. }
  287. }
  288.  
  289. window.addEventListener("DOMContentLoaded", function () {
  290. insertCssInHead();
  291. });
  292.  
  293. function insertCssInHead() {
  294. var doc = document;
  295.  
  296. if (doc && doc.head) {
  297. var head = doc.head;
  298.  
  299. var _css = doc.createElement("style");
  300.  
  301. var cssStr = "\n[class|=coco],[class|=coco]::after,[class|=coco]::before{box-sizing:border-box;outline:0}.coco-msg-progress{width:14px;height:14px}.coco-msg__circle{stroke-width:2;stroke-linecap:square;fill:none;transform:rotate(-90deg);transform-origin:center}.coco-msg-stage:hover .coco-msg__circle{-webkit-animation-play-state:paused!important;animation-play-state:paused!important}.coco-msg__background{stroke-width:2;fill:none}.coco-msg-stage{position:fixed;top:20px;left:50%;width:auto;transform:translate(-50%,0);z-index:3000}.coco-msg-wrapper{position:relative;left:50%;transform:translate(-50%,0);transition:height .25s ease-out,padding .25s ease-out;transition:height .35s ease-out,padding .35s ease-out;padding:8px 0;will-change:transform,opacity}.coco-msg-content,.coco-msg-icon,.coco-msg-wait{display:inline-block}.coco-msg-icon{position:relative;width:13px;height:13px;border-radius:100%;display:flex;justify-content:center;align-items:center;opacity:.8}.coco-msg-icon svg{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:11px;height:11px;box-sizing:content-box}.coco-msg-wait{width:20px;height:20px;position:relative}.coco-msg-wait svg{position:absolute;top:50%;right:-4px;transform:translate(0,-50%);fill:#b3b9b9}.coco-msg-close{width:16px;height:16px}.coco-msg-content{margin:0 10px;min-width:220px;text-align:left;font-size:14px;font-weight:400}.coco-msg.error .coco-msg-icon,.coco-msg.info .coco-msg-icon,.coco-msg.success .coco-msg-icon,.coco-msg.warning .coco-msg-icon{background-color:currentColor}.coco-msg{padding:13px 25px;border-radius:2px;position:relative;left:50%;transform:translate(-50%,0);display:flex;align-items:center}.coco-msg.info,.coco-msg.loading{color:#fff;background-color:#2980b9;box-shadow:0 0 1px 0 rgba(239,238,240,.3)}.coco-msg.success{color:#68c43b;background-color:#f0faeb;box-shadow:0 0 1px 0 rgba(145,194,126,.3)}.coco-msg.warning{color:#be820a;background-color:#faf4e1;box-shadow:0 0 1px 0 rgba(212,198,149,.3)}.coco-msg.error{color:#f74e60;background-color:#fee2e5;box-shadow:0 0 1px 0 rgba(218,163,163,.3)}.coco-msg.loading .coco-msg-icon{background-color:transparent}@keyframes coco-msg__circle{0%{stroke:#b3b9b9;stroke:currentColor}to{stroke:#b3b9b9;stroke:currentColor;stroke-dasharray:0 100}}.coco-msg_loading{flex-shrink:0;width:20px;height:20px;position:relative}.coco-msg-circular{-webkit-animation:coco-msg-rotate 2s linear infinite both;animation:coco-msg-rotate 2s linear infinite both;transform-origin:center center;height:18px!important;width:18px!important}.coco-msg-path{stroke-dasharray:1,200;stroke-dashoffset:0;stroke:currentColor;-webkit-animation:coco-msg-dash 1.5s ease-in-out infinite;animation:coco-msg-dash 1.5s ease-in-out infinite;stroke-linecap:round}@-webkit-keyframes coco-msg-rotate{100%{transform:translate(-50%,-50%) rotate(360deg)}}@keyframes coco-msg-rotate{100%{transform:translate(-50%,-50%) rotate(360deg)}}@-webkit-keyframes coco-msg-dash{0%{stroke-dasharray:1,200;stroke-dashoffset:0}50%{stroke-dasharray:89,200;stroke-dashoffset:-35px}100%{stroke-dasharray:89,200;stroke-dashoffset:-124px}}@keyframes coco-msg-dash{0%{stroke-dasharray:1,200;stroke-dashoffset:0}50%{stroke-dasharray:89,200;stroke-dashoffset:-35px}100%{stroke-dasharray:89,200;stroke-dashoffset:-124px}}.coco-msg-pointer{cursor:pointer}.coco-msg-fade-in{-webkit-animation:coco-msg-fade .22s ease-out both;animation:coco-msg-fade .22s ease-out both}.coco-msg-fade-out{animation:coco-msg-fade .22s linear reverse both}@-webkit-keyframes coco-msg-fade{0%{opacity:0;transform:translate(-50%,0)}to{opacity:1;transform:translate(-50%,0)}}@keyframes coco-msg-fade{0%{opacity:0;transform:translate(-50%,-80%)}to{opacity:1;transform:translate(-50%,0)}}\n ";
  302. _css.innerHTML = cssStr;
  303.  
  304. if (head.children.length) {
  305. head.insertBefore(_css, head.children[0]);
  306. } else {
  307. head.appendChild(_css);
  308. }
  309. }
  310. }
  311.  
  312. return cocoMessage;
  313. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址