util

TL;DR

目前为 2024-11-04 提交的版本。查看 最新版本

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

  1. // ==UserScript==
  2. // @name util
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @license MIT
  6. // @description TL;DR
  7. // @author https://greasyfork.org/ja/users/705684
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. /**
  15. * format to HH:MM:SS
  16. * @param {Date} date JavaScript Date object
  17. * @param {number} offsetHours UTC to LocalTime
  18. * @return {string} HH:MM:SS
  19. */
  20. window.formatTime = date => {
  21. let d = date && date !== 0 ? new Date(date) : Date.now();
  22. d -= new Date().getTimezoneOffset() * 60 * 1000;
  23. d /= 1000;
  24. const s = (d | 0) % 60;
  25. d /= 60;
  26. const m = (d | 0) % 60;
  27. d /= 60;
  28. const h = (d | 0) % 24;
  29. return [h, m, s].map(v => v.toString().padStart(2, '0')).join(':');
  30. };
  31.  
  32. /**
  33. * calc PseudoRandom from seed
  34. * @param {string} seed
  35. * @return {number} 0 < x <= 1
  36. */
  37. window.pseudoRandomBy = async seed => {
  38. const [a, b] = new Uint8Array(await window.crypto.subtle.digest('SHA-1', (new TextEncoder()).encode(seed)));
  39. const unique = (a << 8) + b;
  40. return unique / 0x10000;
  41. };
  42. })();