customnotifications

notifications

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/484331/1308737/customnotifications.js

  1. var AWN;
  2. (() => {
  3. var e = {
  4. 628: (e, t, s) => {
  5. "use strict";
  6. s.d(t, {
  7. default: () => m
  8. });
  9. const i = {
  10. maxNotifications: 10,
  11. animationDuration: 300,
  12. position: "bottom-right",
  13. labels: {
  14. tip: "Tip",
  15. info: "Info",
  16. success: "Success",
  17. warning: "Attention",
  18. alert: "Error",
  19. async: "Loading",
  20. confirm: "Confirmation required",
  21. confirmOk: "OK",
  22. confirmCancel: "Cancel"
  23. },
  24. icons: {
  25. tip: "question-circle",
  26. info: "info-circle",
  27. success: "check-circle",
  28. warning: "exclamation-circle",
  29. alert: "exclamation-triangle",
  30. async: "cog fa-spin",
  31. confirm: "exclamation-triangle",
  32. prefix: "<i class='fa fas fa-fw fa-",
  33. suffix: "'></i>",
  34. enabled: !0
  35. },
  36. replacements: {
  37. tip: null,
  38. info: null,
  39. success: null,
  40. warning: null,
  41. alert: null,
  42. async: null,
  43. "async-block": null,
  44. modal: null,
  45. confirm: null,
  46. general: {
  47. "<script>": "",
  48. "<\/script>": ""
  49. }
  50. },
  51. messages: {
  52. tip: "",
  53. info: "",
  54. success: "Action has been succeeded",
  55. warning: "",
  56. alert: "Action has been failed",
  57. confirm: "This action can't be undone. Continue?",
  58. async: "Please, wait...",
  59. "async-block": "Loading"
  60. },
  61. formatError(e) {
  62. if (e.response) {
  63. if (!e.response.data) return "500 API Server Error";
  64. if (e.response.data.errors) return e.response.data.errors.map((e => e.detail)).join("<br>");
  65. if (e.response.statusText) return `${e.response.status} ${e.response.statusText}: ${e.response.data}`
  66. }
  67. return e.message ? e.message : e
  68. },
  69. durations: {
  70. global: 5e3,
  71. success: null,
  72. info: null,
  73. tip: null,
  74. warning: null,
  75. alert: null
  76. },
  77. minDurations: {
  78. async: 1e3,
  79. "async-block": 1e3
  80. }
  81. };
  82. class n {
  83. constructor(e = {}, t = i) {
  84. Object.assign(this, this.defaultsDeep(t, e))
  85. }
  86. icon(e) {
  87. return this.icons.enabled ? `${this.icons.prefix}${this.icons[e]}${this.icons.suffix}` : ""
  88. }
  89. label(e) {
  90. return this.labels[e]
  91. }
  92. duration(e) {
  93. let t = this.durations[e];
  94. return null === t ? this.durations.global : t
  95. }
  96. toSecs(e) {
  97. return e / 1e3 + "s"
  98. }
  99. applyReplacements(e, t) {
  100. if (!e) return this.messages[t] || "";
  101. for (const s of ["general", t])
  102. if (this.replacements[s])
  103. for (const t in this.replacements[s]) e = e.replace(t, this.replacements[s][t]);
  104. return e
  105. }
  106. override(e) {
  107. return e ? new n(e, this) : this
  108. }
  109. defaultsDeep(e, t) {
  110. let s = {};
  111. for (const i in e) t.hasOwnProperty(i) ? s[i] = "object" == typeof e[i] && null !== e[i] ? this.defaultsDeep(e[i], t[i]) : t[i] : s[i] = e[i];
  112. return s
  113. }
  114. }
  115. const r = "awn-popup",
  116. a = "awn-toast",
  117. o = "awn-btn",
  118. l = "awn-confirm",
  119. c = {
  120. prefix: a,
  121. klass: {
  122. label: `${a}-label`,
  123. content: `${a}-content`,
  124. icon: `${a}-icon`,
  125. progressBar: `${a}-progress-bar`,
  126. progressBarPause: `${a}-progress-bar-paused`
  127. },
  128. ids: {
  129. container: `${a}-container`
  130. }
  131. },
  132. d = {
  133. prefix: r,
  134. klass: {
  135. buttons: "awn-buttons",
  136. button: o,
  137. successBtn: `${o}-success`,
  138. cancelBtn: `${o}-cancel`,
  139. title: `${r}-title`,
  140. body: `${r}-body`,
  141. content: `${r}-content`,
  142. dotAnimation: `${r}-loading-dots`
  143. },
  144. ids: {
  145. wrapper: `${r}-wrapper`,
  146. confirmOk: `${l}-ok`,
  147. confirmCancel: `${l}-cancel`
  148. }
  149. },
  150. h = {
  151. hiding: "awn-hiding"
  152. },
  153. u = class {
  154. constructor(e, t, s, i, n) {
  155. this.newNode = document.createElement("div"), t && (this.newNode.id = t), s && (this.newNode.className = s), i && (this.newNode.style.cssText = i), this.parent = e, this.options = n
  156. }
  157. beforeInsert() {}
  158. afterInsert() {}
  159. insert() {
  160. return this.beforeInsert(), this.el = this.parent.appendChild(this.newNode), this.afterInsert(), this
  161. }
  162. replace(e) {
  163. if (this.getElement()) return this.beforeDelete().then((() => (this.updateType(e.type), this.parent.replaceChild(e.newNode, this.el), this.el = this.getElement(e.newNode), this.afterInsert(), this)))
  164. }
  165. beforeDelete(e = this.el) {
  166. let t = 0;
  167. return this.start && (t = this.options.minDurations[this.type] + this.start - Date.now(), t < 0 && (t = 0)), new Promise((s => {
  168. setTimeout((() => {
  169. e.classList.add(h.hiding), setTimeout(s, this.options.animationDuration)
  170. }), t)
  171. }))
  172. }
  173. delete(e = this.el) {
  174. return this.getElement(e) ? this.beforeDelete(e).then((() => {
  175. e.remove(), this.afterDelete()
  176. })) : null
  177. }
  178. afterDelete() {}
  179. getElement(e = this.el) {
  180. return e ? document.getElementById(e.id) : null
  181. }
  182. addEvent(e, t) {
  183. this.el.addEventListener(e, t)
  184. }
  185. toggleClass(e) {
  186. this.el.classList.toggle(e)
  187. }
  188. updateType(e) {
  189. this.type = e, this.duration = this.options.duration(this.type)
  190. }
  191. },
  192. p = class extends u {
  193. constructor(e, t, s, i) {
  194. super(i, `${c.prefix}-${Math.floor(Date.now()-100*Math.random())}`, `${c.prefix} ${c.prefix}-${t}`, `animation-duration: ${s.toSecs(s.animationDuration)};`, s), this.updateType(t), this.setInnerHtml(e)
  195. }
  196. setInnerHtml(e) {
  197. "alert" === this.type && e && (e = this.options.formatError(e)), e = this.options.applyReplacements(e, this.type), this.newNode.innerHTML = `<div class="awn-toast-wrapper">${this.progressBar}${this.label}<div class="${c.klass.content}">${e}</div><span class="${c.klass.icon}">${this.options.icon(this.type)}</span></div>`
  198. }
  199. beforeInsert() {
  200. if (this.parent.childElementCount >= this.options.maxNotifications) {
  201. let e = Array.from(this.parent.getElementsByClassName(c.prefix));
  202. this.delete(e.find((e => !this.isDeleted(e))))
  203. }
  204. }
  205. afterInsert() {
  206. if ("async" == this.type) return this.start = Date.now();
  207. if (this.addEvent("click", (() => this.delete())), !(this.duration <= 0)) {
  208. this.timer = new class {
  209. constructor(e, t) {
  210. this.callback = e, this.remaining = t, this.resume()
  211. }
  212. pause() {
  213. this.paused = !0, window.clearTimeout(this.timerId), this.remaining -= new Date - this.start
  214. }
  215. resume() {
  216. this.paused = !1, this.start = new Date, window.clearTimeout(this.timerId), this.timerId = window.setTimeout((() => {
  217. window.clearTimeout(this.timerId), this.callback()
  218. }), this.remaining)
  219. }
  220. toggle() {
  221. this.paused ? this.resume() : this.pause()
  222. }
  223. }((() => this.delete()), this.duration);
  224. for (const e of ["mouseenter", "mouseleave"]) this.addEvent(e, (() => {
  225. this.isDeleted() || (this.toggleClass(c.klass.progressBarPause), this.timer.toggle())
  226. }))
  227. }
  228. }
  229. isDeleted(e = this.el) {
  230. return e.classList.contains(h.hiding)
  231. }
  232. get progressBar() {
  233. return this.duration <= 0 || "async" === this.type ? "" : `<div class='${c.klass.progressBar}' style="animation-duration:${this.options.toSecs(this.duration)};"></div>`
  234. }
  235. get label() {
  236. return `<b class="${c.klass.label}">${this.options.label(this.type)}</b>`
  237. }
  238. },
  239. f = class extends u {
  240. constructor(e, t = "modal", s, i, n) {
  241. let r = `animation-duration: ${s.toSecs(s.animationDuration)};`;
  242. super(document.body, d.ids.wrapper, null, r, s), this[d.ids.confirmOk] = i, this[d.ids.confirmCancel] = n, this.className = `${d.prefix}-${t}`, ["confirm", "async-block", "modal"].includes(t) || (t = "modal"), this.updateType(t), this.setInnerHtml(e), this.insert()
  243. }
  244. setInnerHtml(e) {
  245. let t = this.options.applyReplacements(e, this.type);
  246. switch (this.type) {
  247. case "confirm":
  248. let e = [`<button class='${d.klass.button} ${d.klass.successBtn}'id='${d.ids.confirmOk}'>${this.options.labels.confirmOk}</button>`];
  249. !1 !== this[d.ids.confirmCancel] && e.push(`<button class='${d.klass.button} ${d.klass.cancelBtn}'id='${d.ids.confirmCancel}'>${this.options.labels.confirmCancel}</button>`), t = `${this.options.icon(this.type)}<div class='${d.klass.title}'>${this.options.label(this.type)}</div><div class="${d.klass.content}">${t}</div><div class='${d.klass.buttons} ${d.klass.buttons}-${e.length}'>${e.join("")}</div>`;
  250. break;
  251. case "async-block":
  252. t = `${t}<div class="${d.klass.dotAnimation}"></div>`
  253. }
  254. this.newNode.innerHTML = `<div class="${d.klass.body} ${this.className}">${t}</div>`
  255. }
  256. keyupListener(e) {
  257. if ("async-block" === this.type) return e.preventDefault();
  258. switch (e.code) {
  259. case "Escape":
  260. e.preventDefault(), this.delete();
  261. case "Tab":
  262. if (e.preventDefault(), "confirm" !== this.type || !1 === this[d.ids.confirmCancel]) return !0;
  263. let t = this.okBtn;
  264. e.shiftKey ? document.activeElement.id == d.ids.confirmOk && (t = this.cancelBtn) : document.activeElement.id !== d.ids.confirmCancel && (t = this.cancelBtn), t.focus()
  265. }
  266. }
  267. afterInsert() {
  268. switch (this.listener = e => this.keyupListener(e), window.addEventListener("keydown", this.listener), this.type) {
  269. case "async-block":
  270. this.start = Date.now();
  271. break;
  272. case "confirm":
  273. this.okBtn.focus(), this.addEvent("click", (e => {
  274. if ("BUTTON" !== e.target.nodeName) return !1;
  275. this.delete(), this[e.target.id] && this[e.target.id]()
  276. }));
  277. break;
  278. default:
  279. document.activeElement.blur(), this.addEvent("click", (e => {
  280. e.target.id === this.newNode.id && this.delete()
  281. }))
  282. }
  283. }
  284. afterDelete() {
  285. window.removeEventListener("keydown", this.listener)
  286. }
  287. get okBtn() {
  288. return document.getElementById(d.ids.confirmOk)
  289. }
  290. get cancelBtn() {
  291. return document.getElementById(d.ids.confirmCancel)
  292. }
  293. };
  294. class m {
  295. constructor(e = {}) {
  296. this.options = new n(e)
  297. }
  298. tip(e, t) {
  299. return this._addToast(e, "tip", t).el
  300. }
  301. info(e, t) {
  302. return this._addToast(e, "info", t).el
  303. }
  304. success(e, t) {
  305. return this._addToast(e, "success", t).el
  306. }
  307. warning(e, t) {
  308. return this._addToast(e, "warning", t).el
  309. }
  310. alert(e, t) {
  311. return this._addToast(e, "alert", t).el
  312. }
  313. async (e, t, s, i, n) {
  314. let r = this._addToast(i, "async", n);
  315. return this._afterAsync(e, t, s, n, r)
  316. }
  317. confirm(e, t, s, i) {
  318. return this._addPopup(e, "confirm", i, t, s)
  319. }
  320. asyncBlock(e, t, s, i, n) {
  321. let r = this._addPopup(i, "async-block", n);
  322. return this._afterAsync(e, t, s, n, r)
  323. }
  324. modal(e, t, s) {
  325. return this._addPopup(e, t, s)
  326. }
  327. closeToasts() {
  328. let e = this.container;
  329. for (; e.firstChild;) e.removeChild(e.firstChild)
  330. }
  331. _addPopup(e, t, s, i, n) {
  332. return new f(e, t, this.options.override(s), i, n)
  333. }
  334. _addToast(e, t, s, i) {
  335. s = this.options.override(s);
  336. let n = new p(e, t, s, this.container);
  337. return i ? i instanceof f ? i.delete().then((() => n.insert())) : i.replace(n) : n.insert()
  338. }
  339. _afterAsync(e, t, s, i, n) {
  340. return e.then(this._responseHandler(t, "success", i, n), this._responseHandler(s, "alert", i, n))
  341. }
  342. _responseHandler(e, t, s, i) {
  343. return n => {
  344. switch (typeof e) {
  345. case "undefined":
  346. case "string":
  347. let r = "alert" === t ? e || n : e;
  348. this._addToast(r, t, s, i);
  349. break;
  350. default:
  351. i.delete().then((() => {
  352. e && e(n)
  353. }))
  354. }
  355. }
  356. }
  357. _createContainer() {
  358. return new u(document.body, c.ids.container, `awn-${this.options.position}`).insert().el
  359. }
  360. get container() {
  361. return document.getElementById(c.ids.container) || this._createContainer()
  362. }
  363. }
  364. },
  365. 612: (e, t, s) => {
  366. e.exports = s(628).default
  367. }
  368. },
  369. t = {};
  370.  
  371. function s(i) {
  372. var n = t[i];
  373. if (void 0 !== n) return n.exports;
  374. var r = t[i] = {
  375. exports: {}
  376. };
  377. return e[i](r, r.exports, s), r.exports
  378. }
  379. s.d = (e, t) => {
  380. for (var i in t) s.o(t, i) && !s.o(e, i) && Object.defineProperty(e, i, {
  381. enumerable: !0,
  382. get: t[i]
  383. })
  384. }, s.o = (e, t) => Object.prototype.hasOwnProperty.call(e, t);
  385. var i = s(612);
  386. AWN = i
  387. })();

QingJ © 2025

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