Greasy Fork 还支持 简体中文。

H2P: 通用脚本

通用脚本

目前為 2020-09-13 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/411278/847384/H2P%3A%20%E9%80%9A%E7%94%A8%E8%84%9A%E6%9C%AC.js

  1. // ==UserScript==
  2. // @name H2P: 通用脚本
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.1
  5. // @icon http://www.douyutv.com/favicon.ico
  6. // @description 通用脚本
  7. // @author H2P
  8. // @compatible chrome
  9. // ==/UserScript==
  10.  
  11. (() => {
  12. 'use strict';
  13.  
  14. // 在字符串前(后)添加 0
  15. function add0(s = '', len = 0, isAddFront = true) {
  16. s = s.toString();
  17. while (s.length < len) { s = isAddFront ? '0' + s : s + '0'; }
  18. return s;
  19. }
  20. // 返回毫秒
  21. function timeMS(num = 0) {
  22. num = Number.parseInt(num);
  23. return num < 946684800000 ? num * 1000 : num;
  24. }
  25.  
  26. const $H2P = (xpath = 'body', queryOneElement = true) => queryOneElement ? document.querySelector(xpath) : Array.from(document.querySelectorAll(xpath));
  27. const $LS = {
  28. init: (itemKey = '', itemPre = {}) => {
  29. let item = Object.assign({}, itemPre, $LS.get(itemKey));
  30. for (let key in item) { if (!(key in itemPre)) { delete item[key]; } }
  31. localStorage.removeItem(itemKey);
  32. localStorage.setItem(itemKey, JSON.stringify(item));
  33. return item;
  34. },
  35. set: (itemKey = '', item = {}) => { localStorage.setItem(itemKey, JSON.stringify(item)); },
  36. get: (itemKey = '') => JSON.parse(localStorage.getItem(itemKey)) || {},
  37. remove: (itemKey = '') => { localStorage.removeItem(itemKey); }
  38. }
  39. const $INVL = {
  40. clear: (INVLID) => { clearInterval(INVLID); INVLID = null; }
  41. }
  42. const $TIME = {
  43. hms: (time = 0) => {
  44. let h = Number.parseInt(time / 3600000);
  45. let m = Number.parseInt(time % 3600000 / 60000);
  46. let s = Number.parseInt(time % 3600000 % 60000 / 1000);
  47. return {
  48. h: add0(h, 2),
  49. m: add0(m, 2),
  50. s: add0(s, 2)
  51. }
  52. }
  53. }
  54.  
  55. // return millisecond
  56. Date.prototype.$timems = Date.prototype.getTime;
  57. // return second
  58. Date.prototype.$times = function() { return Number.parseInt(this.getTime() / 1000); }
  59. // format time: yyyy-MM-dd hh-mm-ss
  60. 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)}`; }
  61. // format date: yyyy-MM-dd
  62. Date.prototype.$formatDate = function() { return `${this.getFullYear()}-${add0(this.getMonth() + 1, 2)}-${add0(this.getDate(), 2)}`; }
  63. })();