H2P: utils

针对 Date、localStorage 的操作

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

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

  1. // ==UserScript==
  2. // @name H2P: utils
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.7
  5. // @icon http://www.douyutv.com/favicon.ico
  6. // @description 针对 Date、localStorage 的操作
  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. * 键盘按键对应的 event code
  30. */
  31. this.keyCode = {
  32. 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71,
  33. 'h': 72, 'i': 73, 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78,
  34. 'o': 79, 'p': 80, 'q': 81, 'r': 82, 's': 83, 't': 84,
  35. 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90,
  36. }
  37.  
  38. /**
  39. * 返回毫秒
  40. * @param {Number} num
  41. */
  42. this.timeMS = (num = 0) => {
  43. num = Number.parseInt(num);
  44. return num < 946684800000 ? num * 1000 : num;
  45. }
  46. /**
  47. * localStorage 相关操作
  48. */
  49. this.LS = (() => {
  50. function LS() {
  51. this.init = (itemKey = '', itemPre = {}) => {
  52. let item = Object.assign({}, itemPre, this.get(itemKey));
  53. for (let key in item) { if (!(key in itemPre)) { delete item[key]; } }
  54. localStorage.removeItem(itemKey);
  55. localStorage.setItem(itemKey, JSON.stringify(item));
  56. return item;
  57. }
  58. this.set = (itemKey = '', item = {}) => { localStorage.setItem(itemKey, JSON.stringify(item)); },
  59. this.get = (itemKey = '') => JSON.parse(localStorage.getItem(itemKey)) || {},
  60. this.remove = (itemKey = '') => { localStorage.removeItem(itemKey); }
  61. }
  62. return new LS();
  63. })();
  64. this.HMS = (time = 0) => {
  65. time = this.timeMS(time);
  66. let h = Number.parseInt(time / 3600000);
  67. let m = Number.parseInt(time % 3600000 / 60000);
  68. let s = Number.parseInt(time % 3600000 % 60000 / 1000);
  69. return {
  70. h: add0(h, 2),
  71. m: add0(m, 2),
  72. s: add0(s, 2),
  73. }
  74. }
  75. }
  76. return new Util();
  77. })();
  78.  
  79. // return millisecond
  80. Date.prototype.$timems = Date.prototype.getTime;
  81. // return second
  82. Date.prototype.$times = function() { return Number.parseInt(this.getTime() / 1000); }
  83. // format time: yyyy-MM-dd hh-mm-ss
  84. 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)}`; }
  85. // format date: yyyy-MM-dd
  86. Date.prototype.$formatDate = function() { return `${this.getFullYear()}-${add0(this.getMonth() + 1, 2)}-${add0(this.getDate(), 2)}`; }
  87.  
  88. /**
  89. * 根据 xpath 查询元素
  90. * @param {String} xpath
  91. * @param {Boolean} queryOneElement
  92. */
  93. w.$H2P = (xpath = 'body', queryOneElement = true) => queryOneElement ? document.querySelector(xpath) : Array.from(document.querySelectorAll(xpath));
  94. })(window);