H2P: utils

utils

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

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址