H2P: notify util

通知栏

当前为 2020-09-13 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/411280/847417/H2P%3A%20notify%20util.js

  1. // ==UserScript==
  2. // @name H2P: notify util
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.1
  5. // @description 通知栏
  6. // @author H2P
  7. // @compatible chrome
  8. // ==/UserScript==
  9.  
  10. ((w) => {
  11. 'use strict';
  12.  
  13. const $H2P = (xpath = 'body', queryOneElement = true) => queryOneElement ? document.querySelector(xpath) : Array.from(document.querySelectorAll(xpath));
  14.  
  15. const style_notify = document.createElement('style');
  16. style_notify.id = 'h2p-style-notify';
  17. style_notify.innerHTML = `
  18. #h2p-div-notify-container {
  19. position: fixed;
  20. width: 280px;
  21. bottom: 20px;
  22. right: 20px;
  23. overflow: hidden;
  24. z-index: 9999;
  25. }
  26.  
  27. .h2p-div-notify-item-container {
  28. position: relative;
  29. width: 250px;
  30. height: 25px;
  31. right: -280px;
  32. padding: 9px 13px;
  33. margin: 6px 0;
  34. border: 1px solid;
  35. border-radius: 5px;
  36. display: flex;
  37. align-items: center;
  38. transition: left 1.5s, right 1.5s;
  39. }
  40.  
  41. .h2p-div-notify-item-container-in {
  42. right: 0;
  43. }
  44.  
  45. .h2p-div-notify-item {
  46. width: 235px;
  47. height: 25px;
  48. margin-right: 5px;
  49. display: flex;
  50. align-items: center;
  51. }
  52.  
  53. .h2p-div-notify-close {
  54. cursor: pointer;
  55. }
  56. .h2p-div-notify-close::before, .h2p-div-notify-close::after {
  57. position: absolute;
  58. content: '';
  59. width: 12px;
  60. height: 2px;
  61. background: chocolate;
  62. }
  63. .h2p-div-notify-close::before {
  64. transform: rotate(45deg);
  65. }
  66. .h2p-div-notify-close::after {
  67. transform: rotate(-45deg);
  68. }
  69. `;
  70. document.body.appendChild(style_notify);
  71. const div_notify = document.createElement('div');
  72. div_notify.id = 'h2p-div-notify-container';
  73. document.body.appendChild(div_notify);
  74.  
  75. w.$notifyMgr = (() => {
  76. const Notify = function() {
  77. this.type = {
  78. default: {
  79. bgColor: '#e6ffff',
  80. bdColor: '#23bdd9'
  81. },
  82. success: {
  83. bgColor: '#f6ffec',
  84. bdColor: '#53752d'
  85. },
  86. warn: {
  87. bgColor: '#fefbe6',
  88. bdColor: '#fdc446'
  89. },
  90. error: {
  91. bgColor: '#fff0ef',
  92. bdColor: '#e75252'
  93. }
  94. }
  95.  
  96. this.createNotify = ({ msg = '', type = notifyType.default, autoClose = true }) => {
  97. const ran = Math.floor(Math.random() * 100000000);
  98. const div_notify_item = document.createElement('div');
  99. div_notify_item.id = `h2p-div-notify-${ran}`;
  100. div_notify_item.classList.add('h2p-div-notify-item-container');
  101. div_notify_item.style.backgroundColor = type.bgColor;
  102. div_notify_item.style.borderColor = type.bdColor;
  103. div_notify_item.innerHTML = `
  104. <div class="h2p-div-notify-item">${msg}</div>
  105. `;
  106.  
  107. const div_notify_item_close = document.createElement('div');
  108. div_notify_item_close.id = `h2p-div-notify-close-${ran}`;
  109. div_notify_item_close.classList.add('h2p-div-notify-close');
  110. div_notify_item_close.addEventListener('click', (e) => { this.closeNotify(`h2p-div-notify-${e.currentTarget.id.match(/[a-zA-Z\-]*(\d+)[a-zA-Z\-]*/g)[1]}`); });
  111. div_notify_item.appendChild(div_notify_item_close);
  112.  
  113. $H2P('div#h2p-div-notify-container').appendChild(div_notify_item);
  114.  
  115. setTimeout((id) => {
  116. // 显示通知栏
  117. $H2P(`#${id}`).classList.add('h2p-div-notify-item-container-in');
  118. autoClose && setTimeout(this.closeNotify, 4000, id);
  119. }, 100, div_notify_item.id);
  120. }
  121.  
  122. this.closeNotify = (id = '') => {
  123. $H2P(`#${id}`).classList.remove('h2p-div-notify-item-container-in');
  124. setTimeout(() => {
  125. $H2P('div#h2p-div-notify-container').removeChild($H2P(`#${id}`));
  126. }, 1500);
  127. }
  128. }
  129. return new Notify();
  130. })();
  131. })(window);