Greasy Fork is available in English.

Notify Library

Very Simple JS Notifications Library

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greatest.deepsurf.us/scripts/401626/1109786/Notify%20Library.js

  1. // ==UserScript==
  2. // @version 2.0
  3. // @license MIT
  4. // ==/UserScript==
  5.  
  6. /* **************
  7.  
  8. Notify Library
  9. Developed by pizidavi
  10.  
  11. Options:
  12. - text required
  13. - type optional
  14. - success
  15. - info/information
  16. - warn/warning
  17. - error
  18. - timeout optional (default: 5s)
  19.  
  20. ************** */
  21.  
  22. var Notify = function Notify(options) {
  23. var _this = this;
  24. const CSS = '#notify { position: fixed; top: 0; right: 0; z-index: 9999; } #notify > div { display: none; position: relative; width: 300px; padding: .5em .8em; margin: 1em 1em 0 0; border-radius: 2px; background-color: white; color: #2c3e50; font-size: 17px; cursor: pointer; transition: .4s ease-in; }';
  25. const TYPES = {
  26. 'success': '#2ecc71',
  27. 'info': '#3498db',
  28. 'information': '#3498db',
  29. 'warn': '#f9ca24',
  30. 'warning': '#f9ca24',
  31. 'error': '#e74c3c',
  32. };
  33.  
  34. if (!document.querySelectorAll('#notify').length) {
  35. const style = document.createElement('style');
  36. style.innerText = CSS;
  37. const div = document.createElement('div');
  38. div.id = 'notify';
  39. div.append(style);
  40.  
  41. document.querySelector('body').append(div);
  42. }
  43. const template = document.createElement('div');
  44. template.append(document.createElement('span'))
  45.  
  46. _this.options = options;
  47. _this.container = document.querySelector('#notify');
  48. _this.template = template;
  49.  
  50. if (!_this.options || typeof _this.options != 'object') {
  51. throw 'Options required';
  52. }
  53. if (!_this.options.text) {
  54. throw 'Options TEXT must not be empty';
  55. }
  56.  
  57. if (_this.options.type) {
  58. const background = TYPES[_this.options.type];
  59. if (background) {
  60. _this.template.style.backgroundColor = background;
  61. _this.template.style.color = 'white';
  62. } else {
  63. throw 'Type not found';
  64. }
  65. }
  66.  
  67. _this.id = 'noty_' + Math.random().toString(36).substring(2);
  68. _this.template.setAttribute('id', _this.id);
  69. _this.template.querySelector('span').textContent = _this.options.text;
  70. _this.template.style.right = '-110%';
  71. _this.template.addEventListener('click', function() {
  72. _this.close();
  73. });
  74.  
  75. };
  76.  
  77. Notify.prototype.show = function () {
  78. var _this = this;
  79.  
  80. if (!_this.container.querySelector('#'+_this.id)) {
  81. _this.container.prepend(_this.template); }
  82.  
  83. _this.template.style.display = 'block';
  84. setTimeout(function() {
  85. _this.template.style.right = '0';
  86. }, 50);
  87.  
  88. if (_this.options.timeout !== false) {
  89. clearTimeout(_this.closeTime);
  90. _this.closeTime = setTimeout(function() {
  91. _this.close();
  92. }, (_this.options.timeout || 5000));
  93. }
  94.  
  95. return _this;
  96. };
  97.  
  98. Notify.prototype.close = function () {
  99. var _this = this;
  100.  
  101. if (!_this.container.querySelector('#'+_this.id)) return;
  102.  
  103. clearTimeout(_this.closeTime);
  104. _this.template.style.right = '-110%';
  105.  
  106. setTimeout(function() {
  107. _this.template.remove();
  108. }, 450);
  109.  
  110. return _this;
  111. };