H2P: utils

utils

目前为 2020-09-13 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/411278/847392/H2P%3A%20utils.js

  1. // ==UserScript==
  2. // @name H2P: utils
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.6
  5. // @icon http://www.douyutv.com/favicon.ico
  6. // @description utils
  7. // @author H2P
  8. // @compatible chrome
  9. // ==/UserScript==
  10.  
  11. ((w) => {
  12. 'use strict';
  13.  
  14. /**
  15. * 在字符串前(后)添加 0
  16. * @param {String} s
  17. * @param {Number} len
  18. * @param {Boolean} isAddFront
  19. */
  20. function add0(s = '', len = 0, isAddFront = true) {
  21. s = s.toString();
  22. while (s.length < len) { s = isAddFront ? '0' + s : s + '0'; }
  23. return s;
  24. }
  25.  
  26. w.$util = (() => {
  27. function Util() {
  28. /**
  29. * 返回毫秒
  30. * @param {Number} num
  31. */
  32. this.timeMS = (num = 0) => {
  33. num = Number.parseInt(num);
  34. return num < 946684800000 ? num * 1000 : num;
  35. }
  36. /**
  37. * localStorage 相关操作
  38. */
  39. function LS() {
  40. this.init = (itemKey = '', itemPre = {}) => {
  41. let item = Object.assign({}, itemPre, this.get(itemKey));
  42. for (let key in item) { if (!(key in itemPre)) { delete item[key]; } }
  43. localStorage.removeItem(itemKey);
  44. localStorage.setItem(itemKey, JSON.stringify(item));
  45. return item;
  46. }
  47. this.set = (itemKey = '', item = {}) => { localStorage.setItem(itemKey, JSON.stringify(item)); },
  48. this.get = (itemKey = '') => JSON.parse(localStorage.getItem(itemKey)) || {},
  49. this.remove = (itemKey = '') => { localStorage.removeItem(itemKey); }
  50. }
  51. this.LS = new LS();
  52. this.HMS = (time = 0) => {
  53. time = this.timeMS(time);
  54. let h = Number.parseInt(time / 3600000);
  55. let m = Number.parseInt(time % 3600000 / 60000);
  56. let s = Number.parseInt(time % 3600000 % 60000 / 1000);
  57. return {
  58. h: add0(h, 2),
  59. m: add0(m, 2),
  60. s: add0(s, 2),
  61. }
  62. }
  63. }
  64. return new Util();
  65. })();
  66.  
  67. // return millisecond
  68. Date.prototype.$timems = Date.prototype.getTime;
  69. // return second
  70. Date.prototype.$times = function() { return Number.parseInt(this.getTime() / 1000); }
  71. // format time: yyyy-MM-dd hh-mm-ss
  72. Date.prototype.$formatTime = function() { return `${this.getFullYear()}-${add0(this.getMonth() + 1, 2)}-${add0(this.getDate(), 2)} ${add0(this.getHours(), 2)}:${add0(this.getMinutes(), 2)}:${add0(this.getSeconds(), 2)}`; }
  73. // format date: yyyy-MM-dd
  74. Date.prototype.$formatDate = function() { return `${this.getFullYear()}-${add0(this.getMonth() + 1, 2)}-${add0(this.getDate(), 2)}`; }
  75.  
  76. /**
  77. * 根据 xpath 查询元素
  78. * @param {String} xpath
  79. * @param {Boolean} queryOneElement
  80. */
  81. w.$H2P = (xpath = 'body', queryOneElement = true) => queryOneElement ? document.querySelector(xpath) : Array.from(document.querySelectorAll(xpath));
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
  90. //
  91. // 通知栏
  92. //
  93. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
  94.  
  95. const notifyType = {
  96. default: {
  97. bgColor: '#e6ffff',
  98. bdColor: '#23bdd9'
  99. },
  100. success: {
  101. bgColor: '#f6ffec',
  102. bdColor: '#53752d'
  103. },
  104. warn: {
  105. bgColor: '#fefbe6',
  106. bdColor: '#fdc446'
  107. },
  108. error: {
  109. bgColor: '#fff0ef',
  110. bdColor: '#e75252'
  111. }
  112. }
  113.  
  114. w.$notifyMgr = (() => {
  115. const style_notify = document.createElement('style');
  116. style_notify.id = 'h2p-style-notify';
  117. style_notify.innerHTML = `
  118. #h2p-div-notify-container {
  119. position: fixed;
  120. width: 280px;
  121. bottom: 20px;
  122. left: 20px;
  123. overflow: hidden;
  124. z-index: 9999;
  125. }
  126.  
  127. .h2p-div-notify-item {
  128. position: relative;
  129. width: 250px;
  130. height: 25px;
  131. right: -280px;
  132. padding: 9px 13px;
  133. margin: 6px 0;
  134. border: 1px solid;
  135. border-radius: 5px;
  136. display: flex;
  137. align-items: center;
  138. transition: left 1.5s, right 1.5s;
  139. }
  140.  
  141. .h2p-div-notify-item-in {
  142. right: 0;
  143. }
  144.  
  145. .h2p-div-notify-close {
  146. position: absolute;
  147. top: 15px;
  148. right: 20px;
  149. cursor: pointer;
  150. }
  151.  
  152. .h2p-div-notify-close::before, .h2p-div-notify-close::after {
  153. position: absolute;
  154. content: '';
  155. width: 12px;
  156. height: 2px;
  157. background: chocolate;
  158. }
  159.  
  160. .h2p-div-notify-close::before {
  161. transform: rotate(45deg);
  162. }
  163. .h2p-div-notify-close::after {
  164. transform: rotate(-45deg);
  165. }
  166. `;
  167. document.body.appendChild(style_notify);
  168.  
  169. const div_notify = document.createElement('div');
  170. div_notify.id = 'h2p-div-notify-container';
  171. document.body.appendChild(div_notify);
  172.  
  173. const Notify = function() {
  174. this.createNotify = ({ msg = '', type = notifyType.default, autoClose = true }) => {
  175. const ran = Math.floor(Math.random() * 100000000);
  176. const div_notify_item = document.createElement('div');
  177. div_notify_item.id = `h2p-div-notify-${ran}`;
  178. div_notify_item.classList.add('h2p-div-notify-item');
  179. div_notify_item.style.backgroundColor = type.bgColor;
  180. div_notify_item.style.borderColor = type.bdColor;
  181. div_notify_item.innerHTML = msg;
  182. $H2P('div#h2p-div-notify-container').appendChild(div_notify_item);
  183.  
  184. const div_notify_item_close = document.createElement('div');
  185. div_notify_item_close.id = `h2p-div-notify-close-${ran}`;
  186. div_notify_item_close.classList.add('h2p-div-notify-close');
  187. 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]}`); })
  188. $H2P('div#h2p-div-notify-container').appendChild(div_notify_item_close);
  189.  
  190. setTimeout((id) => {
  191. // 显示通知栏
  192. $H2P(`#${id}`).classList.add('h2p-div-notify-item-in');
  193. autoClose && setTimeout(this.closeNotify, 4000, id);
  194. }, 100, div_notify_item.id);
  195. }
  196.  
  197. this.closeNotify = (id = '') => {
  198. $H2P(`#${id}`).classList.remove('h2p-div-notify-item-in');
  199. setTimeout(() => {
  200. $H2P('div#h2p-div-notify-container').removeChild($H2P(`#${id}`));
  201. }, 1500);
  202. }
  203. }
  204. return new Notify();
  205. })();
  206. })(window);