H2P: notify util

通知栏

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/411280/847405/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 style_notify = document.createElement('style');
  14. style_notify.id = 'h2p-style-notify';
  15. style_notify.innerHTML = `
  16. #h2p-div-notify-container {
  17. position: fixed;
  18. width: 280px;
  19. bottom: 20px;
  20. right: 20px;
  21. overflow: hidden;
  22. z-index: 9999;
  23. }
  24.  
  25. .h2p-div-notify-item {
  26. position: relative;
  27. width: 250px;
  28. height: 25px;
  29. right: -280px;
  30. padding: 9px 13px;
  31. margin: 6px 0;
  32. border: 1px solid;
  33. border-radius: 5px;
  34. display: flex;
  35. align-items: center;
  36. transition: left 1.5s, right 1.5s;
  37. }
  38.  
  39. .h2p-div-notify-item-in {
  40. right: 0;
  41. }
  42.  
  43. .h2p-div-notify-close {
  44. position: absolute;
  45. top: 15px;
  46. right: 20px;
  47. cursor: pointer;
  48. }
  49. .h2p-div-notify-close::before, .h2p-div-notify-close::after {
  50. position: absolute;
  51. content: '';
  52. width: 12px;
  53. height: 2px;
  54. background: chocolate;
  55. }
  56. .h2p-div-notify-close::before {
  57. transform: rotate(45deg);
  58. }
  59. .h2p-div-notify-close::after {
  60. transform: rotate(-45deg);
  61. }
  62. `;
  63. document.body.appendChild(style_notify);
  64. const div_notify = document.createElement('div');
  65. div_notify.id = 'h2p-div-notify-container';
  66. document.body.appendChild(div_notify);
  67.  
  68. w.$notifyMgr = (() => {
  69. const Notify = function() {
  70. this.type = {
  71. default: {
  72. bgColor: '#e6ffff',
  73. bdColor: '#23bdd9'
  74. },
  75. success: {
  76. bgColor: '#f6ffec',
  77. bdColor: '#53752d'
  78. },
  79. warn: {
  80. bgColor: '#fefbe6',
  81. bdColor: '#fdc446'
  82. },
  83. error: {
  84. bgColor: '#fff0ef',
  85. bdColor: '#e75252'
  86. }
  87. }
  88.  
  89. this.createNotify = ({ msg = '', type = notifyType.default, autoClose = true }) => {
  90. const ran = Math.floor(Math.random() * 100000000);
  91. const div_notify_item = document.createElement('div');
  92. div_notify_item.id = `h2p-div-notify-${ran}`;
  93. div_notify_item.classList.add('h2p-div-notify-item');
  94. div_notify_item.style.backgroundColor = type.bgColor;
  95. div_notify_item.style.borderColor = type.bdColor;
  96. div_notify_item.innerHTML = msg;
  97. $H2P('div#h2p-div-notify-container').appendChild(div_notify_item);
  98.  
  99. const div_notify_item_close = document.createElement('div');
  100. div_notify_item_close.id = `h2p-div-notify-close-${ran}`;
  101. div_notify_item_close.classList.add('h2p-div-notify-close');
  102. div_notify_item_close.addEventListener('click', (e) => { this.closeNotify(`h2p-div-notify-${e.target.id.match(/[a-zA-Z\-]*(\d+)[a-zA-Z\-]*/g)[1]}`); })
  103. $H2P('div#h2p-div-notify-container').appendChild(div_notify_item_close);
  104.  
  105. setTimeout((id) => {
  106. // 显示通知栏
  107. $H2P(`#${id}`).classList.add('h2p-div-notify-item-in');
  108. autoClose && setTimeout(this.closeNotify, 4000, id);
  109. }, 100, div_notify_item.id);
  110. }
  111.  
  112. this.closeNotify = (id = '') => {
  113. $H2P(`#${id}`).classList.remove('h2p-div-notify-item-in');
  114. setTimeout(() => {
  115. $H2P('div#h2p-div-notify-container').removeChild($H2P(`#${id}`));
  116. }, 1500);
  117. }
  118. }
  119. return new Notify();
  120. })();
  121. })(window);